Advertisement
Guest User

network temp sensor

a guest
Jun 25th, 2023
426
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.05 KB | None | 0 0
  1. import network
  2. import urequests
  3. import machine
  4. import utime
  5. import gc # Import the garbage collector
  6.  
  7. # Connect to network
  8. wlan = network.WLAN(network.STA_IF)
  9. wlan.active(True)
  10.  
  11. led = machine.Pin("LED", machine.Pin.OUT)
  12.  
  13. # Fill in your network name (SSID) and password here:
  14. ssid = 'SuperHot'
  15. password = '(pass)'
  16. while not wlan.isconnected():
  17. wlan.connect(ssid, password)
  18. utime.sleep(2) # Wait for 2 seconds before retrying
  19.  
  20. url = "http://192.168.4.23:8000/data"
  21. headers = {
  22. "Content-Type": "text/plain"
  23. }
  24.  
  25. LM35 = machine.ADC(0) #setup analog reading on ADC
  26. Cal_Offset = 5050 #Calibration offset value
  27. #Determined from practical testing
  28.  
  29. def Compute_Temp(Avg_A):
  30. LM35_A = Avg_A + Cal_Offset #Add Calibration Adjustment
  31. LM35_V = LM35_A * .00005 #Convert analog reading to volts
  32. Tmp_C = round((LM35_V * 100),1) #Convert volts to temp celcius
  33. Tmp_F = round((Tmp_C * 1.8 + 32),1) #Convert Tmp_C to Tmp_F
  34. return Tmp_C, Tmp_F #Return Temps
  35.  
  36.  
  37. Samples = 0 #Variable holds all samples
  38. Num_Samples = 1 #Counter for num samples collected
  39. while True:
  40. led.on()
  41. utime.sleep(.5)
  42. led.off()
  43. utime.sleep(.25)
  44. if Num_Samples <= 10: #storing a total of 10 samples
  45. LM35_A = LM35.read_u16() #Read the ADC port to get sensor data
  46. Samples = Samples + LM35_A #Add current reading to sample batch
  47. Num_Samples += 1 #Increment counter
  48. else:
  49. Avg_A = Samples / 10 #Get the average of samples
  50. Samples = 0 #Reset Samples variable to zero
  51. Num_Samples = 1 #Reset counter to one
  52. T_c, T_f = Compute_Temp(Avg_A) #Fetch the temps from the function
  53. data = "Celcius=" + str(T_c) + " Fahrenheit=" + str(T_f)
  54. response = urequests.put(url, data=data, headers=headers)
  55.  
  56. gc.collect() # Invoke the garbage collector here
  57.  
  58. utime.sleep(.1) #slow the loop down
  59.  
  60.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement