NodeMCU does not receive data as I send via UDP Protocol

1

I'm developing a simple application, the goal is to simply send the reading of a sensor connected to an Arduino NodeMCU to a Raspberry PI, and perform a similar reading using Raspberry and send to the NodeMCU.

Basically, both are inside the same network, and they exchange information from their sensors.

To receive packets via UDP on the raspberry side, the Python language was used with its standard socket library. And for the NodeMCU, used the WiFiUDP.h library and ESP8266WiFi.h standards of the NodeMCU ...

The problem here is that I send and receive data normally in Raspberry, however, and when I receive a data via UDP in the NodeMCU sent by Raspberry, some strange characters occur.

Python code in Raspberry PI

import sys
from ConnetionUDP import ConnetionUDP
from LDR import LDR
from LED import LED
from USC import UniversalCoverter
from OrderScale import OrderScale
import socket


address = "192.168.43.107"

myled = LED()

myldr = LDR()

rbscale = UniversalCoverter(100,10,OrderScale.DESCENDING)

nmcuscale = UniversalCoverter(0,100, OrderScale.ASCENDING)


sock = socket.socket(socket.AF_INET,  socket.SOCK_DGRAM)
sock.bind((address, 555))

while True:

    print('Waiting to receive anything ...')
    data, addr = sock.recvfrom(1024)

    #data, addr = conn.ReciveData()

    if data.decode() != '':
        print ('Raw data ::: ', data)
        nodeldrvalue = nmcuscale.GetValueUniversalScale(float(data.decode()))
        print ('NodeMCU Value ::: ',  nodeldrvalue, '\n')

    raspberryldrvalue = nmcuscale.GetValueUniversalScale(myldr.GetLDRCount())

    print ('Rasperry Value ::: ',  raspberryldrvalue, '\n')

    if (abs( nodeldrvalue - raspberryldrvalue ) < 4):
        sock.sendto('2'.encode(), addr)
        #conn.SendData(2, addr)
        myled.sendsignalled("blink")

    elif (nodeldrvalue > raspberryldrvalue):
        sock.sendto('0'.encode(), addr)
        #conn.SendData(0, addr)
        myled.sendsignalled("off")

    elif (nodeldrvalue < raspberryldrvalue):
        sock.sendto('1'.encode(), addr)
        #conn.SendData(1, addr)
        myled.sendsignalled("on")

C code on NodeMCU

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>

const char* ssid     = "AP";
const char* password = "10203040";     
int port = 555;
char * ip = "192.168.43.107";

WiFiUDP udp;
char * data;

int sensorPin = A0;

int myvalue = 0;
int readedValue = 0;

int wifiStatus;

void setup() {

    pinMode(D4, OUTPUT);

    // 1 = Disable, 0 = Enable
    digitalWrite(D4, 1);

    Serial.begin(115200);
    delay(200);

    // We start by connecting to a WiFi network
    Serial.println();
    Serial.println();
    Serial.print("Your are connecting to; ");
    Serial.println(ssid);

    WiFi.mode(WIFI_STA);
    udp.begin(port);
}   

void loop() {

    do_sensor();
    do_connect();
    do_send();
    do_listen();

    Serial.println("");
    Serial.print("My value: ");
    Serial.println(myvalue);
    Serial.print("Status value: ");
    Serial.println(readedValue);

    if (readedValue == 0) {
      // desliga
      digitalWrite(D4, HIGH);
      delay(2000);
    }
    else if (readedValue == 1) {
      // liga
      digitalWrite(D4, LOW);
      delay(2000);
    }
    else if (readedValue == 2) {
      // pisca
      digitalWrite(D4, LOW);
      delay(250);
      digitalWrite(D4, HIGH);
      delay(250);
      digitalWrite(D4, LOW);
      delay(250);
      digitalWrite(D4, HIGH);
      delay(250);
      digitalWrite(D4, LOW);
      delay(250);
      digitalWrite(D4, HIGH);
      delay(250);
      digitalWrite(D4, LOW);
      delay(250);
      digitalWrite(D4, HIGH);
      delay(250);
      digitalWrite(D4, LOW);
      delay(250);
      digitalWrite(D4, HIGH);
    }
}

void do_sensor() {

    myvalue = analogRead(sensorPin); 

}

void do_connect() {

    wifiStatus = WiFi.status();

    if(wifiStatus == WL_CONNECTED) {

        Serial.println("");
        Serial.println("Your ESP is connected!");  
        Serial.println("Your IP address is: ");
        Serial.println(WiFi.localIP());  

        delay(100);
    }
    else {
        Serial.println("");
        Serial.println("WiFi not connected, trying again ...");
        WiFi.begin(ssid, password);
        delay(1000);
    }

    delay(1000);
}

void do_send() {

    if (wifiStatus == WL_CONNECTED) {

        udp.beginPacket(ip, port);
        udp.println(myvalue);
        udp.endPacket();

        Serial.print("Sending: ");
        Serial.println(myvalue);

        delay(5);
    }
    else {
        Serial.println("Connection failed");
        delay(150);
    }

}

void do_listen() {

    if (udp.parsePacket() > 0)
    {
        data = "";

        while (udp.available() > 0)
        {
            char z = udp.read();
            data += z;
        }

        Serial.println("");
        Serial.print("Received data: ");
        Serial.println(data);

        readedValue = atoi(data);
    }
}

Print Screen of what NodeMCU is receiving:

Raspberry sends only 3 possible values: 0, 1, or 2. Each represents an action for the NodeMCU, the problem is that the NodeMCU receives strange characters like: vd , % s , aso ... (seemingly random characters)

    
asked by anonymous 10.06.2018 / 01:44

0 answers