WiFi and the Raspberry Pi

Installation of the IDE

With the new 1.6.x release of the Arduino IDE, it is much simpler to add and manage new boards. In the case of the Adafruit Huzzah ESP8266, it is a simple process. Rather than repeat the instructions here, I suggest following the Adafruit tutorial [13].

Adafruit produces a wonderful set of tutorials for most of the products for which they manufacture and sell boards; however, they can become outdated – links and libraries change, and the hardware changes. Except for that, these tutorials are gold for beginners or those who just want to get things done quickly.

Web Server Code

Listing 5 is the Arduino version of the simple web server: 71 lines of code. Not bad. This web server code is included with the Arduino IDE under File | Examples | ESP8266WebServer.

Listing 5

Arduino Web Server

01 #include <ESP8266WiFi.h>
02 #include <WiFiClient.h>
03 #include <ESP8266WebServer.h>
04 #include <ESP8266mDNS.h>
05
06 const char* ssid = "........";
07 const char* password = "........";
08 MDNSResponder mdns;
09
10 ESP8266WebServer server(80);
11
12 const int led = 13;
13
14 void handleRoot() {
15   digitalWrite(led, 1);
16   server.send(200, "text/plain", "hello from esp8266!");
17   digitalWrite(led, 0);
18 }
19
20 void handleNotFound(){
21   digitalWrite(led, 1);
22   String message = "File Not Found\n\n";
23   message += "URI: ";
24   message += server.uri();
25   message += "\nMethod: ";
26   message += (server.method() == HTTP_GET)?"GET":"POST";
27   message += "\nArguments: ";
28   message += server.args();
29   message += "\n";
30   for (uint8_t i=0; i<server.args(); i++){
31     message += " " + server.argName(i) + ": " + server.arg(i) + "\n";
32   }
33   server.send(404, "text/plain", message);
34   digitalWrite(led, 0);
35 }
36
37 void setup(void){
38   pinMode(led, OUTPUT);
39   digitalWrite(led, 0);
40   Serial.begin(115200);
41   Serial.println("ESP8266 Webserver Start");
42   WiFi.begin(ssid, password);
43   Serial.println("");
44
45   // Wait for connection
46   while (WiFi.status() != WL_CONNECTED) {
47     delay(500);
48     Serial.print(".");
49   }
50   Serial.println("");
51   Serial.print("Connected to ");
52   Serial.println(ssid);
53   Serial.print("IP address: ");
54   Serial.println(WiFi.localIP());
55
56   if (mdns.begin("esp8266", WiFi.localIP())) {
57     Serial.println("MDNS responder started");
58   }
59
60   server.on("/", handleRoot);
61
62   server.on("/inline", [](){
63     server.send(200, "text/plain", "this works as well");
64   });
65
66   server.onNotFound(handleNotFound);
67
68   server.begin();
69   Serial.println("HTTP server started");
70 }
71
72 void loop(void){
73   server.handleClient();
74 }

I added the SSID and the password of my local WiFi network; then, I compiled and loaded it into the ESP8266, and it worked. That simple:

............
Connected to xxxxxx IP address:
192.168.1.127 MDNS responder
started HTTP server started

The resulting Chrome browser screen is shown in Figure 7.

Figure 7: Arduino IDE web server in the Chrome browser.

Buy this article as PDF

Express-Checkout as PDF
Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

SINGLE ISSUES
 
SUBSCRIPTIONS
 
TABLET & SMARTPHONE APPS
Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content