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?