0

I am trying to send a serial data from NodeMCU to Arduino. I use MicroPython to program. As well as Serial.read on Arduino. I can send and receive successfully. But the problem is the NodeMCU sends data along with number which is not needed. And Arduino receives data along with number. For Example, if I send "Hello" it sends as "Hello5". I understood that the number is nothing but the number of alphabets in the string. How can I remove this?

MicroPython on NodeMCU:

import os
import machine
from machine import UART
uart = UART(0)
import time
while True:
    uart.write('1')

Arduino program:

String received;
String msg;
void setup() {
  Serial.begin(115200);
  attachInterrupt(0, light, FALLING);//When arduino Pin 2 is FALLING from   HIGH to LOW, run light procedure!
}

void light() {
  Serial.println(msg);
}

void loop()
{
   if (Serial.available() > 0){ 
    received = Serial.readStringUntil('\n');
    msg = received;
   }
}
gre_gor
  • 6,669
  • 9
  • 47
  • 52
nawas
  • 41
  • 1
  • 10

1 Answers1

0

I just checked the microPython's UART (http://docs.micropython.org/en/latest/wipy/library/machine.UART.html) and Arduino's Serial (https://www.arduino.cc/en/Reference/Serial ), and it seems you're missing one initialization line for UART. UART document states the default baud rate it sets is 9600, and you expect a 115200 on serial receiver. I believe setting the baud rate different on each side will have undefined behavior.

In your python code, could you try uart.init(115200) after uart = UART(0) call (and the default values for the rest seems same as the Serial's expectations on receiver)?

Also, Serial document says that if it can't find the char you define in the readStringUntil(), then it'll try until it times out. So I guess your function call times-out because it won't find an endline ('\n') in the stream, because you didn't inject any.

In addition, although the help documents of the functionality you're using don't state such a thing, if you really always get the number of characters as the first char at the receiver, it might be worthwhile to try using that to your advantage. I wonder if you can try to get that number first, then read that many chars afterwards (at the Arduino receiver site). Here's some code I hope may help (I'm afraid I didn't try using it):

#include <string.h>
char buffer[256];  // buffer to use while reading the Serial
memset(buffer, (char)0, 256);  // reset the buffer area to all zeros

void loop()
{
   if (Serial.available() > 0){ 
    int count = Serial.read();  // the first byte that shows the num of chars to read after, assuming that this is a 'byte' - which means we can have max 256 chars in the stream
    Serial.readBytes(buffer, count);
    msg = String(buffer);
   }
}
AlbusMPiroglu
  • 645
  • 3
  • 13
  • I tried your suggestion. When i use uart(1) it sends only number of alphabets in message. – nawas May 13 '17 at 04:42
  • Even after changing the baudrate the issue is not solved.. I you have have a code can you share it? – nawas May 13 '17 at 04:48