Control your Arduino and Pi projects with a PSP

Arduino Web Server

To create Arduino WiFi projects there are a number of different ESP8266 based modules [2] (Figure 5) and approaches that can be used. The standalone ESP8266 module can be wired into an Arduino Uno/Nano/Mega module or you can buy a board with an integrated ESP8266 chip. For our testing we used an older WeMo board. Some other integrated boards are: the NodeMCU, Adafruit HUZZAH and the Arduino Yun.

Figure 5: An ESP8266 standalone and some integrated modules.

To get started you will need to download the following libraries: ESP8266WiFi, WiFiClient and ESP8266WiFi.

The ESP8266WebServer library has a simple access point example. We modified the WifiAccessPoint example (Listing 2) to include our GO/STOP HTML form. We also included a go and a stop function. Custom functions are referenced by the server.on call. So for example if we wanted to include a backward motion, we would include an HTML form with a GET action of backward, and then in the Arduino code we would add a server.on("/backward",backward) line, and a void backward() function.

Listing 2


01 #include <ESP8266WiFi.h>
02 #include <WiFiClient.h>
03 #include <ESP8266WebServer.h>
05 /* Set these to your desired credentials. */
06 const char *ssid = "MY8266";
07 const char *password = "";
09 char *webpage  = "<html><head><title>My8266 Control</title> </head><body> <h1>Click a button to control the car</h1><hr> <form action='/go' method='GET' > <input type='submit' style='font-size:150px;color:lime' value='GO'></form><form action='/stop' method='GET'> <input type='submit' style='font-size:150px;color:red' value='STOP'></form> </body></html>";
11 ESP8266WebServer server(80);
13 void handleRoot() {
14   Serial.println("Base page");
15         server.send(200, "text/html", webpage);
16 }
18 void go() {
19   Serial.println("Go forward");
20   server.send(200, "text/html", webpage);
22 }
23 void stop() {
24   Serial.println("Stop");
25   server.send(200, "text/html", webpage);
27 void setup() {
28   delay(1000);
29   Serial.begin(115200);
30   Serial.print("Configuring access point...");
31   WiFi.softAP(ssid, password);
32   IPAddress myIP = WiFi.softAPIP();
33   Serial.print("AP IP address: ");
34   Serial.println(myIP);
35   server.on("/", handleRoot);
36   server.on("/go",go);
37   server.on("/stop",stop);
38   server.begin();
39   serial.println("HTTP server started");
40 }
42 void loop() {
43         server.handleClient();
44 }<C>

Once you restart the Arduino, an you will see an Access Point named MY8266 become available. By default the Arduino's web server address will be at

We enhanced the basic GO/STOP example to create an air boat (Figures 6 and 7). We built the frame with K'Nex and used some bottles for flotation. The three fans were controlled by digital output. We also added commands for turning left and right. You can find all the source available from the magazine website [5].

Figure 6: The Arduino airboat takes to the seas!

Pi Web Server

On the Raspberry Pi, there are many options for creating a web server. For our projects we wanted something that was simple and didn't require a lot of setup, so we looked at Python. By using Python we were able to create an "all-in-one" micro web server and project application.

To build a Python web server you need to import the HTTPServer library. You can remap the Python web server to another port, for example PORT_NUMBER = 8081 (where 8081 is a port number you choose) if your Pi is already running a web server on the standard ports.

The following statement will start the web server object:

server = HTTPServer(('', PORT_NUMBER), myHandler)

The myHandler class is used to read web page responses. GET requests are handled in the do_GET(self) function and the web form parameters are read in the self.path variable.

In the Arduino, we needed to embed the HTML form code in the program. On the Pi we are able to use an external file:

HTMLfile =open("piweb.html")
myHTML =

Listing 3 shows a basic stand alone micro web-server program that will present a web page and monitor a STOP and GO button action.

Listing 3

01 #!/usr/bin/python
02 from BaseHTTPServer import BaseHTTPRequestHandler, HTTPServer
04 PORT_NUMBER = 3000
06 #This class will handles any incoming browser request
08 global myHTML
10 class myHandler(BaseHTTPRequestHandler):
12     #Handler for the GET requests
13     def do_GET(self):
14         # Check for new commands
15         if "GO" in self.path:
16             print "GO FORWARD"
17         if "STOP" in self.path:
18             print "STOP"
19         # Write the Web page out
20         self.send_response(200)
21         self.send_header('Content-type','text/html')
22         self.end_headers()
23         self.rfile
24         # Send the html message
25         self.wfile.write(myHTML)
26         return
28 try:
30     HTMLfile =open ("piweb.html")
31     myHTML =
33     #Create a web server and define the handler to manage the
34     #incoming request
35     server = HTTPServer(('', PORT_NUMBER), myHandler)
36     print 'Started httpserver on port ' , PORT_NUMBER
38     #Wait forever for incoming HTTP requests
39     server.serve_forever()
41 except KeyboardInterrupt:
42     print '^C received, shutting down the web server'
43     server.socket.close()

To find the IP address of your Pi, run the command ifconfig wlan. To call the web page from the PSP don't forget to add the port number, for example: When running your program you should see the PSP push button feedback similar to what you can see in Listing 4.

Listing 4


01 Started httpserver on port  3000
03 - - [05/Feb/2017 16:24:51] "GET /GO? HTTP/1.1" 200 -
04 - - [05/Feb/2017 16:24:51] "GET /favicon.ico HTTP/1.1" 200 -
06 - - [05/Feb/2017 16:24:55] "GET /STOP? HTTP/1.1" 200 -
07 - - [05/Feb/2017 16:24:55] "GET /favicon.ico HTTP/1.1" 200 -
08 .
09 .
10 .

You can modify this basic Python code to create many fun projects. We added a car chassis, an ExplorerHAT Pro (for motor controls), a USB missile launcher [3] and a portable USB battery to create a moving missile launcher (Figure 7).

Figure 7: Defend your bedroom with a PSP-controlled missile launcher.

Buy this article as PDF

Express-Checkout as PDF

Pages: 6

Price $2.95
(incl. VAT)

Buy Raspberry Pi Geek

Get it on Google Play

US / Canada

Get it on Google Play

UK / Australia

Related content