2

I am having small project where I want to read couple of analog inputs from Arduino UNO, send it via software serial to ESP8266-01 and from that to python server using TCP.

Data is sent to ESP using this code on Arduino UNO

#include <SoftwareSerial.h>
 SoftwareSerial mySerial(10, 11); // RX, TX
 int pin = 5;
 void setup() {
 mySerial.begin(9600);
 delay(20000);
 }
 void loop() {
 delay(1000);
 int data = analogRead(pin);
 mySerial.println(data);
 }

Code on ESP is bellow

void loop() {
 while(Serial.available()>0){
 char data = Serial.read();
 client.print(data);
 }
}

When data is sent to Python server, it prints it out but data is sent in two seperate parts, so it always looks like this

9
59

, but number sent from arduino is just 959!

Python server code snipet:

while True:
 #Receiving from client
 data = conn.recv(1024)
 print data
 #send data to database
 post.create(
 current = str(data)
 )
 if not data:
 break

So the question is, why data sent to server is separated in two parts? Maybe it have to do something with how data is reading from serial on esp?

asked May 14, 2017 at 17:13
3
  • Suspect the problem to be with the python code. We'd need a complete minimal verifiable example of it. Commented May 14, 2017 at 18:00
  • Python server code can be found in link Commented May 14, 2017 at 18:18
  • read() in a loop to a buffer, then print() all in once go; the extra read delay might be hampering you Commented May 15, 2017 at 13:57

1 Answer 1

0

You assume all 5 bytes of 959\r\n are received by the server at the same time. The data is sent byte after byte, based on the client.print(data) in the ESP code and assuming the underlying library doesn't buffer bytes before transmitting, so there's no way they'll arrive at the server simultaneously. Most likely, your first pass with conn.recv(1024) grabs the first byte 9 and times out. And then on the second pass, the others (at least 2) arrive and are read. The newline between the packets is typical Python 2 print behaviour.

What you need to do is parse the data at the server. Basically read data till you get CRLF i.e. \r\n, which is a good enough delimiter and you're sending it already anyways, and then use that to know when to write to your database.

answered May 14, 2017 at 21:05

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.