Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Sun Sep 29 13:59:57 NZDT 2024.Sun Sep 29 18:09:57 NZDT 2024.
- #Sun l29 13:59:57 NZDT 2024.Sun Sep 29 18:09:57 NZDT 2024
- #Umin3.py gets edited in Thonny via file explorer. Works. Oled OK
- #Next do SPI and SD card stff Mon Sep 30 12:23:24 NZDT 2024..
- import network
- import socket
- import machine
- import do_csv, do_log
- import sdcard
- import os
- import onewire, ds18x20, time
- adcpin = 4
- sensor = machine.ADC(adcpin)
- file = open("temps.txt", "w")
- rtc=machine.RTC()
- timestamp=rtc.datetime()
- #temperature = 27 - (reading - 0.706)/0.001721
- #rtc.datetime((2020, 1, 21, 2, 10, 32, 36, 0))
- timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] +
- timestamp[4:7])
- file.write(timestring + "\n")
- file.flush()
- #-------------------------oled suff below------------------------------
- #set up oled display. NB need ssd1306 file in lib
- #Next lines are for oled . Originally from digikey.
- from machine import Pin, I2C
- from ssd1306 import SSD1306_I2C #make sure ssd1306 for micropythin is in libd
- i2c=I2C(0,sda=Pin(0), scl=Pin(1), freq=400000)
- oled = SSD1306_I2C(128, 64, i2c)
- oled.text("Ming92 Hardw7OOct", 0, 0)
- oled.show()
- #-------------------------sd card stuff below---------------------
- # Assign chip select (CS) pin (and start it high)
- cs = machine.Pin(9, machine.Pin.OUT) #does this really start high?
- # Intialize SPI peripheral (start with 1 MHz) Check it IS 1000000
- spi = machine.SPI(1,
- baudrate=100000,
- polarity=0,
- phase=0,
- bits=8,
- firstbit=machine.SPI.MSB,
- sck=machine.Pin(10),
- mosi=machine.Pin(11),
- miso=machine.Pin(8))
- # Initialize SD card
- sd = sdcard.SDCard(spi, cs) #make sure sdcard.py in path
- vfs = os.VfsFat(sd)
- os.mount(vfs, "/sd")
- # Create a file and write something to it
- with open("/sd/test03.txt", "w") as file:
- file.write("Hello2,WWWWORX12345 69## SD World!\r\n")
- file.write("This is a test and seems to work@@022 ABCDEfg1 Sat Sep 21 12:09:51 NZST 2024\r\n")
- file.write("Fri Sep 20 12:10:40 NZST 2024\r\n")
- # Open the file we just created and read from it
- with open("/sd/test03.txt", "r") as file:
- data = file.read()
- print(data)
- #---------------DS18b20 1wire temp sensor below----------------
- #Now do DS18b20 temperature stuff. The third sensor.
- ds_pin = machine.Pin(22)
- ds_sensor = ds18x20.DS18X20(onewire.OneWire(ds_pin))
- roms = ds_sensor.scan()
- print('Found DS devices: ', roms) #will find only one at first
- ds_sensor.convert_temp()
- time.sleep_ms(750)
- for rom in roms: #keep loop for later multiple roms
- print(rom) #byte array of Ds18b02. Only one at first
- print(roms[0])
- tempC = ds_sensor.read_temp(roms[0]) #just on sensor at first
- print('temperature (ºC):', "{:.2f}".format(tempC))
- print()
- #----------------------------network stuff below --------------
- # Set up access point
- ap = network.WLAN(network.AP_IF)
- te =""
- templ = "template5.html" #"template1.html" #update when form changes
- led = machine.Pin("LED", machine.Pin.OUT)
- led.on()
- ap.config(essid='NAME', password='PASSWORD')
- ap.active(True)
- while not ap.isconnected():
- pass
- print('AP Mode Is Active, You can Now Connect')
- print('IP Address To Connect to::', ap.ifconfig()[0])
- # Set up socket
- addr = socket.getaddrinfo('0.0.0.0', 80)[0][-1]
- s = socket.socket()
- s.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1) #promising fix?
- s.bind(addr)
- s.listen(1)
- print('Listening on', addr)
- #----------------------------main loop--------------
- while True:
- cl, addr = s.accept()
- print('Got a connection from', addr)
- request = cl.recv(1024)
- request_str = request.decode('utf-8')
- lines = request_str.split('\r\n')
- if lines==['']:
- print("lines have only one value")
- else:
- method, path, protocol = lines[0].split(' ')
- print('The method,path,protocol= ',method,path,protocol)
- if path=='/':
- print("Very short path",path)
- try:
- with open(templ, 'r') as file: #('simpleled2.html', 'r')
- te = file.read()
- except OSError:
- te = "Error: template0.html file not found."
- response = """\
- HTTP/:1.1 200 OK
- Content-Type: text/html
- Content-Length: {}
- {}
- """.format(len(te), te) #(len(x), x)
- #-------------------do tasks based in path params-----------------------------------
- if "toggle" in path:
- led.toggle()
- if "exttemp" in path:
- ds_sensor.convert_temp()
- time.sleep_ms(750)
- tempC = ds_sensor.read_temp(rom)
- et2=str(round(tempC,1))
- response = response.replace("***",et2 )
- if "inttemp" in path:
- volt = (3.3/65535) * sensor.read_u16()
- temperature = 27 - (volt - 0.706)/0.001721
- response = response.replace("***", str(round(temperature,1)))
- if "csv" in path:
- print("csv in path ...")
- response = do_csv.do_csv()
- if "log" in path:
- print("log in path ...")
- response = do_log.do_log()
- if "readRTC" in path:
- print("Got readRTC request")
- timestamp=rtc.datetime()
- timestring="%04d-%02d-%02d %02d:%02d:%02d"%(timestamp[0:3] + timestamp[4:7])
- print("timestring is", timestring)
- response = response.replace("***",timestring)
- if "date" in path:
- date_split = path.split('=')
- time1=date_split[1]
- time2 = date_split[1].replace("%2C",",")
- time3 = time2.replace("+","")
- time4 = time3.split(',')
- ds_list=list(map(int,time4))
- rtc.datetime(ds_list)
- print("This ds_list[] inserted into rtc ...",ds_list)
- cl.send(response)
- cl.close()
Advertisement
Add Comment
Please, Sign In to add comment