prjbrook

Umin8Fork5.py best Ben network to date

Oct 9th, 2024
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.75 KB | None | 0 0
  1. #Sun Sep 29 13:59:57 NZDT 2024.Sun Sep 29 18:09:57 NZDT 2024.
  2. #Sun l29 13:59:57 NZDT 2024.Sun Sep 29 18:09:57 NZDT 2024
  3. #Umin3.py gets edited in Thonny via file explorer. Works. Oled OK
  4. #Next do SPI and SD card stff  Mon Sep 30 12:23:24 NZDT 2024..
  5.  
  6. import network
  7. import socket
  8. import machine
  9. import do_csv, do_log
  10. import sdcard
  11. import os
  12. import onewire, ds18x20, time
  13.  
  14. adcpin = 4
  15. sensor = machine.ADC(adcpin)
  16.  
  17. file = open("temps.txt", "w")
  18.  
  19. rtc=machine.RTC()
  20. timestamp=rtc.datetime()
  21. #temperature = 27 - (reading - 0.706)/0.001721
  22. #rtc.datetime((2020, 1, 21, 2, 10, 32, 36, 0))
  23.  
  24. timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] +
  25.                                             timestamp[4:7])
  26. file.write(timestring + "\n")
  27. file.flush()
  28. #-------------------------oled suff below------------------------------
  29. #set up oled display. NB need ssd1306 file in lib
  30.  
  31. #Next lines are for oled . Originally from digikey.
  32. from machine import Pin, I2C
  33. from ssd1306 import SSD1306_I2C    #make sure ssd1306 for micropythin is in libd
  34. i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
  35. oled = SSD1306_I2C(128, 64, i2c)
  36. oled.text("Ming92 Hardw7OOct", 0, 0)
  37. oled.show()  
  38. #-------------------------sd card stuff below---------------------
  39.  
  40. # Assign chip select (CS) pin (and start it high)  
  41. cs = machine.Pin(9, machine.Pin.OUT)  #does this really start high?
  42.  
  43. # Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000
  44. spi = machine.SPI(1,
  45.                   baudrate=100000,
  46.                   polarity=0,
  47.                   phase=0,
  48.                   bits=8,
  49.                   firstbit=machine.SPI.MSB,
  50.                   sck=machine.Pin(10),
  51.                   mosi=machine.Pin(11),
  52.                   miso=machine.Pin(8))
  53.  
  54. # Initialize SD card
  55. sd = sdcard.SDCard(spi, cs)     #make sure sdcard.py in path
  56.  
  57. vfs = os.VfsFat(sd)
  58. os.mount(vfs, "/sd")
  59.  
  60. # Create a file and write something to it
  61. with open("/sd/test03.txt", "w") as file:
  62.     file.write("Hello2,WWWWORX12345 69## SD World!\r\n")
  63.     file.write("This is a test and seems to work@@022 ABCDEfg1 Sat Sep 21 12:09:51 NZST 2024\r\n")
  64.     file.write("Fri Sep 20 12:10:40 NZST 2024\r\n")
  65. # Open the file we just created and read from it
  66. with open("/sd/test03.txt", "r") as file:
  67.     data = file.read()
  68.     print(data)
  69. #---------------DS18b20 1wire temp sensor below----------------
  70.  
  71. #Now do DS18b20 temperature stuff. The third sensor.    
  72. ds_pin = machine.Pin(22)
  73. ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
  74.  
  75. roms = ds_sensor.scan()
  76. print('Found DS devices: ', roms)  #will find only one at first
  77. ds_sensor.convert_temp()
  78. time.sleep_ms(750)
  79. for rom in roms:  #keep loop for later multiple roms
  80.     print(rom)   #byte array of Ds18b02. Only one at first
  81.     print(roms[0])
  82.     tempC = ds_sensor.read_temp(roms[0]) #just on sensor at first
  83.     print('temperature (ºC):', "{:.2f}".format(tempC))
  84.     print()
  85.  
  86. #----------------------------network stuff below --------------
  87.  
  88. # Set up access point
  89. ap = network.WLAN(network.AP_IF)
  90. te =""
  91. templ = "template5.html"  #"template1.html"   #update when form changes
  92. led = machine.Pin("LED", machine.Pin.OUT)
  93. led.on()
  94.  
  95. ap.config(essid='NAME', password='PASSWORD')
  96. ap.active(True)
  97.  
  98. while not ap.isconnected():
  99.     pass
  100.  
  101. print('AP Mode Is Active, You can Now Connect')
  102. print('IP Address To Connect to::', ap.ifconfig()[0])
  103.  
  104. # Set up socket
  105. addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
  106. s = socket.socket()
  107. s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)   #promising fix?
  108.  
  109. s.bind(addr)
  110. s.listen(1)
  111.  
  112. print('Listening on', addr)
  113. #----------------------------main loop--------------
  114. while True:
  115.     cl, addr = s.accept()
  116.     print('Got a connection from', addr)
  117.     request = cl.recv(1024)
  118.     request_str = request.decode('utf-8')
  119.     lines = request_str.split('\r\n')
  120.     if lines==['']:
  121.         print("lines have only one value")
  122.     else:
  123.         method, path, protocol = lines[0].split(' ')
  124.         print('The method,path,protocol= ',method,path,protocol)
  125.     if path=='/':
  126.         print("Very short path",path)
  127.     try:
  128.         with open(templ, 'r') as file:  #('simpleled2.html', 'r')
  129.             te = file.read()
  130.     except OSError:
  131.         te = "Error: template0.html file not found."
  132.        
  133.     response = """\
  134. HTTP/:1.1 200 OK
  135. Content-Type: text/html
  136. Content-Length: {}
  137.  
  138. {}
  139.  
  140. """.format(len(te), te)  #(len(x), x)
  141.    
  142. #-------------------do tasks based in path params-----------------------------------
  143.     if "toggle" in path:
  144.         led.toggle()
  145.     if "exttemp" in path:
  146.         ds_sensor.convert_temp()
  147.         time.sleep_ms(750)
  148.         tempC = ds_sensor.read_temp(rom)
  149.         et2=str(round(tempC,1))
  150.         response = response.replace("***",et2 )
  151.     if "inttemp" in path:
  152.         volt = (3.3/65535) * sensor.read_u16()
  153.         temperature = 27 - (volt - 0.706)/0.001721
  154.         response = response.replace("***", str(round(temperature,1)))
  155.     if "csv" in path:
  156.         print("csv in path ...")
  157.         response = do_csv.do_csv()
  158.     if "log" in path:
  159.         print("log in path ...")
  160.         response = do_log.do_log()
  161.     if "readRTC" in path:
  162.         print("Got readRTC request")
  163.         timestamp=rtc.datetime()
  164.         timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] + timestamp[4:7])
  165.         print("timestring is", timestring)
  166.         response = response.replace("***",timestring)
  167.     if "date" in path:
  168.          date_split = path.split('=')
  169.          time1=date_split[1]
  170.          time2 = date_split[1].replace("%2C",",")
  171.          time3 = time2.replace("+","")
  172.          time4 = time3.split(',')
  173.          ds_list=list(map(int,time4))
  174.          rtc.datetime(ds_list)
  175.          print("This ds_list[] inserted into rtc ...",ds_list)
  176.     cl.send(response)
  177.     cl.close()
  178.  
  179.  
Advertisement
Add Comment
Please, Sign In to add comment