RFlink wemos gateway

Moderators: rtenklooster, Voyager, BertB, Stuntteam

Post Reply
Message
Author
Flop
New user
Posts: 6
Joined: 23 Apr 2020, 20:07

RFlink wemos gateway

#1 Post by Flop » 11 Apr 2021, 01:18

Hi,

I wanted to have a bridge between my rflink and my openHab server.
So I have connected my RFlink to a wemos.
The wemos read serial from rflink and send it to mqtt server.
You can link sensors to wemos interactively.

espeasy firmware is free and rflink too, so i share my work too.
This is an arduino ide file.
( I Know this is not state of the art coding ! )

Code: Select all

#include <SoftwareSerial.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <string.h>
#include <EEPROM.h>;

#define RESET_EEPROM false



const char* ssid     = "WifiSSID";      // SSID
const char* password = "wifipass";      // Password
#define mqtt_server "server"

WiFiClient espClientRFLINK;
PubSubClient mqttclient(espClientRFLINK);


SoftwareSerial SoftSer(D1,D2);

char temp[6];
byte idx = 0;
int i=0;

byte id_capteur=0;


int temperature=0;
int humidite=0;
int EtatBatterie=0;
char root_topic[16]="RFLINK/capteur0";
char topic[50];
char inData[80];


typedef struct {
  char marque[20];
  char ID[20];
} capteurs;

capteurs capteur[10];

void EEPROMcommit()
{
  if (EEPROM.commit()) {
    Serial.println("Data successfully committed");
  } else {
    Serial.println("ERROR! Data commit failed");
  }
}

void GestionEEPROM()
{
   Serial.println(EEPROM.read(0));
   Serial.println(EEPROM.read(1));
   if ( (EEPROM.read(0)!=0xAA) || (EEPROM.read(1)!=0xAA) )
    {// premier démarrage, on efface l'eeprom et on marque les deux premier bytes en AA 
      Serial.println("Premier démarrage, effacement de l'eeprom");
      EEPROM.write(0,0xAA);
      EEPROM.write(1,0xAA);
      for (int i=2;i<512;i++)
      EEPROM.write(i,0);
      EEPROMcommit();
    }
   else  // ce n'est pas le premier démarrage, on recharge la liste des capteurs associés
    {
    LireListeCapteurs();
    ListerLesCapteurs();
    }
   
}

void LireListeCapteurs()
{
    for (int i=0;i<10;i++)
      {   
      for(int j=0;j<20;j++)
        {
        capteur[i].marque[j]=char(EEPROM.read(2+i*40+j));
        capteur[i].ID[j]=char(EEPROM.read(2+i*40+j+20));      
        }
      }  
}

void EnregistrerListeCapteurs(){
  EEPROM.write(0,0xAA);
  EEPROM.write(1,0xAA);
  for (int i=0;i<10;i++)
    {
    for (int j=0;j<20;j++)
      {
      EEPROM.write(2+i*40+j,capteur[i].marque[j]);
      EEPROM.write(2+i*40+j+20,capteur[i].ID[j]);  
      }
    }
  EEPROMcommit();
}

void ListerLesCapteurs(){
 for (int i=0;i<10;i++)
  {
  Serial.print("Capteur ");
  Serial.print(i);
  Serial.print(" :");
  Serial.print(capteur[i].marque);
  Serial.print(":");
  Serial.println(capteur[i].ID);   
  }
}

void InitCapteurs()
{
  for (int i=0;i<10;i++)
    for (int j=0;j<20;j++)
    {
    capteur[i].marque[j]=0;
    capteur[i].ID[j]=0;  
    }
}

void setup() {
  
  Serial.begin(57600);
  SoftSer.begin(57600);
  delay(1000);
  Serial.println("INIT Serial OK");
  EEPROM.begin(512);
  delay(3000);
  InitCapteurs();
  GestionEEPROM();
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  } 
  Serial.println("");
  Serial.println("WiFi connected");  
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  mqttclient.setServer(mqtt_server, 1883);
  initCode();
  
}

