Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import network
- import urequests
- import machine
- import utime
- import gc # Import the garbage collector
- # Connect to network
- wlan = network.WLAN(network.STA_IF)
- wlan.active(True)
- led = machine.Pin("LED", machine.Pin.OUT)
- # Fill in your network name (SSID) and password here:
- ssid = 'SuperHot'
- password = '(pass)'
- while not wlan.isconnected():
- wlan.connect(ssid, password)
- utime.sleep(2) # Wait for 2 seconds before retrying
- url = "http://192.168.4.23:8000/data"
- headers = {
- "Content-Type": "text/plain"
- }
- LM35 = machine.ADC(0) #setup analog reading on ADC
- Cal_Offset = 5050 #Calibration offset value
- #Determined from practical testing
- def Compute_Temp(Avg_A):
- LM35_A = Avg_A + Cal_Offset #Add Calibration Adjustment
- LM35_V = LM35_A * .00005 #Convert analog reading to volts
- Tmp_C = round((LM35_V * 100),1) #Convert volts to temp celcius
- Tmp_F = round((Tmp_C * 1.8 + 32),1) #Convert Tmp_C to Tmp_F
- return Tmp_C, Tmp_F #Return Temps
- Samples = 0 #Variable holds all samples
- Num_Samples = 1 #Counter for num samples collected
- while True:
- led.on()
- utime.sleep(.5)
- led.off()
- utime.sleep(.25)
- if Num_Samples <= 10: #storing a total of 10 samples
- LM35_A = LM35.read_u16() #Read the ADC port to get sensor data
- Samples = Samples + LM35_A #Add current reading to sample batch
- Num_Samples += 1 #Increment counter
- else:
- Avg_A = Samples / 10 #Get the average of samples
- Samples = 0 #Reset Samples variable to zero
- Num_Samples = 1 #Reset counter to one
- T_c, T_f = Compute_Temp(Avg_A) #Fetch the temps from the function
- data = "Celcius=" + str(T_c) + " Fahrenheit=" + str(T_f)
- response = urequests.put(url, data=data, headers=headers)
- gc.collect() # Invoke the garbage collector here
- utime.sleep(.1) #slow the loop down
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement