Announcement

REXYGEN forum has been moved. This one is closed.
Head over to the new REXYGEN Community Forum at https://forum.rexygen.com.

Looking forward to meeting you there!
 

#1 2018-06-21 03:46:50

soxso2011
Member
From: India
Registered: 2015-11-25
Posts: 37

Machine learning blocks and IOT MQTT/COAP blocks

When can we expect Machine learning blocks and IOT MQTT/COAP blocks?
These are the present buzzword in the control system and required for Industry 4.0.

Offline

#2 2018-06-22 09:59:31

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: Machine learning blocks and IOT MQTT/COAP blocks

Hello soxso2011,

we are of course trying to keep the pace with present standards. We have already implemented MQTT driver which is now under testing. If all goes well it will be part of next release.

For now you can easily integrate REXYGEN with other devices for example using REST API and HTTP2 function block.

Do you have any specific application in your mind?

Regards, Tomas

Offline

#3 2018-06-25 08:21:58

scoobsalamander
Member
From: Belgium - Hulshout
Registered: 2015-10-27
Posts: 217

Re: Machine learning blocks and IOT MQTT/COAP blocks

I'm receiving MQTT messages from different nodes around the house by means of Node-red as an intermediate layer between the MQTT broker and Rexygen.(REST API) This is working like a charm but I agree that it is a much nicer solution if it could be integrated in Rexygen.

Offline

#4 2018-08-24 10:17:29

soxso2011
Member
From: India
Registered: 2015-11-25
Posts: 37

Re: Machine learning blocks and IOT MQTT/COAP blocks

Hi Mr. Tomas, I want to make a sensor network using ESP32 controllers. Also, want to make a IOT network having MQTT/COAP/OPC-UA and HTTP communications simultaneously.

Offline

#5 2018-08-24 10:21:38

soxso2011
Member
From: India
Registered: 2015-11-25
Posts: 37

Re: Machine learning blocks and IOT MQTT/COAP blocks

Hi scoobsalamander,
Please share the architecture and snapshots , I would like to implement the same in my environment too.

Offline

#6 2018-08-26 01:42:14

scoobsalamander
Member
From: Belgium - Hulshout
Registered: 2015-10-27
Posts: 217

Re: Machine learning blocks and IOT MQTT/COAP blocks

I'm still at work but once I'm home I'll try to post some more info. (This will be somewhere next week....)

Offline

#7 2018-08-28 11:41:33

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: Machine learning blocks and IOT MQTT/COAP blocks

Hi soxso2011,

MQTT driver for REXYGEN is still under testing.
OPC-UA is already a part of REXYGEN starting with 2.50.7 version - see documentation: https://www.rexygen.com/doc/PDF/ENGLISH … Ua_ENG.pdf

If I can be of assistance, let me know.

Regards, Tomas

Offline

#8 2018-09-03 09:20:47

scoobsalamander
Member
From: Belgium - Hulshout
Registered: 2015-10-27
Posts: 217

Re: Machine learning blocks and IOT MQTT/COAP blocks

Here we go :

MQTT Mosquitto broker is running on my NAS (Synology) in a Docker container.
But this doesn't have to be...it can also run on the RasPi where your RexyGen is installed.

2018-09-03-09_33_52-Window.png

Once it is up and running you should be able to test the installation. I'm using MQTT.fx for this but there are probably many other tools with the same functionality....

Now your data can be published to Mosquitto. I'm using an ESP8266 with a SHT21 sensor to publish temperature  and humidity at a regular time interval.

2018-09-03-09_57_06-Window.png

Here is the code I'm running on the ESP8266 (Arduino IDE). It is already old code and I didn't checked it but it might give you an idea how the publishing works....

// Code based on
// https://github.com/adafruit/DHT-sensor-library/blob/master/examples/DHTtester/DHTtester.ino
// https://gist.github.com/igrr/7f7e7973366fc01d6393
// https://github.com/iot-playground/Arduino/blob/master/ESP8266ArduinoIDE/DS18B20_temperature_sensor/DS18B20_temperature_sensor.ino

// esp8266 + dht22 + mqtt


#include <PubSubClient.h>
#include <ESP8266WiFi.h>

#include <Arduino.h>
#include <Wire.h>
#include "Adafruit_SHT31.h"

#define BUTTONPIN 13 
#define GREENLEDPIN 14    
#define REDLEDPIN 15 
#define RELAYPIN 5

const char* ssid = "**************";
const char* password = "**************";

