Communication over TCP between two ESP8266
Posted: Wed Mar 09, 2016 12:56 pm
Hello,
I am doing a project. For the project I made some kind of encrypted communication between two ESP8266; very lightweight, using the XOR method. However, I have a problem with the communication over TCP between the two ESP's. Most times from the beginning everything works. However, after the 3rd check the server responds that there is no client connected. And overall can you look at the following codes and say if I am doing something wrong with the TCP communication because with UDP everything worked right from the beginning smoothly.
Server Code:
Client ESP:
Thank you in advance!!!
I am doing a project. For the project I made some kind of encrypted communication between two ESP8266; very lightweight, using the XOR method. However, I have a problem with the communication over TCP between the two ESP's. Most times from the beginning everything works. However, after the 3rd check the server responds that there is no client connected. And overall can you look at the following codes and say if I am doing something wrong with the TCP communication because with UDP everything worked right from the beginning smoothly.
Server Code:
Code: Select all
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <WiFiUdp.h>
const char *ssid = "SumoRobot";
const char *password = "ZumoShield";
WiFiUDP communication;
int numClients;
WiFiServer server(100);
void setup() {
Serial.begin(9600);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED,LOW);
Wire.begin();
WiFi.softAP(ssid, password);
randomSeed(analogRead(0));
server.begin();
}
char buff[UDP_TX_PACKET_MAX_SIZE];
int state=HIGH,timecnt,timeout;
String xorStrAuth(String input,int xorNum)
{
for(int i=0;i<input.length();i++)
{
input[i]^=xorNum;
}
return input;
}
long hash(String input,long mod)
{
long res=0;
for(int i=0;i<input.length();i++)
{
res*=256;
res+=input[i];
res%=mod;
}
return res;
}
long long decryptHash,UDPPort;
boolean error=false;
String decrypt(String input)
{
for(int i=0;i<input.length();i++)
{
input[i]^=decryptHash;
}
return input;
}
void loop() {
if(numClients==0)
{
Serial.println("INIT");
//blinking
timecnt+=1;
if(state==HIGH&&timecnt==10)
{
digitalWrite(BUILTIN_LED,LOW);
state=LOW;
timecnt=0;
}
if(state==LOW&&timecnt==2)
{
digitalWrite(BUILTIN_LED,HIGH);
state=HIGH;
timecnt=0;
}
delay(100);
WiFiClient accell=server.available();
if(accell.connected())
{
int xorNum1=0;
int cnt=0;
while(cnt<100&&!accell.available())
{
cnt++;
delay(100);
}
if(cnt==100)
{
error=true;
return;
}
String helper=accell.readStringUntil('\r');
accell.flush();
xorNum1=helper.toInt();
Serial.print("xorNum1: ");
Serial.println(xorNum1);
String genHash;
for(int i=0;i<10;i++)
{
genHash+=random(0,256);
}
Serial.print("genHash: ");
Serial.println(genHash);
accell.println(genHash);
cnt=0;
while(cnt<100&&!accell.available())
{
cnt++;
delay(100);
}
if(cnt==100)
{
error=true;
return;
}
int xorNum2=0;
helper=accell.readStringUntil('\r');
accell.flush();
xorNum2=helper.toInt();
Serial.print("xorNum2: ");
Serial.println(xorNum2);
String portString;
for(int i=0;i<10;i++)
{
portString+=random(0,256);
}
Serial.print("portString: ");
Serial.println(portString);
accell.println(portString);
delay(100);
accell.stop();
genHash=xorStrAuth(genHash,xorNum1);
decryptHash=hash(genHash,100000);
portString=xorStrAuth(portString,xorNum2);
UDPPort=hash(portString,60000);
communication.begin(UDPPort);
error=false;
numClients++;
}
return;
}
if(error)
{
numClients=0;
return;
}
WiFiClient check=server.available();
if(check.connected())
{
int cnt=0;
while(cnt<100&&!check.available())
{
cnt++;
delay(100);
}
if(cnt==100)
{
Serial.println("No data received!!!");
error=true;
return;
}
String mess=check.readStringUntil('\r');
check.flush();
if(mess=="8888")
{
timeout=0;
check.println("9999");
}
}
else
{
timeout++;
if(timeout>10500)
{
Serial.println("Not there!!!");
numClients=0;
error=true;
timeout=0;
}
}
check.stop();
if(state==HIGH)
{
digitalWrite(BUILTIN_LED,LOW);
state=LOW;
}
int length=communication.parsePacket();
if(length>0)
{
Serial.println("READER");
communication.read(buff,length);
String message=String(buff);
Serial.println(message);
message=decrypt(message);
Serial.print("Commands: ");
Serial.println(message);
Wire.beginTransmission(7);
Wire.write(message.c_str());
Wire.endTransmission();
delay(10); //delay needed in order the Sumo Robot to respond
}
}
Client ESP:
Code: Select all
#include <ESP8266WiFi.h>
#include<Wire.h>
#include<WiFiUdp.h>
//#include<WiFi.h>
const int MPU_addr=0x68;
int16_t AcX,AcY,AcZ,Tmp,GyX,GyY,GyZ;
const char* ssid = "SumoRobot";
const char* password = "ZumoShield";
const char* host = "192.168.4.1";
const int buttonPowerPin=13;
const int buttonGroundPin=14;
int lastButtonState=LOW;
bool gestureControl=true;
WiFiUDP communication;
void setup() {
Serial.begin(9600);
pinMode(buttonPowerPin,OUTPUT);
digitalWrite(buttonPowerPin,HIGH);
pinMode(buttonGroundPin,INPUT);
pinMode(BUILTIN_LED, OUTPUT);
digitalWrite(BUILTIN_LED,LOW);
Wire.begin();
Wire.beginTransmission(MPU_addr);
Wire.write(0x6B);
Wire.write(0);
Wire.endTransmission(true);
delay(10);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
randomSeed(analogRead(0));
}
String message="";
long counterAccel=0,counterSend=0,timeout=0;
int state=HIGH,timecnt;
boolean connected,error=false;
long encryptHash,UDPPort;
String xorStrAuth(String input,int xorNum)
{
for(int i=0;i<input.length();i++)
{
input[i]^=xorNum;
}
return input;
}
long hash(String input,long mod)
{
long res=0;
for(int i=0;i<input.length();i++)
{
res*=256;
res+=input[i];
res%=mod;
}
return res;
}
void associate()
{
WiFiClient robot;
int cnt=0;
while(cnt<10&&!robot.connect(host,100))
{
cnt++;
delay(100);
}
if(cnt==10)
{
error=true;
return;
}
else
{
int xorNum1=random(0,256);
robot.println(xorNum1);
Serial.print("xorNum1: ");
Serial.println(xorNum1);
int cnt=0;
while(cnt<100&&!robot.available())
{
cnt++;
delay(100);
}
if(cnt==100)
{
error=true;
return;
}
String genHash=robot.readStringUntil('\r');
robot.flush();
Serial.print("genHash: ");
Serial.println(genHash);
int xorNum2=random(0,256);
robot.println(xorNum2);
Serial.print("xorNum2: ");
Serial.println(xorNum2);
cnt=0;
while(cnt<100&&!robot.available())
{
cnt++;
delay(100);
}
if(cnt==100)
{
error=true;
return;
}
String portString=robot.readStringUntil('\r');
robot.flush();
Serial.print("portString: ");
Serial.println(portString);
delay(100);
robot.stop();
genHash=xorStrAuth(genHash,xorNum1);
encryptHash=hash(genHash,100000);
portString=xorStrAuth(portString,xorNum2);
UDPPort=hash(portString,60000);
error=false;
}
}
String encrypt(String input)
{
for(int i=0;i<input.length();i++)
{
input[i]^=encryptHash;
}
return input;
}
void loop() {
if(WiFi.status()!=WL_CONNECTED)
{
if(connected)
{
digitalWrite(BUILTIN_LED,HIGH);
state=HIGH;
}
connected=false;
//blinking
timecnt+=1;
if(state==HIGH&&timecnt>=10)
{
digitalWrite(BUILTIN_LED,LOW);
state=LOW;
timecnt=0;
WiFi.begin(ssid,password);
}
if(state==LOW&&timecnt>=2)
{
digitalWrite(BUILTIN_LED,HIGH);
state=HIGH;
timecnt=0;
}
delay(100);
return;
}
if(!connected||error)
{
Serial.print("Connected and Error:");
Serial.print(connected);
Serial.println(error);
connected=true;
error=false;
delay(500);
associate();
digitalWrite(BUILTIN_LED,LOW);
state=LOW;
}
int currentButtonState=digitalRead(buttonGroundPin);
if(currentButtonState!=lastButtonState)
{
if(currentButtonState==LOW)
{
gestureControl=!gestureControl;
}
lastButtonState=currentButtonState;
}
timeout++;
if(timeout==10000)
{
WiFiClient check;
int cnt=0;
while(cnt<10&&!check.connect(host,100))
{
cnt++;
delay(100);
}
if(cnt==10)
{
error=true;
return;
}
else
{
check.println("8888");
Serial.println("Check Sent!!");
int cnt=0;
while(cnt<100&&!check.available())
{
cnt++;
delay(100);
}
if(cnt==100)
{
error=true;
return;
}
String ans=check.readStringUntil('\r');
check.flush();
Serial.println(ans);
if(ans=="9999")
{
timeout=0;
error=false;
}
else
{
Serial.println("Failed Check!!!");
error=true;
}
}
check.stop();
timeout=0;
}
if(!gestureControl||error)
{
return;
}
counterSend++;
counterAccel++;
if(counterAccel==500)
{
Wire.beginTransmission(MPU_addr);
Wire.write(0x3B);
Wire.endTransmission(false);
Wire.requestFrom(MPU_addr,14,true);
AcX=Wire.read()<<8|Wire.read();
AcY=Wire.read()<<8|Wire.read();
AcZ=Wire.read()<<8|Wire.read();
Tmp=Wire.read()<<8|Wire.read();
GyX=Wire.read()<<8|Wire.read();
GyY=Wire.read()<<8|Wire.read();
GyZ=Wire.read()<<8|Wire.read();
//Applying a formula to turn raw values into Yaw, Pitch and Roll
int16_t roll=atan2(AcY,AcZ)*180/PI;
int16_t pitch=atan2(AcX,sqrt(AcY*AcY+AcZ*AcZ))*180/PI;
if(roll>25&&pitch>-30&&pitch<30)
{
message+='F';
}
if(roll<-38&&pitch>-30&&pitch<30)
{
message+='B';
}
if(pitch>35)
{
message+='R';
}
if(pitch<-35)
{
message+='L';
}
counterAccel=0;
}
if(counterSend==1000)
{
Serial.println("SENDER");
if(message.length()>0)
{
communication.beginPacket(host,UDPPort);
message=encrypt(message);
Serial.println(message);
communication.write(message.c_str(),message.length());
communication.endPacket();
message="";
}
counterSend=0;
}
}
Thank you in advance!!!