[PHP] Gestion Poéle à pellets Rika

Vous avez créé un script LUA dont vous êtes fier, un .sh génial, un programme Python hors du commun, un Tuto, c'est ici que vous pouvez les partager.
Celtic80
Messages : 10
Inscription : 01 oct. 2016, 21:50

Re: Gestion Poéle à pellets Rika

Message par Celtic80 »

Bonjour la clé FIRENET est un serveur http ; C'est un modèle ST SPWF01S Intelligent WiFi Module !
USB\VID_0483&PID_5740
driver : STMicroelectronics Virtual COM Port
Dernière modification par Celtic80 le 10 oct. 2017, 22:55, modifié 1 fois.
razorbak
Messages : 26
Inscription : 26 oct. 2016, 23:05

Re: Gestion Poéle à pellets Rika

Message par razorbak »

et tu as reussi à interfacer directement avec lui sans passer par le cloud ?
Pour le moment j'ai un script qui utilise une 'API' de leur server rika mais en cas de coupure internet c'est mort.

Merci
Celtic80
Messages : 10
Inscription : 01 oct. 2016, 21:50

Re: Gestion Poéle à pellets Rika

Message par Celtic80 »

Non rien pour l' instant .
J' ai essayé de discuté directement avec la clé en commande AT mais elle ne répond pas .
As tu un script qui envoie les données ?
razorbak
Messages : 26
Inscription : 26 oct. 2016, 23:05

Re: Gestion Poéle à pellets Rika

Message par razorbak »

oui emission reception mais je n'utilise pas domoticz donc c'est un truc en php que tu devrais adapter
Celtic80
Messages : 10
Inscription : 01 oct. 2016, 21:50

Re: Gestion Poéle à pellets Rika

Message par Celtic80 »

Pas de soucis ! peux tu le mettre en ligne stp .
Mais je n' abandonne pas les test pour le dialogue direct.
razorbak
Messages : 26
Inscription : 26 oct. 2016, 23:05

Re: Gestion Poéle à pellets Rika

Message par razorbak »

alors le voila.
C'est sale et j'ai cleané des trucs sans rapport mais ca doit marcher en gros. Il y a les fonctions principales à appeler

Code : Tout sélectionner

<?php
$now = round(microtime(true)*1000,0);
$get = 'https://www.rika-firenet.com/api/client/31711030/status?nocache=';
$controls_url = 'https://www.rika-firenet.com/api/client/31711030/controls';
$login_url = 'https://www.rika-firenet.com/web/login';

$username_rika = 'toto%40coucou.fr';
$password_rika = 'monpassword';

$login = 'https://www.rika-firenet.com/web/login';
$log = 'https://www.rika-firenet.com/api/client';

$timeout1 = 30;
$timeout2 = 10;
$nb_tentative_config = 3;
$interval_entre_tentative = 15;
$interval_verification_commande = 10;
$path_cookie = "/root/xavier/scripts/rika/Cookie_connexion_Rika.txt";
$StatusFile = "/root/xavier/scripts/rika/status.json";


$script_controle_poele = '/usr/bin/php /root/xavier/scripts/rika/set_power_manual.php';
$script_poele = '/root/xavier/scripts/rika/set_power_manual.php';


function stop_poele()
	{
		global $script_controle_poele;
		logs("Action Stop");
		$output = programme_poele_manuel(0);
		echo "$output";
		return;
	}

function programme_poele ($mode,$value)
	{
		global $config,$script_controle_poele;
		//$corrected_value = roundUpToAny($value);
		$corrected_value = $value;
		logs ('Poele programme pour '.$mode." ".$corrected_value."%");

		//$output = shell_exec($script_controle_poele." ".$corrected_value);
		$output = programme_poele_manuel($corrected_value);
		echo "$output";
		change_config('cur_state','heat',$config['timestamp']);
		change_config('lastState','heat',$config['timestamp']);
		return;
	}

function logs($string)
	{
		global $debug;
		$da = date('Y-m-d H:i:s');
		if ($debug)
				{
					echo "$da : $string \n";
				}
	}

function programme_poele_manuel ($power_consigne)
	{
		echo date("Y-m-d H:i:s")."\n";

		$status = login();

		if ($status['connected'])
			{

				//echo "Connected\n";

				$result_controls = set_power_manual($power_consigne);
				
				logout();
			}

		else
			{
				echo 'Pb de connection au site rika : '.$status['curl_errno'].' : '.$status['curl_error']."\n";
			}

	}

