Arduino Uno Board and NodeMCU 12E communication

Hi there
i want to send data from Arduino UNO to NodeMCU and then to MQTT server. I have both codes for UNO and NodeMCU but data on mqtt server are different. NodeMCU get data which are different from data on UNO. Data is bigger or zero or sometimes late… i dont know what to do
does anyone knows?

here are the codes for UNO and NodeMCU

ARDUINO UNO CODE

#include "ACS712.h"
char wattp[5];
char wattd[5];
char wattt[5];
ACS712 sensor1(ACS712_30A, A0);
ACS712 sensor2(ACS712_30A, A1);
ACS712 sensor3(ACS712_30A, A2);
unsigned long last_time =0;
unsigned long current_time =0;
float Wh1 =0 ; 
float Wh2 =0 ;
float Wh3 =0 ;
void setup() {
  Serial.begin(115200);
  sensor1.calibrate();
  sensor2.calibrate();
  sensor3.calibrate();
}

void loop() {
  float V = 220;
  float I1 = sensor1.getCurrentAC();
  float I2 = sensor2.getCurrentAC();
  float I3 = sensor3.getCurrentAC();
// Serial.println(I);
  float P1 = V * I1;
  float P2 = V * I2;
  float P3 = V * I3;
  float U = P1 + P2 + P3;
  last_time = current_time;
  current_time = millis();    
  Wh1 = Wh1+  P1 *(( current_time -last_time) /3600000.0) ; 
   dtostrf(Wh1, 4, 2, wattp);  
   Wh2 = Wh2+  P2 *(( current_time -last_time) /3600000.0) ; 
   dtostrf(Wh2, 4, 2, wattd);  
   Wh3 = Wh3+  P3 *(( current_time -last_time) /3600000.0) ; 
   dtostrf(Wh3, 4, 2, wattt);
  Serial.print( " (1) W: " );     
  Serial.print(wattp);
  Serial.print("   ");
  Serial.print( " (2) W: " );     
  Serial.print(wattd);
  Serial.print("   ");
  Serial.print( " (3) W: " );     
  Serial.print(wattt);
  Serial.print("          ");
  Serial.print(P1);
  Serial.print("  W/h (1)");
  Serial.print("      ");
  Serial.print(P2);
  Serial.print("  W/h (2)");
  Serial.print("      ");
  Serial.print(P3);
  Serial.print("  W/h (3)");
  Serial.print("          ");
  Serial.print(U);
  Serial.print("  W/h (1,2,3)");
   delay(10000);
  Serial.println();

NodeMCU code:

#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
#define WLAN_SSID       "Z2qqwe"
#define WLAN_PASS       "qwerty"
char wattp[5];
#define AIO_SERVER      "io.adafruit.com"
#define AIO_SERVERPORT  1883                   
#define AIO_USERNAME    "qwert"
#define AIO_KEY         "123456"

WiFiClient client;
 
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish Power = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/power");
//Adafruit_MQTT_Publish bill = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/bill");
void MQTT_connect();

void setup() {
  Serial.begin(115200);
  delay(10);
  Serial.println(F("Adafruit MQTT demo"));

  // Connect to WiFi access point.
  Serial.println(); Serial.println();
  Serial.print("Connecting to ");
  Serial.println(WLAN_SSID);

  WiFi.begin(WLAN_SSID, WLAN_PASS);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println();

  Serial.println("WiFi connected");
  Serial.println("IP address: "); Serial.println(WiFi.localIP());
}

void loop() {
  // Ensure the connection to the MQTT server is alive (this will make the first
  // connection and automatically reconnect when disconnected).  See the MQTT_connect
  // function definition further below.
  MQTT_connect();

 int i=0;
 float watt1;
 if(Serial.available() > 0 ){
   delay(100); //allows all serial sent to be received together
    while(Serial.available() && i<5) {
     wattp[i++] = Serial.read();
    }
    wattp[i++]='\0';
  }

 watt1 = atof(wattp);
  
  Serial.print(F("\nSending Power val "));
  Serial.println(watt1);
  Serial.print("...");

  if (! Power.publish(watt1)) {
    Serial.println(F("Failed"));
  } else {
    Serial.println(F("OK!"));
  }

    
delay(10000);
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
  int8_t ret;

  // Stop if already connected.
  if (mqtt.connected()) {
    return;
  }

  Serial.print("Connecting to MQTT... ");

  uint8_t retries = 3;
  while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
       Serial.println(mqtt.connectErrorString(ret));
       Serial.println("Retrying MQTT connection in 5 seconds...");
       mqtt.disconnect();
       delay(5000);  // wait 5 seconds
       retries--;
       if (retries == 0) {
         // basically die and wait for WDT to reset me
         while (1);
       }
  }
  Serial.println("MQTT Connected!");
}

