MQTT serial bridge with EspEasy

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
Alan_37
Normal user
Posts: 18
Joined: 30 Apr 2017, 00:04

MQTT serial bridge with EspEasy

#1 Post by Alan_37 » 20 Apr 2020, 17:53

Hi,

Was wondering if there is a plugin to use Espeasy as an MQTT serial bridge to make a microcontroller
MQTT enabled via Esp.

Any ideas Please

User avatar
grovkillen
Core team member
Posts: 3621
Joined: 19 Jan 2017, 12:56
Location: Hudiksvall, Sweden
Contact:

Re: MQTT serial bridge with EspEasy

#2 Post by grovkillen » 20 Apr 2020, 18:18

Nothing like that is present no.
ESP Easy Flasher [flash tool and wifi setup at flash time]
ESP Easy Webdumper [easy screendumping of your units]
ESP Easy Netscan [find units]
Official shop: https://firstbyte.shop/
Sponsor ESP Easy, we need you :idea: :idea: :idea:

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

Re: MQTT serial bridge with EspEasy

#3 Post by TD-er » 21 Apr 2020, 01:40

Well you can post the raw data from a serial port to MQTT via the "Serial Proxy" plugin.
It does have a few limitations like you only can send a single init string (called at PLUGIN_READ) to trigger a read on the connected device and it does need to have a CRLF (or just CR, not 100% sure) to recognize it as a full line.

Alan_37
Normal user
Posts: 18
Joined: 30 Apr 2017, 00:04

Re: MQTT serial bridge with EspEasy

#4 Post by Alan_37 » 24 Apr 2020, 19:53

hi
Thanks for your reply and suggestions I came across
this simple ESP8266 (Arduino IdE) code that works 100% perfectly
it would be really great if someone can build a plugin from it

Code: Select all

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

const char* ssid = "SSID";
const char* password = "Password";
const char* mqtt_server = "192.168.10.4";
const char* topic_rx = "uart/rx";
const char* topic_tx = "uart/tx";


const byte numChars = 100;
char receivedChars[numChars]; // an array to store the received data
boolean newData = false;

WiFiClient espClient;
PubSubClient client(espClient);

void setup() {
  pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  Serial.begin(19200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void setup_wifi() {
  delay(10);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }
}

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

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;
}

void reconnect() {
  String clientName;
  clientName += "esp8266-";
  uint8_t mac[6];
  WiFi.macAddress(mac);
  clientName += macToStr(mac);
  clientName += "-";
  clientName += String(micros() & 0xff, 16);
  while (!client.connected()) {
    if (client.connect((char*) clientName.c_str())) { // random client id
      digitalWrite(BUILTIN_LED, LOW); // low = high, so this turns on the led
      client.subscribe(topic_rx); // callback: mqtt bus -> arduino
    } else {
      digitalWrite(BUILTIN_LED, HIGH); // high = low, so this turns off the led
      delay(5000);
    }
  }
}

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

  recvWithEndMarker();
  showNewData();
}

void recvWithEndMarker() {
  static byte ndx = 0;
  char endMarker = '\n';
  char rc;
 
  while (Serial.available() > 0 && newData == false) {
    rc = Serial.read();

    if (rc != endMarker) {
      receivedChars[ndx] = rc;
      ndx++;
      if (ndx >= numChars) {
        ndx = numChars - 1;
      }
    }
    else {
      receivedChars[ndx] = '\0'; // terminate the string
      ndx = 0;
      newData = true;
    }
  }
}

void showNewData() {
  if (newData == true) {
    client.publish(topic_tx, receivedChars); // publish: arduino -> mqtt bus
    newData = false;
  }
}

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

Re: MQTT serial bridge with EspEasy

#5 Post by TD-er » 25 Apr 2020, 15:29

I edited your post to add code tags wrapping it.
That makes it a lot easier to read, or else all indents would be lost.

I looked at the code and it looks to me it is very much the same as the SerialProxy plugin does.
Only thing it doesn't do is redirecting the receiving messages published in the subscribed topic to the serial port.

Post Reply

Who is online

Users browsing this forum: No registered users and 20 guests