char* topic = "basement";

char* server = "192.168.178.32";
char* hellotopic = "basement/SHT21";

Adafruit_SHT31 sht31 = Adafruit_SHT31();

#define REPORT_INTERVAL 30 // in sec
int buttonState = 0;  
String clientName;

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

void handleInterrupt() {
  digitalWrite(REDLEDPIN, !digitalRead(REDLEDPIN));
}


WiFiClient wifiClient;
PubSubClient client(server, 1883, callback, wifiClient);

float oldH ;
float oldT ;

void setup() {
  Wire.begin(2,4);
  Serial.begin(38400);
  pinMode(BUTTONPIN, INPUT_PULLUP);
  pinMode(REDLEDPIN, OUTPUT);
  pinMode(GREENLEDPIN, OUTPUT);
  pinMode(RELAYPIN, OUTPUT);

  attachInterrupt(digitalPinToInterrupt(BUTTONPIN), handleInterrupt, FALLING);
  
  while (!Serial)
    delay(10);     // 

  Serial.println("SHT31 test");
  if (! sht31.begin(0x44)) {   
    Serial.println("Couldn't find SHT31");
    while (1) delay(1);
  }
  
  Serial.println("SHT22 test!");
  delay(20);

  Serial.println();
  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());

  clientName += "esp8266-";
  uint8_t mac[6];
  WiFi.macAddress(mac);
  clientName += macToStr(mac);
  clientName += "-";
  clientName += String(micros() & 0xff, 16);

  Serial.print("Connecting to ");
  Serial.print(server);
  Serial.print(" as ");
  Serial.println(clientName);

  if (client.connect((char*) clientName.c_str())) {
    Serial.println("Connected to MQTT broker");
    Serial.print("Topic is: ");
    Serial.println(topic);
    
    if (client.publish(hellotopic, "hello from ESP8266")) {
      Serial.println("Publish ok");
    }
    else {
      Serial.println("Publish failed");
    }
  }
  else {
    Serial.println("MQTT connect failed");
    Serial.println("Will reset and try again...");
    abort();
  }

  sht31.heater(false);
  oldH = -1;
  oldT = -1;
}

void loop() {

  if (client.state()==0){
    digitalWrite(GREENLEDPIN, HIGH);
  } else {
    digitalWrite(GREENLEDPIN, LOW);
  }

  float t = sht31.readTemperature();
  float h = sht31.readHumidity();

  if (! isnan(t)) {  // check if 'is not a number'
    Serial.print("Temp *C = "); Serial.println(t);
  } else { 
    Serial.println("Failed to read temperature");
  }
  
  if (! isnan(h)) {  // check if 'is not a number'
    Serial.print("Hum. % = "); Serial.println(h);
  } else { 
    Serial.println("Failed to read humidity");
  }
  Serial.println();
  delay(1000);
  
  

  String payload = "{\"humidity\":";
  payload += h;
  payload += ",\"temperature\":";
  payload += t;
  payload += "}";
 
  if (t != oldT || h != oldH )
  {
    sendTemperature(payload);
    oldT = t;
    oldH = h;
  }

  int cnt = REPORT_INTERVAL;

  while (cnt--)
    delay(1000);
}


void sendTemperature(String payload) {
  if (!client.connected()) {
    if (client.connect((char*) clientName.c_str())) {
      Serial.println("Connected to MQTT broker again");
      Serial.print("Topic is: ");
      Serial.println(topic);
    }
    else {
      Serial.println("MQTT connect failed");
      Serial.println("Will reset and try again...");
      abort();
    }
  }

  if (client.connected()) {
    Serial.print("Sending payload: ");
    Serial.println(payload);

    if (client.publish(topic, (char*) payload.c_str())) {
      Serial.println("Publish ok");
      digitalWrite(GREENLEDPIN, LOW);
      delay(500);
      digitalWrite(GREENLEDPIN, HIGH);
    }
    else {
      Serial.println("Publish failed");
    }
    
  }

}




String macToStr(const uint8_t* mac)
{
  String result;
  for (int i = 0; i < 6; ++i) {
    result += String(mac[i], 16);
    if (i < 5)
      result += ':';
  }
  return result;
}

Now it is time to subscribe to the topic by means of Node-RED. The data we hereby receive can then be send by Http POST method in association with the RexygEn's REST API.

2018-09-03-09_28_25-Window.png