void reconnect() {
  // Loop until we're reconnected
  while (!mqttclient.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    // If you do not want to use a username and password, change next line to
    // if (client.connect("ESP8266Client")) {
    if (mqttclient.connect("ESP8266ClientRFLINK")) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(mqttclient.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}



char reception[80];
const char * separators = ";";

String code[8];
uint8_t indexcode=0;



void initCode(){
  for (int i=0;i<8;i++)
    code[i]="";
}

void printCode(){
  for (int i=0;i<8;i++){
    Serial.print(i);
    Serial.print(":");
    Serial.print(code[i]);
    Serial.print(":");
    }
  Serial.println(":");  
}

boolean ecoute(int duree){

  long temps=millis();
  boolean EnCours=false;
  boolean recu=false;
  idx=0;
  indexcode=0;

  for (int i=0;i<80;i++)
    inData[i]=NULL;

while( (millis()-temps<duree) && (recu==false) )
    {  
    if (EnCours==true)
      {
      temps=millis();
      EnCours=false;
      }
    
    while(SoftSer.available() > 0)
      {
      EnCours=true;
      char aChar = SoftSer.read();
      if( aChar =='\n' )
        {
        char * strToken = strtok(inData,separators);
        while ( strToken != NULL )
          {          
          // Serial.print(":");
          // Serial.print(strToken);
          code[indexcode++]=strToken;
          // Serial.println(":");
          strToken = strtok(NULL,separators);
          }
        idx = 0;
        for (int i=0;i<80;i++)
          inData[i]=NULL;
        //printCode();
        recu=true;  
        } 
     else
        {
        inData[idx] = aChar;
        idx++;
        //inData[idx] = '\n'; // Keep the string NULL terminated
        indexcode=0;
        }  
      }
    }
//Serial.print("recu:");
//Serial.println(indexcode);
if (indexcode!=8) recu=false; // si on ne reçoit pas le nombre de parametre attendu on ignore
return recu;
}


char AttenteReponse(int duree){
  long temps=millis();
  char temp=0;
  while (millis()-temps<duree)
    {
    if (Serial.available()>0)
      {
      temp=Serial.read();
      while (Serial.available()>0) Serial.read();
      return temp;    
      }
    }
  return 0;
}

void verification()
{
  boolean match=false;
  for (int i=0;i<10;i++)
    {
      if (code[3]==capteur[i].ID)
        {
          Serial.print("Match : ");
          match=true;
          char temp[6];
          temp[0]='0';
          temp[1]='x';

          for (int i=2;i<6;i++)
            temp[i]=code[4][i+4];
          temp[6]='\0';
          temperature=strtol(temp,NULL,16);
          if (temperature>0x7FFF) 
            {
            //Serial.println("Negatif");
            temperature-=0x7FFF;
            temperature*=-1;
            
            }
          
          
          Serial.println(i);
          root_topic[14]=i+48; 
          strcpy(topic,root_topic);
          strcat(topic,"/temperature");
          Serial.print(topic);
          Serial.print(":");
          Serial.println(String(temperature/10.0).c_str());          
          mqttclient.publish(topic, String(temperature/10.0).c_str(), true);
          //topic[0]='\n';
          
          temp[0]=code[5][4];
          temp[1]=code[5][5];
          temp[2]='\0';
          humidite=atoi(temp);
          
          strcpy(topic,root_topic);
          strcat(topic,"/humidite");
          Serial.print(topic);
          Serial.print(":");
          Serial.println(String(humidite).c_str()); 
          mqttclient.publish(topic, String(humidite).c_str(), true);
          
        }
     // for (int j=0;j<sizeof(code[2]
    }
if (match==false)
  {
  Serial.print("Capteur non associé : ");
  printCode();
  }
}


void loop() {
 if (!mqttclient.connected()) {
    reconnect();
  }
  mqttclient.loop();

if (ecoute(1000)==true)
  {
    //printCode();
    verification();
  }
//else Serial.println("Timeout");
  
if (Serial.available()>0)
  {
    char rep=AttenteReponse(1000);
    if (rep=='c')
      {
      LireListeCapteurs();
      ListerLesCapteurs();  
      }
    if (rep=='l')
      {
        ListerLesCapteurs();
      }
    if (rep=='e')
      {
        Serial.println("Etes vous sur de vouloir supprimer les capteurs associés ? o/n");
         if (AttenteReponse(3000)=='o')
          {
          EEPROM.write(0,0xAA);
          EEPROM.write(1,0xAA);
          for (int i=2;i<512;i++)
            EEPROM.write(i,0);
          EEPROMcommit();
          LireListeCapteurs();
          ListerLesCapteurs();  
          }
 
      }
    if (rep=='a')//if (Serial.read()=='a')
      {
      //Serial.flush();
      Serial.println("Association");
      Serial.println("Appuyez sur le bouton ou enlever et remettre la pile");
      Serial.println("Attente du signal");
      if (ecoute(10000)==true)
        {
         Serial.println("Code reçu");
         printCode();
         Serial.println("Ajouter ? o/n");
         if (AttenteReponse(10000)=='o')
          {
            //TODO ajout
            
            Serial.println("Liste des capteurs associés :");
            ListerLesCapteurs();

            Serial.println("Choisir le numéro :");
            //int NumeroCapteur=0;
            while(Serial.available()>0) Serial.read();
            int NumeroCapteur=AttenteReponse(10000);
            if ((isDigit(NumeroCapteur))==true)
              {
              for (int i=0;i<sizeof(code[2]);i++)
                capteur[NumeroCapteur-48].marque[i]=code[2][i];
              for (int i=0;i<sizeof(code[3]);i++)
                capteur[NumeroCapteur-48].ID[i]=code[3][i];
              ListerLesCapteurs();            
              EnregistrerListeCapteurs();    
              }
            else
              Serial.println("Vous n'avez pas saisi un numéro, sortie du mode association");
            

          }
         else
          {
          //Serial.flush();
          Serial.println("Sortie du mode association");  
          }
        }
      else 
        {
        Serial.println("pas de code recu, sortie du mode association");  
        }
      
      }
  }


  

}
Last edited by Flop on 11 Apr 2021, 10:25, edited 1 time in total.

konehead
Normal user
Posts: 10
Joined: 10 Apr 2021, 13:04

Re: RFlink wemos gateway

#2 Post by konehead » 11 Apr 2021, 09:09

Nice! I am working on the same project. I bought a RFLINK and would like to connect it to openHab3. I am try to use this one: https://github.com/seb821/espRFLinkMQTT
It works, but the serial interface reboots every time. If i am not able to fix it, i will use your script! So thanks for sharing.

What is your project? I would like to steer Sunblinds.

Flop
New user
Posts: 6
Joined: 23 Apr 2020, 20:07

Re: RFlink wemos gateway

#3 Post by Flop » 11 Apr 2021, 10:29

Obviously, i didn't search enough before making this sketch.
If I had know there were this espRFLinqMQTT already made, i would have use it !

I make my own sensors with espXXXX but i already had some temperature and humidity sensors on rf433mhz and they are build to be outside.
I just wanted to integrated these sensors in my openhab.

konehead
Normal user
Posts: 10
Joined: 10 Apr 2021, 13:04

Re: RFlink wemos gateway

#4 Post by konehead » 11 Apr 2021, 12:35

I just finished my configuration (5 min ago), the plugin (DEV version!) works very well. I am now able to send commands to my shutter, via OpenHab / MQTT. What i realy like is the webinterface and logging. Upcomming day's I will do stability checks. Maybe you can try it..

Post Reply

Who is online

Users browsing this forum: No registered users and 2 guests