MicroPython Iceboat

My daughter’s and I have built a number of air boat projects, but this time I thought that I’d try it using MicroPython.

For this project my goal was to create a MicroPython application that was a standalone WiFi access point, and then run a small web server for controlling the fans.

Hardware

The hardware that I used on this project included:

  • 3 – L9110 Fans ($5 each)
  • 1 – ESP-8266  module. (I used a Wemos D1 $10)
  • 1 – Uno proto shield (optional) or use a small breadboard
  • 9V battery or portable phone charger
  • some K-Nex blocks

Fans

The L9110 fans are designed for Arduino projects and they cost about $5. These fans have four pins: VCC, GND, INA (direction), and INB (on/off). The INA direction pin and INB on/off  pin can be a little confusing because if the direction=0 (forward spinning) then the running state is INB=0 and off is INB=1. This is reversed if the direction=1 or backward spinning.

l9110_fan

MicroPython Hardware

MicroPython is supported on a number of Wifi enabled ESP-8266 type modules. These modules are well priced with the NodeMCU modules starting around $5. It’s important to note that most of the ESP-8266 modules also support Lua programming and Arduino C.

nodemcu

For this project I used an ESP-8266 module that came in an Arduino Uno form factor. The reason for this is that the typical MicroPython modules only support 3.3V where as the  Uno compatible modules support 5V. The fans will work with 3.3V but they generate a lot more wind power at 5V.

wemo_uno

Boat Construction

For the boat frame I used K’Nex pieces because they offer a light weight sturdy construction. Some of the important points in the construction were:

  • tightly securing the fans. For this I wire wrapped the fans to the frame
  • use tape to secure the battery and electronics to the frame
  • thin runners

iceboat_details

MicroPython Setup

The uPyCraft IDE offers a nice development environment.

upycraft

There are some excellent tutorials for get you started, I would recommend : https://randomnerdtutorials.com/getting-started-micropython-esp32-esp8266/.

For MicroPython projects you typically create 2 Python applications, a boot.py and a main.py . I like to think of this like in Arduino where you have a setup() function and a loop() function. For this project I kept things simple and I put everything in the boot.py file.

Creating an Access Point

There is lots of documentation on how to have MicroPython connect to a Wifi LAN, but not a lot on how to create a standalone WiFi access point. The code that I used for this is:

# MicroPython boot.py - Access Point Only Example
try:
  import usocket as socket
except:
  import socket
import network

station = network.WLAN(network.AP_IF)
station.active(True)
station.config(essid='ESP32')
station.config(authmode=3,password='12345678')

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

When the code is run you will be able to see when you connect and disconnect to the access point. For this example the access point is call ESP32 with a password of 12345678.

access_point

MicroPython Web Server

After getting the Access Point working the next step is to create a Web server. This is a simple Web server so I embedded the HTML content into my Python code, however for a more complex application I would definitely have my web pages as files that are independent from the code.

# MicroPython boot.py - Access Point Web Server

try:
  import usocket as socket
except:
  import socket

import network

station = network.WLAN(network.AP_IF)
station.active(True)
station.config(essid='ESP32')
station.config(authmode=3,password='12345678')

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

def web_page(request):
  
  fans_state = ""
  if request.find('/?forward') > 0:
    fans_state="Going Forward"
  if request.find('/?Stopped') > 0:
    fans_state="Stopped" 
  
  html = """<html><head> <title>Ice Boat Web Server</title> 
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="data:,"> <style>
  html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}
  .button{display: inline-block; background-color: #e7bd3b; border: none; 
  border-radius: 4px; color: white; text-decoration: none; font-size: 30px; width:100%}
  </style></head>
  <body> <h1>Ice Boat Web Server</h1> 
  <p>ICE Boat : <strong>""" + fans_state + """</strong></p>
  <p><a href='/?forward'><button class="button">Forward</button></a></p>
  <p><a href='/?stop'><button class="button button">STOP</button></a></p>
  
  </body></html>"""
  
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('The Content = %s' % request)
  response = web_page(request)
  conn.send(response)
  conn.close()

For the Web Server example application, we start with the access point connection code, then we setup a socket on port 80. The HTTP request/response sequence is passed through a function called web_page(request).  In web_page(request) we look for keywords in the HREF request. 

