Advertisement
prjbrook

WiFiSensor0.py LED buttons, intern temperature

Aug 15th, 2024
1,053
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.21 KB | None | 0 0
  1. #Now called WiFiSensor0.py from old WiFiAPLEDS3.py. Going to add internal temperature logging
  2. #Worked ok Tue Aug  6 11:04:01 NZST 2024. Can't get other mode to work  Will play with this one.
  3. import network
  4. import time
  5. import socket
  6. #from machine import Pin
  7. import machine
  8.  
  9.  
  10.  
  11. # led = Pin(25, Pin.OUT)
  12. # led.off()
  13. # led.on()
  14.  
  15.  
  16. led = machine.Pin("LED", machine.Pin.OUT)
  17. #led.off()
  18. led.on()
  19. adcpin = 4
  20. sensor = machine.ADC(adcpin)
  21. def ReadTemperature():
  22.     adc_value = sensor.read_u16()
  23.     volt = (3.3/65535) * adc_value
  24.     temperature = 27 - (volt - 0.706)/0.001721
  25.     return round(temperature, 1)
  26.  
  27.  
  28.  
  29. # def web_page():
  30. #   html = """<html><head><meta name="viewport" content="width=device-width, initial-scale=1"></head>
  31. #             <body><h1>Hello 33w21World</h1></body></html>
  32. #          """
  33. #   return html
  34. #todo make this HTML into separate file
  35.  
  36. # if you do not see the network you may have to power cycle
  37. # unplug your pico w for 10 seconds and plug it in again
  38. def ap_mode(ssid, password):
  39.     """
  40.        Description: This is a function to activate AP mode
  41.  
  42.        Parameters:
  43.  
  44.        ssid[str]: The name of your internet connection
  45.        password[str]: Password for your internet connection
  46.  
  47.        Returns: Nada
  48.    """
  49.    
  50. #     file = open("simpleled.html")
  51. #     html = file.read()
  52. #     file.close()
  53.    
  54.     # Just making our internet connection
  55.     stateis ='Starting up'
  56.     ap = network.WLAN(network.AP_IF)
  57.     ap.config(essid=ssid, password=password)
  58.     ap.active(True)
  59.  
  60.     while ap.active() == False:
  61.         pass
  62.     print('AP Mode Is Active, You can Now Connect')
  63.     print('IP Address To Connect to:: ' + ap.ifconfig()[0])
  64.  
  65.     s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)   #creating socket object
  66.     s.bind(('', 80))
  67.     s.listen(5)
  68.  
  69.     while True:
  70.       conn, addr = s.accept()
  71.       print('Got a connection from %s' % str(addr))
  72.       request = conn.recv(1024)
  73.       print('Content received by Pico = %s' % str(request))
  74.       #response = web_page()
  75.       #new---
  76.       request = str(request)
  77.       #led_on = request.find('/light/on')
  78.       #led_off = request.find('/light/off')
  79.       led_on = request.find(' /ledon')  #the space is important
  80.       led_off = request.find(' /ledoff')
  81.       print( 'led on = ' + str(led_on))
  82.       print( 'led off = ' + str(led_off))
  83.       #---finish new---got -1 for off and 308 for on--
  84.       #new new--
  85. #todo sort out indenting. Better to do  != -1
  86.       #if led_on > 0:
  87.       if led_on != -1:
  88.             print("led on")
  89.             led.value(1)
  90.             stateis = "LED is ON"
  91.       if led_off != -1:
  92.         print("led off")
  93.         led.value(0)
  94.         stateis = "LED is OFF"
  95.        
  96.       file = open("simpleled.html")
  97.       html = file.read()
  98.       file.close()
  99.       temperature = ReadTemperature()
  100.       html2 = html.replace('**ledState**', stateis)
  101.       html3 = html2.replace('**TEMP**',str(temperature))
  102.      
  103.       print(temperature)
  104.       print(stateis)
  105.       response=html3 #new
  106.       conn.send(response)
  107.       conn.close()
  108.       #s.close() #Try this !!
  109.  
  110. ap_mode('NAME',
  111.         'PASSWORD')
  112. #todo put -->html = html.replace('**ledState**', led_state_text) before line 79
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement