Communication Arduino Mega-8266 ESP12E-MQTT server

Hello, I conected Arduino Mega with NodeMCU 8266 ESP12 with serial connection. When ESP accepted message from Arduino Mega, submite at first of all accepted message from MQTT server (2 messages: alarm/stav and alarm/zaciatok) next from Arduino Mega and next message from MQTT server (1 message: alarm/koniec). I want to run like this:
Accepted (submite) all message from arduino to ESP12E and next to send to MQTT server. And reverse: accepted (submite) message from MQTT server to NODEMCU 8266 ESP12 and next send to Arduino Mega.
I send you a code and Serial print to monitor. Exchange messages does not correctly. WHY? Thanks
#include <ArduinoJson.h>
#include <SoftwareSerial.h>
#include <ESP8266WiFi.h> // ESP8266
#include <MQTTClient.h>
#include <Servo.h>
Servo myservo;
SoftwareSerial linkSerial(D6,D5); //komunikácia po seriovej linke
//------ wifi settings
char ssid[] = "T-Optik"; // Change this to your network SSID (name).
char pass[] = "zahrada123"; // Change this your network password

//------ MQTT broker settings and topics
const char* broker = "brka123.cloud.shiftr.io";
char clientID[] ="Inteligentný dom";
char mqttUserName[] = "brka123";
char mqttPass[] = "12345678";
WiFiClient net;
MQTTClient client;
unsigned long lastMillis = 0;
String tempAlarmStav;
String tempAlarmZaciatok;
String tempAlarmKoniec;
String tempRed1;
String tempGreen1;
String tempBlue1;
int tempServoDvere;

//========================================= connect
void connect() {
Serial.print("\nconnecting to wifi.");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(1000);
}
//--- create a random client id

Serial.print("\nconect to server...");
while (!client.connect(clientID,mqttUserName,mqttPass)) {
Serial.print(".");
delay(1000);
}
Serial.println("\nconected!");
client.subscribe("alarm/stav",2);
client.subscribe("alarm/zaciatok",2);
client.subscribe("alarm/koniec",2);
client.subscribe("servo/dvere");
client.subscribe("servo/okno");
client.subscribe("svetlo/izba_1/red1");
client.subscribe("svetlo/izba_1/green1");
client.subscribe("svetlo/izba_1/blue1");
client.subscribe("svetlo/izba_2");
client.subscribe("svetlo/izba_3");
}

//========================================= messageReceived
void messageReceived(String &topic, String &payload) {
StaticJsonDocument<800> doc;
//DynamicJsonDocument doc(1024);
Serial.println("Message from MQTT server");
Serial.println("incoming: " + topic + " - " + payload);
Serial.println("-------------------------");
//tempAlarmZaciatok="999";
//tempAlarmKoniec="999";
//tempAlarmStav="999";
//tempServoDvere="999";
if (topic=="alarm/stav")
{
tempAlarmStav=payload;
}
if (topic=="alarm/zaciatok")
{
tempAlarmZaciatok=payload;
}
if (topic=="alarm/koniec")
{
tempAlarmKoniec=payload;
}
if (topic=="servo/dvere")
{
tempServoDvere=payload.toInt();
myservo.write(tempServoDvere);
}
if (topic=="svetlo/izba_1/red1")
{
tempRed1=payload;
}
if (topic=="svetlo/izba_1/green1")
{
tempGreen1=payload;
}
if (topic=="svetlo/izba_1/blue1")
{
tempBlue1=payload;
}
//vysielanie Json dokumentu po seriovej linke
// Send the JSON document over the "link" serial port
//if ((tempAlarmZaciatok!="999")||(tempAlarmKoniec!="999")||(tempAlarmStav!="999")||(tempServoDvere!="999"))
{
doc["stav"] = tempAlarmStav;
doc["zaciatok"] = tempAlarmZaciatok;
doc["koniec"] = tempAlarmKoniec;
doc["izba1Red1"] = tempRed1;
doc["izba1Green1"] = tempGreen1;
doc["izba1Blue1"] = tempBlue1;
serializeJson(doc, linkSerial);
}
}

void prijatSpravuArduinoMega()
{
//príjem z ArduinoMega a odvysielanie na MQTT server
if (linkSerial.available())
{
// Allocate the JSON document
StaticJsonDocument<800> doc;
//DynamicJsonDocument doctoesp(1024);

// Read the JSON document from the "link" serial port
DeserializationError err = deserializeJson(doc, linkSerial);
Serial.setTimeout(2000);
if (err == DeserializationError::Ok)
{
client.publish("alarm/stav",doc["stav"].as(),true,2);
client.publish("alarm/zaciatok",doc["zaciatok"].as(),true,2);
client.publish("alarm/koniec",doc["koniec"].as(),true,2);
Serial.println("Message from Arduino Mega:");
Serial.print("AlarmStav = ");
Serial.println(doc["stav"].as());
Serial.print("AlarmZaciatok = ");
Serial.println(doc["zaciatok"].as());
Serial.print("AlarmKoniec = ");
Serial.println(doc["koniec"].as());
Serial.println("---------------------------");
}
else
{
// Print error to the "debug" serial port
Serial.print("deserializeJson() returned ");
Serial.println(err.c_str());

// Flush all bytes in the "link" serial port buffer
while (linkSerial.available() > 0)
linkSerial.read();
}
}
}
//========================================= setup
//=========================================
void setup() {
myservo.attach(D4);
Serial.begin(9600);
linkSerial.begin(4800);; //iniciácia po seriovej linke na arduino
WiFi.begin(ssid, pass); // Note: Local domain names (e.g. "Computer.local" on OSX) are not supported by Arduino. You need to set the IP address directly.
client.begin(broker, net);
client.onMessage(messageReceived);
connect();
}
//========================================= loop
//=========================================
void loop() {
client.loop();
delay(10); // <- fixes some issues with WiFi stability
if (!client.connected())connect();
{
prijatSpravuArduinoMega();

}
}

pokus_esp.ino (5.12 KB)

@brka

Your topic was Moved to it's current location / section as it is more suitable.

You bypassed at least one of the links on the way in here please now take the time to use them !

Take a few moments to Learn How To Use The Forum.

Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.

This topic was automatically closed 120 days after the last reply. New replies are no longer allowed.