Link two ESP8266 modules connected to Teensy3.2
Posted: Fri Apr 01, 2016 12:42 pm
Hi, I want to send data between two Teensy3.2 over Wifi (using a router) using ESP8266. I am testing the following code but it is not working, can you please suggest me what is wrong here?
Server
Client Side
Server
Code: Select all
#include <ESP8266_TCP.h>
// ESP8266 Class
ESP8266_TCP wifi;
// Target Access Point
#define ssid "AirCard-3D52"
#define pass "52836427"
// Connect this pin to CH_PD pin on ESP8266
#define PIN_RESET 8
void setup()
{
delay(3000);
// We use Serial1 to interface with ESP8266
// and use Serial to debugging
Serial.begin(9600);
Serial1.begin(115200);
wifi.begin(&Serial1, &Serial, PIN_RESET);
// Check that ESP8266 is available
if(wifi.test())
{
// Connect to target Access Point
String ip = connectAP();
// Open TCP Server on port 2000 and 30 seconds for connection timeout (Max 2880)
wifi.openTCPServer(2000, 30);
}
else
{
// ESP8266 isn't available
Serial.println("Check module connection and restart to try again...");
while(true);
}
}
void loop()
{
// Check for any data has coming to ESP8266
int dataState = wifi.isNewDataComing(WIFI_SERVER);
if(dataState != WIFI_NEW_NONE) {
if(dataState == WIFI_NEW_CONNECTED) {
// Connected with TCP Client Side
Serial.println("Status : Connected");
} else if(dataState == WIFI_NEW_DISCONNECTED) {
// Disconnected from TCP Client Side
Serial.println("Status : Disconnected");
} else if(dataState == WIFI_NEW_MESSAGE) {
// Got a message from TCP Client Side
digitalWrite(13,HIGH);
Serial.println("ID : " + String(wifi.getId()));
Serial.println("Message : " + wifi.getMessage());
wifi.closeTCPConnection(0);
} else if(dataState == WIFI_NEW_SEND_OK) {
// Message transfer has successful
Serial.println("SENT!!!!");
}
}
}
// Access Point Connection Function that you can loop connect to Access Point until successful
String connectAP()
{
String ip = "0.0.0.0";
while(ip.equals("0.0.0.0"))
{
ip = wifi.connectAccessPoint(ssid, pass);
if(!ip.equals("0.0.0.0"))
{
break;
}
}
return ip;
}
Client Side
Code: Select all
#include <ESP8266_TCP.h>
// ESP8266 Class
ESP8266_TCP wifi;
// Target Access Point
#define ssid "AirCard-3D52"
#define pass "52836427"
// TCP Server IP and port
#define serverIP "192.168.1.3"
#define serverPort 2000
// Connect this pin to CH_PD pin on ESP8266
#define PIN_RESET 6
// Pin that connected to button to send any message
#define PIN_SEND 22
void setup()
{
delay(3000);
// Set pin for send command to input mode
pinMode(PIN_SEND, INPUT);
// We use Serial1 to interface with ESP8266
// and use Serial to debugging
Serial.begin(9600);
Serial1.begin(115200);
wifi.begin(&Serial1, &Serial, PIN_RESET);
// Check that ESP8266 is available
if(wifi.test())
{
// Connect to target Access Point
String ip = connectAP();
delay(1000);
}
else
{
// ESP8266 isn't available
Serial.println("Check module connection and restart to try again...");
while(true);
}
}
void loop()
{
// Check for any data has coming to ESP8266
int dataState = wifi.isNewDataComing(WIFI_CLIENT);
if(dataState != WIFI_NEW_NONE)
{
Serial.println("Inside If");
Serial.println(dataState);
if(dataState == WIFI_NEW_CONNECTED) {
// Connected with TCP Server Side
// Send a message to TCP Server Side
Serial.println("Sending");
wifi.send("12345678");
} else if(dataState == WIFI_NEW_DISCONNECTED) {
// Disconnected from TCP Server Side
Serial.println("Disconnected");
} else if(dataState == WIFI_NEW_SEND_OK) {
// Message transfer has successfully
Serial.println("SENT!!!!");
} else if(dataState == WIFI_NEW_ALREADY_CONNECT) {
// Already connected with TCP Server Side
Serial.println("Already Connect!!");
}
}
// When button for connect to TCP Server Side was pressed
//if(digitalRead(PIN_SEND)>500) {
// Send message to TCP Server Side and waiting for 1 sec
Serial.println("Connect!!");
wifi.connectTCP(serverIP, serverPort);
delay(1000);
//}
delay(50);
}
// Access Point Connection Function that you can loop connect to Access Point until successful
String connectAP()
{
String ip = "0.0.0.0";
while(ip.equals("0.0.0.0"))
{
ip = wifi.connectAccessPoint(ssid, pass);
if(!ip.equals("0.0.0.0"))
{
break;
}
}
return ip;
}