The embedded HTML code passes keywords using anchor tags, for example: <a href=’/?forward’>. Often we use mobile frameworks like Bootstrap to help with formatting, however because we’re running a standalone access point we need to manually define all our style codes.

To access our MicroPython web server, use the address: 192.168.4.1. This is the default address (in the access point setup you can change this). When our web server is running we should be able to toggle the stop/forward states.

web_server_screen0

Writing Outputs

The MicroPython command line interface is good way to test outputs. To access the command line interface, use the connect button and then enter “Control-C”.

To manage pins on the hardware, machine library is used :

from machine import Pin

Then pin objects can be defined as either inputs or outputs:

Pin14 = Pin(14, Pin.IN)
Pin5 = Pin(5, Pin.OUT)

The value of a pin is read using: pinobject.value()

motor_test

Final Application

Now that we have all the pieces working we can pull it all together. The fan control code was a little messy because 2 fans were front facing and spinning forward, and 1 fan was facing backwards spinning in reverse. With the back fan I needed to reverse the direction and its on/off settings.

# MicroPython boot.py - Access Point Web Server
import time
try:
  import usocket as socket
except:
  import socket

from machine import Pin
import network

# Define the left, right and back fans pin
motorR = Pin(5,Pin.OUT)
motorL = Pin(12,Pin.OUT)
motorB = Pin(4,Pin.OUT)
motorBdir = Pin(16,Pin.OUT) # sets the back motor direction
# start with fans stopped, Note: my FANs are 0=run
motorR.value(1)
motorL.value(1)
motorB.value(0)
motorBdir.value(0)

station = network.WLAN(network.AP_IF)
station.active(True)
station.config(essid='ESP32')
station.config(authmode=3,password='12345678')

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())

def fancontrol(left,right,back):
  motorL.value(left)
  motorR.value(right)
  motorB.value(back)

def web_page(request):
  
  fans_state = "Stopped"
  if request.find('/?forward') > 0:
    fans_state="Going Forward"
    fancontrol(0,0,1)
  if request.find('/?left') > 0:
    fans_state="Going Left"
    fancontrol(0,1,1)
  if request.find('/?right') > 0:
    fans_state="Going Right" 
    fancontrol(1,0,1)
  if request.find('/?stop') > 0:
    fans_state="Stopped"
    fancontrol(1,1,0)
  
  html = """<html><head> <title>Ice Boat Web Server</title> 
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="data:,"> <style>
  html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}
  .button{display: inline-block; background-color: #e7bd3b; border: none; 
  border-radius: 4px; color: white; text-decoration: none; font-size: 30px; width:100%}
  .button2{background-color: #4286f4; width:49%}
  </style></head>
  <body> <h1>Ice Boat Web Server</h1> 
  <p>ICE Boat : <strong>""" + fans_state + """</strong></p>
  <p><a href='/?forward'><button class="button">Forward</button></a></p>
  <p><a href='/?left'><button class="button button2">LEFT</button></a>
  <a href='/?right'><button class="button button2" >RIGHT</button></a></p>
  <p><a href='/?stop'><button class="button button">STOP</button></a></p>
  
  </body></html>"""
  
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('The Content = %s' % request)
  response = web_page(request)
  conn.send(response)
  conn.close()

ice_boat_page

 

PSP Controlled Arduino Airboat

We thought that it would be fun to try and use an old PlayStation Portable (PSP) on some Arduino and Pi Projects. If you don’t a have PSP you can usually find a used one at a good price.

Some smart people were able to modify or “mod” the PSP firmware so that it is possible to run open source applications on the PSP. We tried using Python, Lua, sdlBasic and SSH to talk between our PSP  and our Arduinos and Pi’s, but none of these methods were simple or 100% reliable. In the end we found that basic built-in PSP Web browser worked the best and it didn’t require a ‘moded’ PSP.

airboat

PSP Setup and Limitations

We were using an older PSP-1000 so if you have a newer PSP GO or PSP Vita you may not have the same limitation that we found. However we think if you stick to our ‘worst case’ setup you should be good to go.

Our recommended setup was:

  • Simple Web Pages
  • No Browser Cache
  • Simple Wireless Network

For the PSP-1000 the web pages had to be very simple, no CSS (Cascading Style Sheets) and no advanced HTML tagging. We had hoped to show Node-Red Web pages from the PI but this was not possible.