as you see in this code i am sending data “wattp” but these data’s are different on UNO and NodeMCU and dont know why

i want to send value “U” from arduino uno through nodemcu on mqtt server
please help

Serial communication can be tricky, check out Robin's Serial Input Basics

thanks for this
my knowledge in coding is very low, i am at the beginning
i am looking for someone who knows coding and can tell me what should i change in my code, especially in nodemcu code because i thinks that code for UNO is good (because i am getting good values from UNO).

Is there any other way to send data from UNO to MQTT through nodemcu 12e?

neznamnista:
thanks for this
my knowledge in coding is very low, i am at the beginning
i am looking for someone who knows coding and can tell me what should i change in my code,

Serial communication is tricky because if you do not find the start of the data-package you will not receive the correct data, if you do have the correct variables you data will be incorrect etc. hence Robin took the trouble of creating several examples that can be used for different purposes. I am not saying it is complete (they are all non-blocking) but it does all work and code has been tested. If you want someone to actually fix your particular protocol you should probably be looking in gigs & collabs (where they always say to bring your wallet)

ok, but what do you suggest how to send data from UNO to MQTT using nodemcu?

neznamnista:
ok, but what do you suggest how to send data from UNO to MQTT using nodemcu?

Have you read the thread i referenced in #1

yes but there is no solution for my codes

neznamnista:
yes but there is no solution for my codes

aren't you sending several 'char' 's ?
from the Updated thread

  • Example 2 - Receiving several characters from the Serial Monitor

