Led on, off via ap network and python request
Led on, off via ap network and python request
Can't get micropython to .find '/ ledon' if sent by python on PC, so have changed parameters to Pledon and Pledoff. Works ok but now micropython server code not compatible with browser Chrome. To be fixed. It's that damn '/'.
Here's the newish micropython code from Thonny:
----------------------------------------------------------
#Now called WiFiSensor0.py from old WiFiAPLEDS3.py. Going to add internal temperature logging
#Worked ok Tue Aug 6 11:04:01 NZST 2024. Can't get other mode to work Will play with this one.
import network
import time
import socket
#from machine import Pin
import machine
# led = Pin(25, Pin.OUT)
# led.off()
# led.on()
led = machine.Pin("LED", machine.Pin.OUT)
#led.off()
led.on()
adcpin = 4
sensor = machine.ADC(adcpin)
def ReadTemperature():
adc_value = sensor.read_u16()
volt = (3.3/65535) * adc_value
temperature = 27 - (volt - 0.706)/0.001721
return round(temperature, 1)
# def web_page():
# html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
# <body><h1>Hello 33w21World</h1></body></html>
# """
# return html
#todo make this HTML into separate file
# if you do not see the network you may have to power cycle
# unplug your pico w for 10 seconds and plug it in again
def ap_mode(ssid, password):
"""
Description: This is a function to activate AP mode
Parameters:
ssid[str]: The name of your internet connection
password[str]: Password for your internet connection
Returns: Nada
"""
# file = open("simpleled.html")
# html = file.read()
# file.close()
# Just making our internet connection
stateis ='Starting up'
ap = network.WLAN(network.AP_IF)
ap.config(essid=ssid, password=password)
ap.active(True)
while ap.active() == False:
pass
print('AP Mode Is Active, You can Now Connect')
print('IP Address To Connect to:: ' + ap.ifconfig()[0])
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) #creating socket object
s.bind(('', 80))
s.listen(5)
while True:
conn, addr = s.accept()
print('Got a connection from %s' % str(addr))
request = conn.recv(1024)
print('Content received by Pico = %s' % str(request))
#response = web_page()
#new---
request = str(request)
#led_on = request.find('/light/on')
#led_off = request.find('/light/off')
# led_on = request.find(' /ledon') #the space is important
# led_off = request.find(' /ledoff')
#todo make led_on report true (not -1), if it sees ' ledon' (Chrome requests) or 'Pledon' (Python requests) OR statement should work.
led_on = request.find('Pledon') #Python sends 'Pledon' in request string parameters
led_off = request.find('Pledoff')
print( 'led on = ' + str(led_on))
print( 'led off = ' + str(led_off))
#---finish new---got -1 for off and 308 for on--
#new new--
#todo sort out indenting. Better to do != -1
#if led_on > 0:
if led_on != -1:
print("led on")
led.value(1)
stateis = "LED is ON"
if led_off != -1:
print("led off")
led.value(0)
stateis = "LED is OFF"
file = open("simpleled1.html")
html = file.read()
file.close()
temperature = ReadTemperature()
html2 = html.replace('**ledState**', stateis)
html3 = html2.replace('**TEMP**',str(temperature))
print(temperature)
print(stateis)
response=html3 #new
conn.send(response)
conn.close()
#s.close() #Try this !!
ap_mode('NAME',
'PASSWORD')
#todo put -->html = html.replace('**ledState**', led_state_text) before line 79

Comments
Post a Comment