3

I am trying to send numeric data using ESP8266 to local network python server. Before that I am collecting data from sensors using Arduino UNO and then send it to ESP using hardware serial.

ESP8266 code:

#include <ESP8266WiFi.h>
const char* ssid = "ssid";
const char* password = "pass";
const char* host = "192.168.1.103";
String serial_data;
WiFiClient client;
void setup() {
 Serial.begin(115200);
 delay(5000);
 // We start by connecting to a WiFi network
 Serial.println();
 Serial.println();
 Serial.print("Connecting to ");
 Serial.println(ssid);
 WiFi.begin(ssid, password);
 while (WiFi.status() != WL_CONNECTED) {
 delay(500);
 Serial.print(".");
 }
 Serial.println("");
 Serial.println("WiFi connected"); 
 Serial.println("IP address: ");
 Serial.println(WiFi.localIP());
 delay(5000);
 Serial.print("connecting to ");
 Serial.println(host);
 // Use WiFiClient class to create TCP connections
 const int port = 8888;
 if (!client.connect(host, port)) 
 {
 Serial.println("connection failed");
 return;
 }
}
void loop() {
 delay(200);
 while(Serial.available() <= 0)
 {
 //wait for arduino serial data
 }
 if(Serial.available()>0)
 {
 // This will send data to client via TCP
 Serial.println("Sent..");
 serial_data = String(Serial.read());
 client.print(serial_data);
 }
}

Python Server code:

import socket
import sys
from thread import *
from peewee import *
db = SqliteDatabase("data.db")
class post(Model):
 timestamp = DateTimeField()
 current = TextField()
 class Meta:
 database = db
#connecting to database
def initialize_db():
 db.connect()
 db.create_tables([post], safe = True)
initialize_db()
HOST = '192.168.1.103' 
PORT = 8888 # Arbitrary non-privileged port
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
print 'Socket created'
try:
 s.bind((HOST, PORT))
except socket.error , msg:
 print 'Bind failed. Error Code : ' + str(msg[0]) + ' Message ' + msg[1]
 sys.exit()
print 'Socket bind complete'
s.listen(10)
print 'Socket now listening'
#keep talking with the client
#Function for handling connections. This will be used to create threads
def clientthread(conn):
 #Sending message to connected client
 conn.send('Welcome to the server. Type something and hit enter\n') #send only takes string
 #infinite loop so that function do not terminate and thread do not end.
 while True:
 #Receiving from client
 data = conn.recv(1024)
 print data
 #send data to database
 post.create(
 current = data
 )
 reply = 'OK ->' + data
 if not data:
 break
 conn.sendall(reply)
 #came out of loop
 conn.close()
#keep talking with the client
while 1:
 #wait to accept a connection - blocking call
 conn, addr = s.accept()
 print 'Connected with ' + addr[0] + ':' + str(addr[1])
 start_new_thread(clientthread ,(conn,))
s.close()

After powering up ESP it successfully connects to server and then simulating arduino esp serial communication I am using serial monitor to send some numbers. Before I send anything server outputs number 240:

Connected with 192.168.1.100:26137
240

Then I am sending number 1 but on server terminal I get numbers 49 and 13. Sending numbers some 2 - 3 times and no more data are outputted to server terminal. Should I parse data received from serial to some form I can transmit it via TCP or data should be processed on server side?

asked May 1, 2017 at 9:58

1 Answer 1

1

The numbers are ASCII Codes (here is a chart from the Arduino website). 49 is the number 1, and 13 is a carriage return ('\n'). There should be a setting in the bottom right corner of Serial Monitor to change line ending, this should be set to "no line ending." If it is, than client.print() must be adding the carriage return, so you will need to remove it in Python. If you are only sending integers, replace

serial_data = String(Serial.read());

with:

serial_data = String(Serial.parseInt());

In the Python code I suggest adding a print statement to check if the connection is closed after

#came out of loop
 conn.close()
answered May 1, 2017 at 22:33
2
  • Thank you. But can you also explain why after sending number 1 to server it prints 3 times these values 49 and 13 but after I can send anything and server receives nothing? Commented May 3, 2017 at 13:06
  • Is the connection being closed, or is it just not receiving anything? Commented May 3, 2017 at 19:17

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.