Serial port - analyze data?

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Lstt
New user
Posts: 8
Joined: 21 Jan 2018, 17:28

Serial port - analyze data?

#1 Post by Lstt » 01 Mar 2018, 10:15

Dear friends! Can i use EspEasy for analyze (include rules) periodical packets via Hardware or Software serial? For example, serial port recieve 0x23ab2c - i analyze it, and if condition is right, send something via MQTT?
I have this 433mHz reciever, that recieve and analyze 433 mHz RF code:
Full specification - https://www.aliexpress.com/item/RF-tran ... autifyAB=0
Attachments
TB2aP31lXXXXXbXXpXXXXXXXXXX_!!218960742.jpg
TB2aP31lXXXXXbXXpXXXXXXXXXX_!!218960742.jpg (90.11 KiB) Viewed 3842 times
TB2xJE8lXXXXXXUXpXXXXXXXXXX_!!218960742.jpg
TB2xJE8lXXXXXXUXpXXXXXXXXXX_!!218960742.jpg (127.74 KiB) Viewed 3842 times
TB2sd72lXXXXXaBXpXXXXXXXXXX_!!218960742.jpg
TB2sd72lXXXXXaBXpXXXXXXXXXX_!!218960742.jpg (150.25 KiB) Viewed 3842 times

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Serial port - analyze data?

#2 Post by TD-er » 01 Mar 2018, 20:18

I think the most difficult issue would be to keep on reading and decoding.
Otherwise properly received messages would get lost.

It also depends on the messages you want to receive. Do they have some proper start of the packets, which is easy to detect?
Or do you want to support a lot of protocols?
The RFlink and RFXcom both have quite some codebase to support all those protocols and those use a CPU and codebase which is optimized to be as real-time as possible.
ESPeasy is not designed to be real-time.

TL;DR
Yes it should be possible to detect messages, but from within ESPeasy it will be very likely you will miss messages and it is quite hard to add a plugin interface to define what to consider a 'correct' message. On the other hand, adding support for a lot of protocols will take quite a lot of development time.

The ESP will be fast enough, but I don't think ESPeasy is the right base to start from.

Lstt
New user
Posts: 8
Joined: 21 Jan 2018, 17:28

Re: Serial port - analyze data?

#3 Post by Lstt » 01 Mar 2018, 20:33

I allready have code (arduino sketch). But i thought that if it was through the EspaEasy interface with its ready-made solutions (in the part of HTTP protocols, mqtt), it would be more interesting..) :)

Code: Select all

#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
unsigned long counter_code, counter_times, counter_init;
byte incomingBytes[4];//was 4

int i;
int serial_enable=1;
int match1=0;
int match2=0;

const char* iii="time";
char charArray[11];
char charArrayResult[11];
long lastMsg = 0;
//char code[6];

const char* ssid =  "xxx";            // cannot be longer than 32 characters!
const char* password =  "xxx";       // WiFi password
const char* mqtt_server = "xx.cloudmqtt.com";

const char* mqtt_login="xxx";
const char* mqtt_pass="xxx";

 *extern "C" {
#include "ets_sys.h"
#include "os_type.h"
#include "osapi.h"
///#include "mem.h"
#include "user_interface.h"
#include "cont.h"
}*

WiFiClient espClient;
PubSubClient client(espClient);

SoftwareSerial swSer(14,15, false, 128); // RX, TX
SoftwareSerial swSer2(9,10, false, 128); // RX, TX

void setup_wifi() {

  delay(4);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.mode(WIFI_STA);
  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());
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("RF", mqtt_login,mqtt_pass)) {
      Serial.println("connected");
      // Once connected, publish an announcement...
      client.publish("yyy/outTopic", iii);
      // ... and resubscribe
      client.subscribe("inTopic");
    }
    else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 2 seconds");
      // Wait 5 seconds before retrying
      delay(2000);
    }
  }
}

void ICACHE_FLASH_ATTR delays(int delay_duration) {
counter_code=0;
counter_times=0;
counter_init=millis();
  do {
     counter_code=millis()-counter_init; 
     swSer.flush();
     } while (counter_code<delay_duration) ;
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message arrived ");
  Serial.print(topic);
  Serial.print("] ");
  for (int i = 0; i < length; i++) {
    Serial.print((char)payload[i]);
  }
  Serial.println();
} 

void setup() {
  swSer.setTimeout(30);
  Serial.begin(250000);
  swSer.begin(9600);
  Serial.println("\nSoftware serial test started");
  setup_wifi();
  client.setServer(mqtt_server, 23456);
  client.setCallback(callback);
}

int io =0;

void loop() {

 if (swSer.available() > 0 ) {
        swSer.readBytes(incomingBytes, 5);  //was 6
            
            for(int i = 0; i < 5; i++)  //was 6
              {
               if (i) Serial.write(':');
               if (incomingBytes[i] < 16) Serial.write('0');
               Serial.print(incomingBytes[i],HEX);
               charArray[i]=incomingBytes[i];
              }
          Serial.println();
          char charArray[5]; //was 6
          char time_from_start[9];
          int j;
          char* jj ;
          i=1;
          j=sprintf(charArray,"0x%X",incomingBytes[i]);
               while (i<5){  //was 5
               j+=sprintf(charArray+j,"%X",incomingBytes[i+1]);
               i++;
               }
              snprintf(charArrayResult,5,"%X",charArray); //was 6
              client.publish("rrr/rf_code", charArray);
              dtostrf((millis()/1000),9,0,time_from_start);
             // delay(10);  //was 75
              client.publish("rrr/time_s", time_from_start);
              delay(170);  //was 75
               i=0;
               Serial.println("All MQTT topics publish!");
               Serial.print("Time from starting: ");
               Serial.print(millis());
               Serial.println(" ms");
               Serial.print("Free memory in bytes: ");
               Serial.println(ESP.getFreeHeap());
               Serial.println("");
               delays(1700);
                                         }
                          if (!client.connected()) {
                  reconnect();
                      }
            
              client.loop();
              long now = millis();
              if (now - lastMsg > 15000) mqtt_publish();
              
  }

TD-er
Core team member
Posts: 8756
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: Serial port - analyze data?

#4 Post by TD-er » 01 Mar 2018, 21:36

ESPeasy does run a loop like any other Arduino project.
The thing is, it has to do a lot more, like keeping WiFi connected, keeping MQTT connected, process sensor data, etc.
The SoftwareSerial will react on interrupts on the pins defined as serial port, and store it into a buffer.
ESPeasy can trigger certain loops of plugins at "ten-per-second" interval when needed.
But there is no hard guarantee it will service every 0.1 sec. Worst-case scenario may be way longer if something else is keeping the module busy.

No idea at what speed you're trying to read, but you'll have to read from the buffer before it is full and you're loosing data.

So there are quite some tricky parts to take into account.
I guess it can be used, but when combining it with other plugins, there is almost a guarantee it will cause issues.
And also there may be quite some dev-time involved.

Lstt
New user
Posts: 8
Joined: 21 Jan 2018, 17:28

Re: Serial port - analyze data?

#5 Post by Lstt » 02 Mar 2018, 06:32

OK I understood. Thanks for the explanation.

Post Reply

Who is online

Users browsing this forum: Ahrefs [Bot] and 36 guests