Advertisement
Guest User

Untitled

a guest
Jun 10th, 2017
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1. import machine
  2. import time
  3. from umqtt import MQTTClient
  4. from network import WLAN
  5.  
  6.  
  7. main()
  8.  
  9.  
  10. def main():
  11. setup_wifi()
  12. username = "your Adafruit IO username"
  13. mqtt = MQTTClient(client_id=str(machine.rng()), server="io.adafruit.com", port=8883,
  14. user=username, password="your Adafruit IO password", ssl=True)
  15. mqtt.connect()
  16. adc = machine.ADC()
  17. pin = adc.channel(pin="P20")
  18. samples = 100
  19. while True:
  20. temperature = measure_temperature(samples, pin)
  21. print(temperature)
  22. mqtt.publish("{}/feeds/office-temperature".format(username), str(temperature))
  23. time.sleep(30)
  24.  
  25.  
  26. def setup_wifi():
  27. wlan = WLAN(mode=WLAN.STA)
  28. wlan.antenna(WLAN.EXT_ANT)
  29. wlan.connect("your WiFi SSID", auth=(WLAN.WPA2, "your WiFi password"), timeout=5000)
  30. while not wlan.isconnected():
  31. machine.idle()
  32.  
  33.  
  34. def measure_temperature(samples, pin):
  35. temperature = 0
  36. for _ in range(samples):
  37. temperature += analog_to_temperature(pin())
  38. temperature /= samples
  39. return temperature
  40.  
  41.  
  42. def analog_to_temperature(adc_value):
  43. temp = 100 * (adc_value / 4095) - 50
  44. return temp
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement