Every amazing project needs to start somewhere, ideally somewhere simple.
Remote controlling GPIOs is essential for many IoT projects - and easier than you might think with MicroPython!
If you also use the PSoC board CY8CPROTO-062-4343W from Infineon like I do you need to install MicroPython on it first. Here you find a great tutorial on how to do that:
https://www.hackster.io/Infineon_Team/micropython-on-psoc-fcf1d0
Now that your MicroPython board is ready, let's set up an AP first. This is only a 4-liner in MicroPython:
import network
ap = network.WLAN(network.AP_IF)
ap.active(True)
ap.config(essid='PSoC', key='infineon')
If you run this code on your board you will already be able to connect to it with your phone via WiFi. A DHCP server for IP address assignment is automatically running in the background, no setup required.
Now let's get the webserver running! For this we can use a very lightweight webserver which I found on GitHub, nanoweb by hugokernel:
https://github.com/hugokernel/micropython-nanoweb
We need to make this module available on our MicroPython board. This can be achieved by downloading this file to your computer
https://raw.githubusercontent.com/hugokernel/micropython-nanoweb/master/nanoweb.py
and uploading it to your MicroPython board (e.g. using the Thonny IDE):
Now the hard part is done! The rest will be mainly copy/paste. Understanding is nice to have but optional ;)
Since we want to control a GPIO (in my case connected to the board's LED), we need to do the GPIO setup first:
from machine import Pin
led = Pin("P13_7") # LED pin for CY8CPROT-062-4343W board
led.init(Pin.OUT)
Now we can control the led by calling led.on() and led.off() - let's make use of that:
We import the nanoweb module we installed earlier and create an object of the nanoweb class:
from nanoweb import Nanoweb
naw = Nanoweb()
Now we define what the webserver does when certain paths are visited, so called "routes". The general form looks like that:
@naw.route("/path_on_the_server")
async def arbitary_function_name(request):
await request.write("HTTP/1.1 200 OK\r\n\r\n")
await request.write('Hello World')
This would call arbitary_function_name printing "Hello World" when visiting http://<device_ip>/path_on_the_server.
Enough theory, let's make it do something! This calls index() when the root (/) of the webserver is visited. Index will print the html status code "200 OK" to the client device and display some html buttons:
@naw.route("/")
async def index(request):
await request.write("HTTP/1.1 200 OK\r\n\r\n")
await request.write('<html>')
await request.write('''<button onclick="window.location.href='/on';">Turn LED on</button>''')
await request.write('''<button onclick="window.location.href='/off';">Turn LED off</button>''')
await request.write('</html>')
These buttons link to two new destinations on the server which we need to define:
@naw.route("/on")
async def on(request):
led.on()
await index(request)
@naw.route("/off")
async def off(request):
led.off()
await index(request)
I think you can guess, what the two functions do. At least the first line of each. The second line just calls index() again - to display the html buttons.
The last step to make this application working is to let the webserver run in an uasyncio loop what can be achieved like this:
import uasyncio
loop = uasyncio.get_event_loop()
loop.create_task(naw.run())
loop.run_forever()
That's it! You can now connect to the WiFi network of the board and use a browser to navigate to http://192.168.0.1 (or another IP address maybe dependent on the board you're using). By using the buttons you can remote-control the LED:
Comments