Created
October 14, 2014 12:17
-
-
Save vixtory09678/4bdeccde8296f4e79ce6 to your computer and use it in GitHub Desktop.
codeding
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266_TCP.h> | |
#include <SoftwareSerial.h> | |
// ESP8266 Class | |
ESP8266_TCP wifi; | |
SoftwareSerial mySerial(10,11); | |
int getTemp = A0; | |
// Target Access Point | |
#define ssid "OpenWrt_NAT3" | |
#define pass "welcometoopendream" | |
// TCP Server IP and port | |
#define serverIP "192.168.60.139" | |
#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 7 | |
void setup() | |
{ | |
delay(3000); | |
pinMode(getTemp,INPUT); | |
// Set pin for send command to input mode | |
pinMode(PIN_SEND, INPUT); | |
// We use Serial1 to interface with ESP8266 | |
// and use Serial to debugging | |
mySerial.begin(9600); | |
Serial.begin(115200); | |
wifi.begin(&Serial, &mySerial, PIN_RESET); | |
/* If your board has only 1 serial port | |
* or you didn't need to debugging, try this. | |
* | |
* Serial.begin(115200); | |
* wifi.begin(&Serial, PIN_RESET); | |
* | |
*/ | |
float TempC = (5.0*analogRead(getTemp)*100.0)/1024.0; | |
// Check that ESP8266 is available | |
if(wifi.test()) | |
{ | |
// Connect to target Access Point | |
String ip = connectAP(); | |
mySerial.println(ip); | |
} | |
else | |
{ | |
// Open TCP Server on port 2000 and 30 seconds for timeout | |
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) { | |
if(dataState == WIFI_NEW_CONNECTED) { | |
// Connected with TCP Server Side | |
Serial.println("Connected"); | |
} else if(dataState == WIFI_NEW_DISCONNECTED) { | |
// Disconnected from TCP Server Side | |
Serial.println("Disconnected"); | |
} else if(dataState == WIFI_NEW_MESSAGE) { | |
// Got a message from TCP Server Side | |
Serial.println("Message : " + wifi.getMessage()); | |
} else if(dataState == WIFI_NEW_SEND_OK) { | |
// Message transfer has successful | |
Serial.println("SENT!!!!"); | |
} | |
} | |
// if(wifi.getRunningState()==WIFI_STATE_CONNECT){ | |
//} | |
// Auto connect to TCP Server Side when connection timeout | |
if(wifi.getRunningState() == WIFI_STATE_UNAVAILABLE) { | |
// Connect to TCP Server Side | |
Serial.println("Connect!!"); | |
delay(500); | |
wifi.connectTCP(serverIP, serverPort); | |
delay(500); | |
}else{ | |
int TempC = (5.0*analogRead(getTemp)*100.0)/1024.0; | |
String dataIn = ""+TempC; | |
wifi.send(dataIn); | |
delay(500); | |
} | |
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")) | |
{ | |
String ip = wifi.connectAccessPoint(ssid, pass); | |
if(!ip.equals("0.0.0.0")) | |
{ | |
break; | |
} | |
} | |
return ip; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment