Arduino Nano RP2040 Connect

From Sketching with Hardware at LMU Wiki
Revision as of 09:10, 14 March 2022 by 217.248.49.125 (talk)
Jump to navigation Jump to search

https://docs.arduino.cc/learn/programming/arduino-and-python

https://docs.arduino.cc/hardware/nano-rp2040-connect

https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-python-api#imu-lsm6dsox

How to control the Builtin LED MicroPython

this is GPIO6 (D13 in Arduino)

 1 from machine import Pin
 2 from time import sleep 
 3 
 4 # using the internal LED on the Pico - pin 25 
 5 myLED = Pin(6, Pin.OUT)
 6 
 7 while True:
 8       # this switches the LED on for 1 second
 9       myLED.on()
10       sleep(0.1)
11       # this switches the LED off for 500 ms
12       myLED.off()
13       sleep(0.5)


Example of a WIFI-Access Point and Webserver to control the LED

 1 # Wi-Fi AP Mode Example
 2 #
 3 # This example shows how to use Wi-Fi in Access Point mode.
 4 # this version by Albrecht Schmidt, https://www.sketching-with-hardware.org/wiki/
 5 # based on the following examples:
 6 # https://randomnerdtutorials.com/esp32-esp8266-micropython-web-server/
 7 # https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-python-api
 8 
 9 
10 import network, socket, time
11 from machine import Pin
12 
13 led = Pin(6, Pin.OUT)              #on board LED
14 
15 SSID ='Nano_RP2040_Connect_test'   # Network SSID
16 KEY  ='12345678'                 # Network key (should be 8 chars) - for real use, choose a safe one
17 HOST = ''
18 PORT = 80                          # 80 ist the http standard port, can also use non-privileged port e.g. 8080
19 
20 # Init wlan module and connect to network
21 wlan = network.WLAN(network.AP_IF)
22 wlan.active(True)
23 # it seems in this version the AP mode only supports WEP
24 wlan.config(essid=SSID, key=KEY, security=wlan.WEP, channel=2)
25 
26 print("AP mode started. SSID: {} IP: {}".format(SSID, wlan.ifconfig()[0]))
27 
28 
29 # create the webpage with a button to toggle the LED
30 def web_page():
31   if led.value() == 1:
32     led_state="ON"
33   else:
34     led_state="OFF"
35   
36   html ="""<html><head>
37       <title>Nano RP2040 Connnect Web Server</title>
38       <meta name="viewport" content="width=device-width, initial-scale=1">
39       <link rel="icon" href="data:,">
40       </head>
41       <body>
42         <h1>Nano RP2040 Connnect </1>
43         <h2>Web Server Test</h2>
44         <p>LED state: <strong>""" + led_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
45         <p><a href="/?led=off"><button class="button button2">OFF</button></a></p>
46       </body>
47       </html>"""
48   return html
49 
50 # get started with setting up the sever sockedt
51 server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
52 # Bind and listen
53 server.bind([HOST, PORT])
54 server.listen(5)
55 
56 # loop to deal with  http requests
57 while True:
58   conn, addr = server.accept()
59   print('Connection from %s' % str(addr))
60   request = conn.recv(1024)
61   request = str(request)
62   print('Request Content = %s' % request)
63   # check if the request includes led=on or off
64   led_on = request.find('/?led=on')
65   led_off = request.find('/?led=off')
66   # request is 'GET /?led=on' or 'GET /?led=off' - the string starts at position 6 (counting starts at 0)
67   if led_on == 6:
68     print('LED ON')
69     led.value(1)
70   if led_off == 6:
71     print('LED OFF')
72     led.value(0)
73   response = web_page()
74   conn.send('HTTP/1.1 200 OK\n')
75   conn.send('Content-Type: text/html\n')
76   conn.send('Connection: close\n\n')
77   conn.send(response)
78   conn.close()