start there, get the reception code to work as you want it to (on the nodeMCU), browse through example 3 as well, it deals with startmarkers.
once you have the reception working (i suggest you actually use the serial monitor to debug this. The sending part will become obvious.

you are telling me to drive airplane…but i dont know…i am looking for help…as i wrote, code for UNO is working and values are ok…but when i publish values through nodemcu on adafruit, i got different values…maybe there is problem with connection btwn UNO and NodeMCU…i connected like on picture…is it ok and is there any other way of connection?

you are telling me to drive airplane..but i dont know..i am looking for help...as i wrote, code for UNO is working and values are ok..

I am not telling you to drive an airplane, if the other code is yours the Serial communication is not beyond your grasp.
the main problem with your reception code is that you just start reading data, and read the first 5 bytes that arrive and expect them to be the first bytes that you intended to send. If you connect the Uno to the serial-monitor and press the reset button you'll see that at first quite a lot of garbage values are being put out, and that is probably what you are receiving. The tutorial clearly explains that and it explains quite a few other things as well, it just seems a bit more complex because the code receives in a 'non-blocking way' and so your looks simpler, and in some way it is (wait 100 ms, and read all after that in chunks of 5 bytes) but it just doesn't quite work like that. you will need to put start and end markers in transmission (or some kind of header at times can be enough) If you then receive in a blocking or non-blocking way doesn't matter all that much (do mind the wdt reset though on an ESP) in your case. In some cases non-blocking is better, in most cases blocking is also fine, and in rare cases only blocking will work.

i told you driving an airplane because i am beginner here and i found this code here

and i copy it and tried but recieved only 0.0 on mqtt server, but on arduino uno i am receiving good values

if you have time you can see those codes and tell me what to change

and yours explanation is good and now i understand something
so i pressed reset button and got this
<> ⸮l⸮ #|⸮⸮⸮⸮r⸮#⸮c⸮⸮‘o⸮do’⸮⸮⸮cp⸮⸮dsdrd8⸮o⸮ ⸮$ ⸮⸮ b"</>

00:42:26.255 → Sending Power val 0.00
00:42:26.255 → …OK!
00:42:26.255 → OK!
00:42:31.267 →
00:42:31.267 → Sending Power val 0.00
00:42:31.267 → …OK!
00:42:31.267 → OK!

and really

if(Serial.available() > 0 ){
   delay(100); //allows all serial sent to be received together
    while(Serial.available() && i<5) {
     wattp[i++] = Serial.read();
    }
    wattp[i++]='\0';
  }

you are reading 4 bytes, but you are sending quite a bit more

Wh1 = Wh1+  P1 *(( current_time -last_time) /3600000.0) ;
   dtostrf(Wh1, 4, 2, wattp); 
   Wh2 = Wh2+  P2 *(( current_time -last_time) /3600000.0) ;
   dtostrf(Wh2, 4, 2, wattd); 
   Wh3 = Wh3+  P3 *(( current_time -last_time) /3600000.0) ;
   dtostrf(Wh3, 4, 2, wattt);
  Serial.print( " (1) W: " );     
  Serial.print(wattp);
  Serial.print("   ");

as far as i know atof()

watt1 = atof(wattp);

watt1 should be 0 (no numeric value return 0 i believe)

so what do i need to change and try if it works?

i told you driving an airplane because i am beginner here and i found this code here

IoT based Electricity Energy Meter using ESP12 and Arduino

and i copy it and tried but recieved only 0.0 on mqtt server, but on arduino uno i am receiving good values

as expected a 0.0 since atof() returns 0 when it finds a non numeric value.

so what do i need to change and try if it works?

Well don’t take this the wrong way, but what you should change is your attitude. You downloaded some code from the internet, and since the interfacing not working you want me/us/the forum to help you fix it. There is a link to a perfectly good tutorial on serial communication that was written by someone who to the time to develop, explain, test and post all this on this forum because this is a problem that many people run into. If we have to explain the whole process every time someone wants their individual bit of "downloaded’ code repaired/fixed/interfaced we are constantly doing the same thing in a slightly different way

i think you read me wrong...yes, i found this code on internet because i am an amateur for this, and i am trying to find solutions by asking people who knows how to help me and want to do it...I always start with myself and am ready to help everyone and not get into the reasons why I help them, because it is human to help and that makes me happy, and no matter how many times I have to repeat ... and on the other hand , I didn't find the complete code in one place where I could read about everything and try it ...but if this is a problem for you and something that you are repeating to everybody then dont do it...i am thankful for that too

and i am trying to find solutions by asking people who knows how to help me

That is why i provided you with the link to the Serial Input Basics, do study it and adapt your Uno code to send what your nodeMCU code can read. The Issue is both in the sending and in the parsing of what you receive. This is how you can help yourself. If you then have difficulties in either understanding how things work (in the examples in the tutorial) or why things don't work (within the implementation of the tutorial code into your sketch) then i can look at your individual case and see what needs to be modified. But start with the tutorial !

also want to tell you this..on this link i found this code

as you can see i was reading and reading and modified this code which was writing for one sensor...i modified for 3 sensors and for calculating total amps and total watts

for me is huge success, so i am going further

but have problem now and after reading lot of pages i dont know how to modified code for nodemcu to get UNO value "cetvrta" on NodeMcu

i found something on pages you sent me....is this important for my code?

The following code returns a character received through the serial port.

int incomingByte = 0; // for incoming serial data

void setup() {
Serial.begin(9600); // opens serial port, sets data rate to 9600 bps
}

void loop() {
// reply only when you receive data:
if (Serial.available() > 0) {
// read the incoming byte:
incomingByte = Serial.read();

// say what you got:
Serial.print("I received: ");
Serial.println(incomingByte, DEC);
}
}

You can use SerialTransfer.h to automatically transfer your data. The library is installable through the Arduino IDE and includes many examples.

Here are the library’s features:

This library:

  • can be downloaded via the Arduino IDE’s Libraries Manager (search “SerialTransfer.h”)
  • works with “software-serial” libraries
  • is non blocking
  • uses packet delimiters
  • uses consistent overhead byte stuffing
  • uses CRC-8 (Polynomial 0x9B with lookup table)
  • allows the use of dynamically sized packets (packets can have payload lengths anywhere from 1 to 255 bytes)
  • can transfer bytes, ints, floats, and even structs!!

Example TX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  myTransfer.txBuff[0] = 'h';
  myTransfer.txBuff[1] = 'i';
  myTransfer.txBuff[2] = '\n';
  
  myTransfer.sendData(3);
  delay(100);
}

Example RX Arduino Sketch:

#include "SerialTransfer.h"

SerialTransfer myTransfer;

void setup()
{
  Serial.begin(115200);
  Serial1.begin(115200);
  myTransfer.begin(Serial1);
}

void loop()
{
  if(myTransfer.available())
  {
    Serial.println("New Data");
    for(byte i = 0; i < myTransfer.bytesRead; i++)
      Serial.write(myTransfer.rxBuff[i]);
    Serial.println();
  }
  else if(myTransfer.status < 0)
  {
    Serial.print("ERROR: ");
    Serial.println(myTransfer.status);
  }
}

how that to apply on this code here