In our testing we found that it was important to turn off the browser cache, otherwise we found that our commands would only work once. To turn off the PSP browser cache, go into the PSP browser and select “Tools”, then “Settings”, and “Cache Settings”.

cache

Our older PSP-1000 had some problems with the newer WPA2 wireless encryption, so to simplify things we created a small standalone open network. For Arduino projects this isn’t a problem because the Arduino can be made into a standalone access point. On Pi projects where you are using an existing wireless network you might need to do some tweeking to add a guest account.

To add a new connection on the PSP go to the “Network Settings” and select “Infrastructure Mode”. Then select “[New Connection]” and “Scan”. The scan will only show networks that the PSP is able to connect to.

networksettings

A Simple Web Form

An HTML form supports two types of action, a POST and a GET. The GET method is the simpler (but less secure) approach and it passes parameters on the URL command line.

Below is a simple Web form:

<html>
<body>
<h1>Click a button to control the car</h1>
<form action='GO' method='GET' >
<INPUT TYPE='submit' VALUE="GO" >
</form>
<form action='STOP' method='GET' >
<INPUT TYPE="submit" VALUE="STOP" >
</form>
<form action='LEFT' method='GET' >
<INPUT TYPE='submit' VALUE="LEFT" >
</form>
<form action='RIGHT' method='GET' >
<INPUT TYPE="submit" VALUE="RIGHT" >
</form>

</body>
</html>

menu

An Arduino Web Server

To create Arduino WiFi projects the ESP8266 based modules are low cost way to go. There are some good ESP8266 libraries and the examples are fairly easy to follow. The ESP8266 module can be wired into an Arduino Uno/Nano/Mega module or you can by buy boards with the ESP8266 chip integrated in. For our testing we used an older WeMo board, but other options like the NodeMCU, Adafruit HUZZAH or even the Arduino Yún could be used.

The ESP8266WebServer library has a simple standalone access point example. We modified this example (WifiAccessPoint) to include HTML form tags for all our required action.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

int pinleft = 12;
int pinright = 13;
int pinfront = 14;

/* Set these to your desired credentials. */
const char *ssid = "MY8266";
const char *password = "";

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>

 \
<form action='/left' method='GET'> \
<input type='submit' style='font-size:150px' value='LEFT'></form>

 \
<form action='/right' method='GET'> \
<input type='submit' style='font-size:150px' value='RIGHT'></form>

 \
</body></html>";

ESP8266WebServer server(80);

/* Just a little test message. Go to http://192.168.4.1 in a web browser
* connected to this access point to see it.
*/
void handleRoot() {
Serial.println("Base page");
server.send(200, "text/html", webpage);
}

void go() {
Serial.println("Go forward");
server.send(200, "text/html", webpage);
digitalWrite(pinleft,LOW);
digitalWrite(pinright,LOW);
digitalWrite(pinfront,LOW);

}
void stop() {
Serial.println("Stop");
server.send(200, "text/html", webpage);
digitalWrite(pinleft,HIGH);
digitalWrite(pinright,HIGH);
digitalWrite(pinfront,HIGH);
}
void left() {
Serial.println("Go left");
server.send(200, "text/html", webpage);
digitalWrite(pinleft,HIGH);
digitalWrite(pinright,LOW);
digitalWrite(pinfront,HIGH);

}
void right() {
Serial.println("Go right");
server.send(200, "text/html", webpage);
digitalWrite(pinleft,LOW);
digitalWrite(pinright,HIGH);
digitalWrite(pinfront,HIGH);
}

void setup() {
delay(1000);
Serial.begin(115200);
Serial.println();
pinMode(pinleft,OUTPUT);
pinMode(pinright,OUTPUT);
pinMode(pinfront,OUTPUT);

digitalWrite(pinleft,HIGH);
digitalWrite(pinright,HIGH);
digitalWrite(pinfront,HIGH);

Serial.print("Configuring access point...");
/* You can remove the password parameter if you want the AP to be open. */
WiFi.softAP(ssid, password);

IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
server.on("/", handleRoot);
server.on("/go",go);
server.on("/stop",stop);
server.on("/left",left);
server.on("/right",right);
server.begin();
Serial.println("HTTP server started");
}

void loop() {
server.handleClient();
}