adafruit / Adafruit_CircuitPython_ESP_ATcontrol

Use the ESP AT command sent to communicate with the Interwebs
MIT License
20 stars 17 forks source link

Example SoftAP Mode? #62

Open 10bn opened 2 years ago

10bn commented 2 years ago

Hi, eventhough this is depricated, could maybe someone provide an example for how to use this in SoftAP Mode?

This is my first approach but it's missing ssid name, pw and for sure other imortant lines. It would be great if someone could help with this. :)

# Adafruit CircuitPython 7.3.2 on 2022-07-20; Challenger NB RP2040 WiFi with rp2040
# References:
# - https://cdn-learn.adafruit.com/downloads/pdf/adding-a-wifi-co-processor-to-circuitpython-esp8266-esp32.pdf
import time
import board
import busio
from digitalio import DigitalInOut, Direction
import rtc
from adafruit_espatcontrol import adafruit_espatcontrol, adafruit_espatcontrol_wifimanager

# Setup system LED
led = DigitalInOut(board.LED)
led.direction = Direction.OUTPUT

# Print Board ID
print(board.board_id)
# Get wifi details and more from a secrets.py file
try:
    from secrets import secrets
except ImportError:
    print("WiFi secrets are kept in secrets.py, please add them there!")
    raise

# Debug Level
# Change the Debug Flag if you have issues with AT commands
debugflag = False

# How Long to sleep between polling
sleep_duration = 5

# Wifi settings for challenger_nb_rp2040_wifi
RX = board.ESP_RX
TX = board.ESP_TX
resetpin = DigitalInOut(board.WIFI_RESET)
rtspin = False
uart = busio.UART(TX, RX, baudrate=11520, receiver_buffer_size=2048)
esp_boot = DigitalInOut(board.WIFI_MODE)
esp_boot.direction = Direction.OUTPUT
esp_boot.value = True
status_light = None

print("ESP AT commands")

esp = adafruit_espatcontrol.ESP_ATcontrol(
    uart, 115200, reset_pin=resetpin, rts_pin=rtspin, debug=debugflag
)
print("Resetting ESP module")
esp.hard_reset()

print(esp.get_version())
esp.mode = adafruit_espatcontrol.ESP_ATcontrol.MODE_SOFTAP

# Complete project details at https://RandomNerdTutorials.com

def web_page():
  if led.value() == 1:
    gpio_state="ON"
  else:
    gpio_state="OFF"

  html = """<html><head> <title>ESP 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; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
  .button2{background-color: #4286f4;}</style></head><body> <h1>ESP Web Server</h1>
  <p>GPIO state: <strong>""" + gpio_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

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('Content = %s' % request)
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  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.sendall(response)
  conn.close()