function login ()

	{
		global $login_url,$username_rika,$password_rika,$path_cookie;

		$postinfo = "email=".$username_rika."&password=".$password_rika;	
		$status = false;
		$ch = curl_init();
		curl_setopt($ch, CURLOPT_HEADER, false);
		curl_setopt($ch, CURLOPT_NOBODY, false);
		curl_setopt($ch, CURLOPT_URL, $login_url);
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
		curl_setopt($ch, CURLOPT_FAILONERROR, true);

		curl_setopt($ch, CURLOPT_COOKIEJAR, $path_cookie);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in
		//set the cookie the site has for certain features, this is optional
		curl_setopt($ch, CURLOPT_COOKIE, "cookiename=0");
		curl_setopt($ch, CURLOPT_USERAGENT,
		    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);

		curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
		curl_setopt($ch, CURLOPT_POST, 1);
		curl_setopt($ch, CURLOPT_POSTFIELDS, $postinfo);
		$return = curl_exec($ch);

		// Retourne le numéro d'erreur de la dernière opération cURL.  
		$curl_errno = curl_errno($ch);
		$curl_error = curl_error($ch);

		if ($curl_errno > 0) 
			{
				echo "cURL Error ($curl_errno): $curl_error\n";
				$status['connected'] = false;
				$status['curl_errno'] = curl_errno($ch);
				$status['curl_error'] = curl_error($ch);
				
				exit;                                               // mettre en veille en mode développement 
			} 
		else 
			{
				//echo "Data received phase 1 : $return\n";
				$status['connected'] = true;
			}

		curl_close($ch);

		return $status;

	}

function logout()
	{

		global $timeout2,$path_cookie;
		$url = 'https://www.rika-firenet.com/web/logout';

		$ch = curl_init($url);

		curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
		curl_setopt($ch, CURLOPT_TIMEOUT, $timeout2);
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout2);

		if (preg_match('`^https://`i', $url))
			{
				curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
				curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
			}

		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
		curl_setopt($ch, CURLOPT_NOBODY, true);

		curl_exec($ch);
		curl_close($ch);

		unlink($path_cookie);  // Supprime le fichier cookie

		//echo "Logout ! \n";

	}

function set_power_manual($power)
	{
		global $connect_id,$controls_url,$path_cookie,$timeout2;
		global $nb_tentative_config ,$interval_entre_tentative ,$interval_verification_commande;

		$power_valide = array (0,30,35,40,45,50,55,60,65,70,75,80,85,90,95,100);

		$power_requested = $power;
		echo "power requested : ".$power_requested."\n";
		if ($power < 30) {$power = 0;}
		if ($power > 100) {$power = 100;}
		if (($power >= 30) && ($power <= 100))
			{
				$power = (int) (round($power / 5)) * 5;
				echo "power config : ".$power."\n";
 	
			}

		
		for ($i=1; $i <= $nb_tentative_config; $i++) 
			{ 
			
				$status_array = get_status();
				if  ($status_array['ok'])
					{
						$status_values = $status_array['info'];
						if (in_array($power, $power_valide,true))

							{		

								$on_off = 0;
								if ($status_values['controls']['onOff']) {$on_off = 1;}
								
								if ((($on_off) == $power) || (($status_values['controls']['onOff']) && ($power == $status_values['controls']['heatingPower']) && ($status_values['controls']['operatingMode'] == 0)))
									{
										$retour['message'] = "Aucun changement necessaire puissance $power en cours \n";
										echo $retour['message']."\n";
										return $retour;
									}
								else
									{
										echo "configuration du poele pour passer en manuel puissance $power\n";
										
										$control = '';



										// preparation du fichier de config control //
										foreach ($status_values['controls'] as $key => $value) {
											
											$value_back = $value;
											if ($key == 'heatingPower')	{$value = $power;}
											if ($key == 'operatingMode') {$value = 0;}
											if ($key == 'onOff') {$value = 'true';}
											
											if ($power == 0)
												{
													if ($key == 'heatingPower')	{$value = $value_back;}
													if ($key == 'operatingMode') {$value = $value_back;}
													if ($key == 'onOff') {$value = 'false';}
												}

											if (is_bool($value))
												{
													if ($value) {$value = "true";} else {$value = "false";}
												}
											

											$control  = $control.'&'.$key.'='.$value;
										}
										$control = substr($control, 1);

										$ch = curl_init();
										curl_setopt($ch, CURLOPT_URL, $controls_url);

										curl_setopt($ch, CURLOPT_USERAGENT,
										    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
										curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout2); // 10s timeout time for cURL connection
										curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // allow https verification if true
										curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // check common name and verify with host name
										curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "VeriSignClass3PublicPrimaryCertificationAuthority-G5.pem"); // allow ssl cert direct comparison
										curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
										curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in

										curl_setopt($ch, CURLOPT_POSTFIELDS,$control); 
										curl_setopt($ch, CURLOPT_HTTPHEADER,array('Content-Type: application/x-www-form-urlencoded')); 

										$result=curl_exec ($ch);
										$retour['message'] = $result;
										echo "Code retour passage commande : ".$result."\n";
										
									}

							}
						else
							{
								echo "Puissance non valide : $power \n";
							}

					}

				sleep($interval_verification_commande);

				$status_array = get_status();
				if  ($status_array['ok'])
					{
						if (($status_array['info']['controls']['onOff']) && ($status_array['info']['controls']['heatingPower'] == $power) && ($status_array['info']['controls']['operatingMode'] == 0))
							{
								echo "Ordre confirme apres $i tentative \n ";
								break;
							}
						else
							{	
								echo "Ordre non valide apres $i tentative \n ";
								sleep($interval_entre_tentative);
							}
					}
				else
					{
						sleep($interval_entre_tentative);
					}


			}

	}