[{"id":"86bb9f5f.38b4","type":"mqtt in","z":"35df45de.89998a","name":"","topic":"basement","qos":"2","broker":"2f4461ce.bc17be","x":94,"y":82,"wires":[["92e7e46f.634a38"]]},{"id":"92e7e46f.634a38","type":"json","z":"35df45de.89998a","name":"","x":241,"y":82,"wires":[["60e0bb9c.517d84","e4e95ac8.46ec18"]]},{"id":"28ccaef3.ec5fd2","type":"http request","z":"35df45de.89998a","name":"Post temperature of basement to REXpi","method":"POST","ret":"obj","url":"http://user:password@192.168.178.54:8008/api/tasks/ventilation_task/CNR_basementTemp:ycn?data&mime=application/json","tls":"","x":901,"y":117,"wires":[[]]},{"id":"9bbbf26d.900ec","type":"http request","z":"35df45de.89998a","name":"Post Relative humidity of basement to REXpi","method":"POST","ret":"obj","url":"http://user:password@192.168.178.54:8008/api/tasks/ventilation_task/CNR_basementRHum:ycn?data&mime=application/json","tls":"","x":909,"y":81,"wires":[[]]},{"id":"e4e95ac8.46ec18","type":"function","z":"35df45de.89998a","name":"Temperature","func":"return {\n    payload: '{\"v\":' + msg.payload.temperature + '}',\n    headers: {\n         \"Content-type\" : \"application/json\"\n    }\n};","outputs":1,"noerr":0,"x":448,"y":118,"wires":[["28ccaef3.ec5fd2"]]},{"id":"60e0bb9c.517d84","type":"function","z":"35df45de.89998a","name":"Relative Humidity","func":"return {\n    payload: '{\"v\":' + msg.payload.humidity + '}',\n    headers: {\n         \"Content-type\" : \"application/json\"\n    }\n};","outputs":1,"noerr":0,"x":470,"y":82,"wires":[["9bbbf26d.900ec"]]},{"id":"2f4461ce.bc17be","type":"mqtt-broker","z":"35df45de.89998a","broker":"192.168.178.32","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"willTopic":"","willQos":"0","willPayload":"","birthTopic":"","birthQos":"0","birthPayload":""}]

Just import the above code into Node-red and change to URL's inside the POST function blocks to the correct address of the parameter of  the RexyGen function block. In the MQTT block you need to change the address to the one of your Mosquitto broker (+ use correct topic)

Hope this helps....otherwise just let me know if you have any more questions....

Offline

#9 2018-12-15 12:44:37

soxso2011
Member
From: India
Registered: 2015-11-25
Posts: 37

Re: Machine learning blocks and IOT MQTT/COAP blocks

Thanks for such an elaborated reply! I definitely implement this one !

Offline

#10 2018-12-17 12:48:58

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: Machine learning blocks and IOT MQTT/COAP blocks

Hi guys,

I also use ESP8266 (Wemos D1 mini) with REXYGEN. The ESP8266 works as remote sensor/actuator. Would you be interested in Arduino sketch for ESP8266 demonstrating direct communication with REXYGEN using REST API? Let me know wink

Cheers, Tomas

Offline

#11 2018-12-21 11:27:16

scoobsalamander
Member
From: Belgium - Hulshout
Registered: 2015-10-27
Posts: 217

Re: Machine learning blocks and IOT MQTT/COAP blocks

tomáš čechura wrote:

Hi guys,

I also use ESP8266 (Wemos D1 mini) with REXYGEN. The ESP8266 works as remote sensor/actuator. Would you be interested in Arduino sketch for ESP8266 demonstrating direct communication with REXYGEN using REST API? Let me know wink

Cheers, Tomas

off course we are interested smile

Offline

#12 2018-12-26 12:31:22

tomáš čechura
Administrator
Registered: 2016-02-22
Posts: 371

Re: Machine learning blocks and IOT MQTT/COAP blocks

Hi guys,

please find attached an example on dataexchange between REXYGEN and ESP8266. If you run into any troubles - please, let me know.

All the best in 2019!

Cheers, Tomas

0302-11_ESP8266_REST_API.zip

Offline

#13 2018-12-28 11:49:21

scoobsalamander
Member
From: Belgium - Hulshout
Registered: 2015-10-27
Posts: 217

Re: Machine learning blocks and IOT MQTT/COAP blocks

Tomas, thanks for the code example.

All the best for the REXcontrols-team in 2019 !!

Offline

Board footer

Powered by FluxBB