ESP8266 with ESP-NOW as an alternative to nRF24L01+

Thanks for this introudction!

I want to transfere from ESP-now to MQTT.

My plan is to modify the master scetch such that the transmitted info would be like "TemperatureOutdoor,21.3", and that the ESP to MQTT part will use the TemperatureOutdoor as the MQTT topic and 21.3 as the payload. I do not know if "," is the best character to use for this.

My set-up is the slave and master scetches from above and an additional ESP2866 connected to the slave by Tx-> Rx, Rx->Tx and gnd->Gnd.

Using the code below I can transfere the complete transmission from the master to MQTT, it will be posted in the MQTT topic "test". However, despite many many atempts I have not been able to spit the message into two parts (i.e "TemperatureOutdoor" and use this as the MQTT topic instead of "test" and use "21.3" as MQTT payload).

Is there some one that can help me to do this?

The Slave to MQTT code is attached below.

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

const char* ssid = "yyy";
const char* password = "xxxxx";
const char* mqtt_server = "192.168.2.171";

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(115200);
  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("test"); // 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("test", receivedChars); // publish: arduino -> mqtt bus
    newData = false;
  }
}