function get_status()
	{

		global $get,$now,$timeout2,$path_cookie,$StatusFile;
		

		//  Initiate curl
		$ch = curl_init();
		$status['ok'] = false;
		$status['error'] = "";
		$status['curl_errno'] = "";
		$status['curl_error'] = "";
		
		// Options
		curl_setopt($ch, CURLOPT_URL, $get.$now); // set url

		curl_setopt($ch, CURLOPT_USERAGENT,
		    "Mozilla/5.0 (Windows; U; Windows NT 5.0; en-US; rv:1.7.12) Gecko/20050915 Firefox/1.0.7");
		curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, $timeout2); // 10s timeout time for cURL connection
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0); // allow https verification if true
		curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); // check common name and verify with host name
		curl_setopt($ch, CURLOPT_CAINFO, getcwd() . "VeriSignClass3PublicPrimaryCertificationAuthority-G5.pem"); // allow ssl cert direct comparison
		curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_ANY);
		curl_setopt($ch, CURLOPT_COOKIEFILE, $path_cookie); // file to read cookies in

		// Execution
		$data = curl_exec($ch);

		$curl_errno = curl_errno($ch);
		$curl_error = curl_error($ch);

		if ($curl_errno > 0) 
			{
				echo "Erreur lors de la collecte des infos \n";
				
				echo "cURL Error ($curl_errno): $curl_error\n";
				$status['ok'] = false;
				$status['error'] = "erreur curl";
				$status['curl_errno'] = curl_errno($ch);
				$status['curl_error'] = curl_error($ch);
				
				exit;                                               // mettre en veille en mode développement 
			} 
		else 
			{
				$status['ok'] = true;
				$File_Data = $data;
				if (!$File_Data || strlen($File_Data) == 0 || ($File_Data == "Authorisation required!"))
					{
  						$status['ok'] = false;
  						echo "Erreur lors de la collecte des infos \n";
  						$status['error'] = "json status file incorrect : $File_Data \n";
  						echo $status['error']."\n";
					}
				else 
					{
						$status['info'] = json_decode($File_Data,true);
					}
				
			}

		curl_close($ch);

		return $status;

	}




?>
Celtic80
Messages : 10
Inscription : 01 oct. 2016, 21:50

Re: Gestion Poéle à pellets Rika

Message par Celtic80 »

Cool Merci ! Je n' ai pas trop de temps en ce moment mais si j' ai des nouvelles je posterai.
reiger123
Messages : 5
Inscription : 03 oct. 2017, 07:48

Re: Gestion Poéle à pellets Rika

Message par reiger123 »

Thanks Celtic80! I Googled a bit and figured out that there are 4 different types of these modules: SPWF01SA.11, SPWF01SA.21, SPWF01SC.11 and SPWF01SC.21. But these have a UART interface instead of the USB interface the Rika Wifi Module is providing. I wonder if it's a matter of hooking it up to a UART to USB bridge to get a working product.

I'm investigating because I have multiple pellet stoves in use and I rather not buy multiple official Rika Wifi Modules. They are too expensive.
razorbak
Messages : 26
Inscription : 26 oct. 2016, 23:05

Re: Gestion Poéle à pellets Rika

Message par razorbak »

in that case the easiest way is to connect to the serial interface directly and simulate the MODEM
I did it with rpi to send ATDT comment and it works.

I stoped it because once (other user got the same) the RPI send an un-expected sequence and the stove instead of rejecting the command it crash and I had to re-install the firmware. It scared me a bit and send I prefer to use the official way now.
reiger123
Messages : 5
Inscription : 03 oct. 2017, 07:48

Re: Gestion Poéle à pellets Rika

Message par reiger123 »

That would indeed be the best option if I want to setup an offline system not depending on the Rika Firenet platform. For now I want a cheap option to connect to the Rika Firenet platform. Later I want to check how their API works and maybe integrate my domotica system with it.

It's indeed scary that the firmware crashes when unexpected sequences are received. If it can crash it can do other unexpected actions. So you bought the commercial Rika Wifi module?
Répondre