-
Notifications
You must be signed in to change notification settings - Fork 7
/
publishThingspeak.py
79 lines (69 loc) · 2.57 KB
/
publishThingspeak.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
# The MIT License (MIT)
# Copyright (c) 2019 Mike Teachman
# https://opensource.org/licenses/MIT
#
# Example MicroPython code showing how to use the MQTT protocol to
# publish data to a Thingspeak channel
#
# User configuration parameters are indicated with "ENTER_".
import network
from umqtt.robust import MQTTClient
import time
import gc
import sys
# Configuration parameters
#=======================================================
WIFI_SSID = 'ENTER_WIFI_SSID'
WIFI_PASSWORD = 'ENTER_WIFI_PASSWORD'
THINGSPEAK_MQTT_CLIENT_ID = b"ENTER_THINGSPEAK_MQTT_CLIENT_ID"
THINGSPEAK_MQTT_USERNAME = b"ENTER_THINGSPEAK_MQTT_USERNAME"
THINGSPEAK_MQTT_PASSWORD = b"ENTER_THINGSPEAK_MQTT_PASSWORD"
THINGSPEAK_CHANNEL_ID = b'ENTER_THINGSPEAK_CHANNEL_ID'
#=======================================================
# turn off the WiFi Access Point
ap_if = network.WLAN(network.AP_IF)
ap_if.active(False)
# connect the device to the WiFi network
wifi = network.WLAN(network.STA_IF)
wifi.active(True)
wifi.connect(WIFI_SSID, WIFI_PASSWORD)
# wait until the device is connected to the WiFi network
MAX_ATTEMPTS = 20
attempt_count = 0
while not wifi.isconnected() and attempt_count < MAX_ATTEMPTS:
attempt_count += 1
time.sleep(1)
if attempt_count == MAX_ATTEMPTS:
print('could not connect to the WiFi network')
sys.exit()
# connect to Thingspeak MQTT broker
# connection uses unsecure TCP (port 1883)
#
# To use a secure connection (encrypted) with TLS:
# set MQTTClient initializer parameter to "ssl=True"
# Caveat: a secure connection uses more heap space
THINGSPEAK_MQTT_USERNAME = THINGSPEAK_MQTT_CLIENT_ID
client = MQTTClient(server=b"mqtt3.thingspeak.com",
client_id=THINGSPEAK_MQTT_CLIENT_ID,
user=THINGSPEAK_MQTT_USERNAME,
password=THINGSPEAK_MQTT_PASSWORD,
ssl=False)
try:
client.connect()
except Exception as e:
print('could not connect to MQTT server {}{}'.format(type(e).__name__, e))
sys.exit()
# continually publish two fields to a Thingspeak channel using MQTT
PUBLISH_PERIOD_IN_SEC = 20
while True:
try:
freeHeapInBytes = gc.mem_free()
credentials = bytes("channels/{:s}/publish".format(THINGSPEAK_CHANNEL_ID), 'utf-8')
payload = bytes("field1={:.1f}&field2={:.1f}\n".format(freeHeapInBytes, 10.2), 'utf-8')
client.publish(credentials, payload)
time.sleep(PUBLISH_PERIOD_IN_SEC)
except KeyboardInterrupt:
print('Ctrl-C pressed...exiting')
client.disconnect()
wifi.disconnect()
break