From 052b39cd79cda945bc8d231445210f04b07c993e Mon Sep 17 00:00:00 2001 From: Ivan Grokhotkov Date: Tue, 23 Dec 2014 15:39:48 +0300 Subject: [PATCH] Add samples for the ESP8266WiFi library --- NOTES | 8 +- .../examples/WiFiClient/WiFiClient.ino | 85 ++++++++++++++++ .../examples/WiFiScan/WiFiScan.ino | 48 +++++++++ .../examples/WiFiWebServer/WiFiWebServer.ino | 99 +++++++++++++++++++ 4 files changed, 237 insertions(+), 3 deletions(-) create mode 100644 hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino create mode 100644 hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino create mode 100644 hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino diff --git a/NOTES b/NOTES index f8ce6c6d7..95a509e1f 100644 --- a/NOTES +++ b/NOTES @@ -58,6 +58,7 @@ What works * WiFi.RSSI() doesn't work * WiFi.printDiag(Serial); will print out some diagnostic info WiFiServer and WiFiClient behave mostly the same way as with WiFi shield library. + Three samples are provided for this library. - Other libraries that don't rely on low-level access to AVR registers. @@ -85,17 +86,18 @@ What is not done yet - Upload sketches via WiFi Conceptually and technically simple, but need to figure out how to provide the best UX for this feature. - + - Samples for all the libraries License and credits ------------------- -Arduino is licensed under GPL, with Arduino core libraries licensed under LGPL. +Arduino IDE is licensed under GPL, with Arduino core libraries licensed under LGPL. This build includes an xtensa gcc toolchain, which is also under GPL. Espressif SDK included in this build is under Espressif Public License. -Esptool by Christian Klippel is licensed under GPLv2. +Esptool written by Christian Klippel is licensed under GPLv2. +The source with my modfications is available on github: https://github.com/igrr/esptool-ck ESP8266 port contributed by Ivan Grokhotkov, igrokhotkov at gmail dot com. diff --git a/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino b/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino new file mode 100644 index 000000000..7ffc78acd --- /dev/null +++ b/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiClient/WiFiClient.ino @@ -0,0 +1,85 @@ +/* + * This sketch sends data via HTTP GET requests to data.sparkfun.com service. + * + * You need to get streamId and privateKey at data.sparkfun.com and paste them + * below. Or just customize this script to talk to other HTTP servers. + * + */ + +#include + +const char* ssid = "your-ssid"; +const char* password = "your-password"; + +const char* host = "data.sparkfun.com"; +const char* streamId = "...................."; +const char* privateKey = "...................."; + +void setup() { + Serial.begin(115200); + delay(10); + + // 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()); +} + +int value = 0; + +void loop() { + delay(5000); + ++value; + + Serial.print("connecting to "); + Serial.println(host); + + // Use WiFiClient class to create TCP connections + WiFiClient client; + const int httpPort = 80; + if (!client.connect(host, httpPort)) { + Serial.println("connection failed"); + return; + } + + // We now create a URI for the request + String url = "/input/"; + url += streamId; + url += "?private_key="; + url += privateKey; + url += "&value="; + url += value; + + Serial.print("Requesting URL: "); + Serial.println(url); + + // This will send the request to the server + client.print(String("GET ") + url + " HTTP/1.1\r\n" + + "Host: " + host + "\r\n" + + "Connection: close\r\n\r\n"); + delay(10); + + // Read all the lines of the reply from server and print them to Serial + while(client.available()){ + String line = client.readStringUntil('\r'); + Serial.print(line); + } + + Serial.println(); + Serial.println("closing connection"); +} + diff --git a/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino b/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino new file mode 100644 index 000000000..3e6ff7363 --- /dev/null +++ b/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiScan/WiFiScan.ino @@ -0,0 +1,48 @@ +/* + * This sketch demonstrates how to scan WiFi networks. + * The API is almost the same as with the WiFi Shield library, + * the most obvious difference being the different file you need to include: + */ +#include "ESP8266WiFi.h" + +void setup() { + Serial.begin(115200); + + // Set WiFi to station mode and disconnect from an AP if it was previously connected + WiFi.mode(WIFI_STA); + WiFi.disconnect(); + delay(100); + + Serial.println("Setup done"); +} + +void loop() { + Serial.println("scan start"); + + // WiFi.scanNetworks will return the number of networks found + int n = WiFi.scanNetworks(); + Serial.println("scan done"); + if (n == 0) + Serial.println("no networks found"); + else + { + Serial.print(n); + Serial.println(" networks found"); + for (int i = 0; i < n; ++i) + { + // Print SSID and RSSI for each network found + Serial.print(i + 1); + Serial.print(": "); + Serial.print(WiFi.SSID(i)); + Serial.print(" ("); + Serial.print(WiFi.RSSI(i)); + Serial.print(")"); + Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE)?" ":"*"); + delay(10); + } + } + Serial.println(""); + + // Wait a bit before scanning again + delay(5000); +} diff --git a/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino b/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino new file mode 100644 index 000000000..ed1b64fc9 --- /dev/null +++ b/hardware/arduino/esp8266/libraries/ESP8266WiFi/examples/WiFiWebServer/WiFiWebServer.ino @@ -0,0 +1,99 @@ +/* + * This sketch demonstrates how to set up a simple HTTP-like server. + * The server will set a GPIO pin depending on the request + * http://server_ip/gpio/0 will set the GPIO2 low, + * http://server_ip/gpio/1 will set the GPIO2 high + * server_ip is the IP address of the ESP8266 module, will be + * printed to Serial when the module is connected. + */ + +#include + +const char* ssid = "your-ssid"; +const char* password = "your-password"; + +// Create an instance of the server +// specify the port to listen on (first argument) +// and the receive buffer size (second argument) +WiFiServer server(80, 2048); + +void setup() { + Serial.begin(115200); + delay(10); + + // prepare GPIO2 + pinMode(2, OUTPUT); + digitalWrite(2, 0); + + // Connect to 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"); + + // Start the server + server.begin(); + Serial.println("Server started"); + + // Print the IP address + Serial.println(WiFi.localIP()); +} + +void loop() { + // Check if a client has connected + WiFiClient client = server.available(); + if (!client) { + return; + } + + // Wait until the client sends some data + Serial.println("new client"); + while(!client.available()){ + delay(1); + } + + // Read the first line of the request + String req = client.readStringUntil('\r'); + Serial.println(req); + client.flush(); + + // Match the request + int val; + if (req.indexOf("/gpio/0") != -1) + val = 0; + else if (req.indexOf("/gpio/1") != -1) + val = 1; + else { + Serial.println("invalid request"); + client.stop(); + return; + } + + // Set GPIO2 according to the request + digitalWrite(2, val); + + client.flush(); + + // Prepare the response + String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n\r\n\r\nGPIO is now "; + s += (val)?"high":"low"; + s += "\n"; + + // Send the response to the client + client.print(s); + delay(1); + Serial.println("Client disonnected"); + + // The client will actually be disconnected + // when the function returns and 'client' object is detroyed +} +