Nodemcu does not receive the data as I send via UDP Protocol

Asked

Viewed 66 times

1

I am 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 Nodemcu.

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

To receive packages via UDP on the Raspberry side, the Python language was used, with its standard socket library. And for Nodemcu, used the library Wifiudp. h and Esp8266wifi.h Nodemcu standards ...

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

Python Code on 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")

Code C in 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:

inserir a descrição da imagem aqui

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

No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.