Arduino Nano RP2040 Connect: Difference between revisions

From Sketching with Hardware at LMU Wiki
Jump to navigation Jump to search
No edit summary
No edit summary
(One intermediate revision by one other user not shown)
Line 2: Line 2:


https://docs.arduino.cc/hardware/nano-rp2040-connect
https://docs.arduino.cc/hardware/nano-rp2040-connect
https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-python-api#imu-lsm6dsox
https://github.com/openmv/openmv/blob/918ccb937730cc759ee5709df089d9de516dc7bf/scripts/libraries/lsm6dsox.py


= How to control the Builtin LED MicroPython =
= How to control the Builtin LED MicroPython =

Revision as of 10:12, 14 March 2022

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

https://github.com/openmv/openmv/blob/918ccb937730cc759ee5709df089d9de516dc7bf/scripts/libraries/lsm6dsox.py


How to control the Builtin LED MicroPython

this is GPIO6 (D13 in Arduino)

from machine import Pin
from time import sleep 

# using the internal LED on the Pico - pin 25 
myLED = Pin(6, Pin.OUT)

while True:
      # this switches the LED on for 1 second
      myLED.on()
      sleep(0.1)
      # this switches the LED off for 500 ms
      myLED.off()
      sleep(0.5)


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

# Wi-Fi AP Mode Example
#
# This example shows how to use Wi-Fi in Access Point mode.
# this version by Albrecht Schmidt, https://www.sketching-with-hardware.org/wiki/
# based on the following examples:
# https://randomnerdtutorials.com/esp32-esp8266-micropython-web-server/
# https://docs.arduino.cc/tutorials/nano-rp2040-connect/rp2040-python-api


import network, socket, time
from machine import Pin

led = Pin(6, Pin.OUT)              #on board LED

SSID ='Nano_RP2040_Connect_test'   # Network SSID
KEY  ='12345678'                 # Network key (should be 8 chars) - for real use, choose a safe one
HOST = ''
PORT = 80                          # 80 ist the http standard port, can also use non-privileged port e.g. 8080

# Init wlan module and connect to network
wlan = network.WLAN(network.AP_IF)
wlan.active(True)
# it seems in this version the AP mode only supports WEP
wlan.config(essid=SSID, key=KEY, security=wlan.WEP, channel=2)

print("AP mode started. SSID: {} IP: {}".format(SSID, wlan.ifconfig()[0]))


# create the webpage with a button to toggle the LED
def web_page():
  if led.value() == 1:
    led_state="ON"
  else:
    led_state="OFF"
  
  html ="""<html><head>
      <title>Nano RP2040 Connnect Web Server</title>
      <meta name="viewport" content="width=device-width, initial-scale=1">
      <link rel="icon" href="data:,">
      </head>
      <body>
        <h1>Nano RP2040 Connnect </1>
        <h2>Web Server Test</h2>
        <p>LED state: <strong>""" + led_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
        <p><a href="/?led=off"><button class="button button2">OFF</button></a></p>
      </body>
      </html>"""
  return html

# get started with setting up the sever sockedt
server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# Bind and listen
server.bind([HOST, PORT])
server.listen(5)

# loop to deal with  http requests
while True:
  conn, addr = server.accept()
  print('Connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Request Content = %s' % request)
  # check if the request includes led=on or off
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  # request is 'GET /?led=on' or 'GET /?led=off' - the string starts at position 6 (counting starts at 0)
  if led_on == 6:
    print('LED ON')
    led.value(1)
  if led_off == 6:
    print('LED OFF')
    led.value(0)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.send(response)
  conn.close()