Last active
August 29, 2015 14:12
-
-
Save tprochazka/eb53d4274735161e4ef1 to your computer and use it in GitHub Desktop.
This file contains 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
/* | |
* Example of use ESP8266 library | |
* https://github.com/Diaoul/arduino-ESP8266 | |
* | |
* Created: 12/30/2014 7:29:04 PM | |
* Author: Tomáš Procházka | |
*/ | |
#include <SoftwareSerial.h> | |
//#include <AltSoftSerial.h> | |
#include "ESP8266.h" | |
const char* WIFI_SSID = "ChangeThis!"; | |
const char* WIFI_PASSWORD = "ChangeThis!"; | |
const unsigned int STATUS_CHECK_INTERVAL_IN_S = 10; | |
//AltSoftSerial esp8266Serial; | |
SoftwareSerial esp8266Serial = SoftwareSerial(8, 9); | |
ESP8266 wifi = ESP8266(esp8266Serial); | |
void setup() { | |
Serial.begin(115200); | |
// ESP8266 | |
delay(2000); | |
esp8266Serial.begin(9600); | |
Serial.println("Start"); | |
wifi.setTimeout(100); | |
if (wifi.begin()) { | |
connect(); | |
wifi.setMode(ESP8266_WIFI_BOTH); | |
wifi.setMultipleConnections(true); | |
wifi.createServer(80); | |
sendGetIP(); | |
} else { | |
Serial.println("Init ESP8266 module failed"); | |
} | |
} | |
void loop() { | |
wifiRun(); | |
} | |
// will print public IP to Serial | |
void sendGetIP() { | |
Serial.print("connect: "); | |
Serial.println(getStatus(wifi.connect(1, ESP8266_PROTOCOL_TCP, IPAddress(89,29,94,130), 80))); | |
Serial.print("send: "); | |
Serial.println(getStatus(wifi.send(1, "GET /ip.php HTTP/1.0\r\nHost: www.sms.cz\r\n\r\n"))); | |
//Serial.print("close: "); | |
//Serial.println(getStatus(wifi.close(1))); | |
} | |
// doesn't work | |
void sendGetTime(void* context) { | |
Serial.print("connect2: "); | |
Serial.println(getStatus(wifi.connect(2, ESP8266_PROTOCOL_TCP, IPAddress(54,235,185,235), 80))); | |
Serial.print("send2: "); | |
Serial.println(getStatus(wifi.send(2, "GET /utc/now HTTP/1.0\r\nHost: www.timeapi.org\r\n\r\n"))); | |
//Serial.print("close: "); | |
//Serial.println(getStatus(wifi.close(1))); | |
} | |
void wifiRun() { | |
//static int c; | |
int size = wifi.available(); | |
if (size > 0) { | |
Serial.println("--------------------------------------"); | |
Serial.print(size); | |
Serial.print(" > "); | |
Serial.print(wifi.getId()); | |
Serial.println(" > "); | |
//Serial.println(wifi.readString()); | |
char* buffer = new char[size+1]; | |
buffer[size] = 0; | |
wifi.read(buffer, size); | |
Serial.println(buffer); | |
delete[] buffer; | |
checkConnectedStations(); | |
} | |
} | |
void connect() { | |
ESP8266CommandStatus cr; | |
do { | |
Serial.print("Connecting to AP... "); | |
cr = wifi.joinAP(WIFI_SSID, WIFI_PASSWORD); | |
Serial.println(getStatus(cr)); | |
if (cr != ESP8266_COMMAND_OK) { | |
delay(2000); | |
} else { | |
IPAddress adress; | |
do { | |
cr = wifi.getIP(ESP8266_WIFI_STATION, adress); | |
Serial.print("Obtained IP adress: "); | |
Serial.println(adress); | |
wifi.getIP(ESP8266_WIFI_ACCESSPOINT, adress); | |
Serial.print("AP IP adress: "); | |
Serial.println(adress); | |
} while (cr != ESP8266_COMMAND_OK); | |
} | |
} while (cr != ESP8266_COMMAND_OK); | |
} | |
void checkConnectionStatus() { | |
static ESP8266Connection* connection; | |
static unsigned int count; | |
static ESP8266ConnectionStatus status; | |
wifi.getConnectionStatus(status, connection, count); | |
Serial.print("Status: "); | |
Serial.println(status); | |
} | |
void checkConnectedStations() { | |
static ESP8266Station* stations; | |
static unsigned int count; | |
wifi.getConnectedStations(stations, count, 5); | |
Serial.print("Connected clients to AP: "); | |
Serial.println(count); | |
} | |
String getStatus(ESP8266CommandStatus status) { | |
switch (status) { | |
case ESP8266_COMMAND_INVALID: | |
return "INVALID"; | |
break; | |
case ESP8266_COMMAND_TIMEOUT: | |
return "TIMEOUT"; | |
break; | |
case ESP8266_COMMAND_OK: | |
return "OK"; | |
break; | |
case ESP8266_COMMAND_NO_CHANGE: | |
return "NO CHANGE"; | |
break; | |
case ESP8266_COMMAND_ERROR: | |
return "ERROR"; | |
break; | |
case ESP8266_COMMAND_NO_LINK: | |
return "NO LINK"; | |
break; | |
case ESP8266_COMMAND_TOO_LONG: | |
return "TOO LONG"; | |
break; | |
case ESP8266_COMMAND_FAIL: | |
return "FAIL"; | |
break; | |
default: | |
return "UNKNOWN COMMAND STATUS"; | |
break; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment