-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadafruit-connect.py
85 lines (74 loc) · 2.41 KB
/
adafruit-connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
#
# Micropython ESP8266 code to Publish temperature sensor data to an Adafruit IO Feed using the MQTT protocol
# Also publishes statistics on the number of free bytes on the micropython Heap
#
# Hardware used:
# - Adafruit Huzzah ESP8266 running micropython version esp8266-20160909-v1.8.4.bin
# - Adafruit MCP9808 temperature breakout board
# - USB to serial converter
#
# prerequisites:
# - Adafruit account
# - registered to use Adafruit IO
#
# References and Kudos
#
# Big thanks to Tony DiCola from Adafruit for excellent tutorials on:
# Ampy tutorial: valuable tool to efficiently develop python code on ESP8266 hardware:
# https://learn.adafruit.com/micropython-basics-load-files-and-run-code
# i2c on micropython hardware tutorial:
# https://learn.adafruit.com/micropython-hardware-i2c-devices
#
import network
import time
from machine import Pin
from umqtt.simple import MQTTClient
#
# conversion routine, MCP9808 2-byte response --> Degrees C (courtesy of Tony DiCola)
#
'''def convertMCP9808ToDegC(data):
value = data[0] << 8 | data[1]
temp = (value & 0xFFF) / 16.0
if value & 0x1000:
temp -= 256.0
return temp
'''
#
# configure i2c for communication to MCP9808 sensor hardware
#
butt = Pin(0, Pin.IN, Pin.PULL_UP)
#
# connect the ESP8266 to local wifi network
#
yourWifiSSID = " "
yourWifiPassword = " "
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect(yourWifiSSID, yourWifiPassword)
while not sta_if.isconnected():
pass
#
# connect ESP8266 to Adafruit IO using MQTT
#
myMqttClient = "miket-mqtt-client" # can be anything unique
adafruitIoUrl = "io.adafruit.com"
adafruitUsername = " " # can be found at "My Account" at adafruit.com
adafruitAioKey = " " # can be found by clicking on "VIEW AIO KEYS" when viewing an Adafruit IO Feed
c = MQTTClient(myMqttClient, adafruitIoUrl, 0, adafruitUsername, adafruitAioKey)
c.connect()
#
# publish temperature and free heap to Adafruit IO using MQTT
#
# note on feed name in the MQTT Publish:
# format:
# "<adafruit-username>/feeds/<adafruitIO-feedname>"
# example:
# "MikeTeachman/feeds/feed-TempInDegC"
#
#
while True:
if not butt.value():
c.publish("username/feeds/digital", str(1)) #publish num free bytes on the Heap
print("send ok")
time.sleep(2) # number of seconds between each Publish
c.disconnect()