Skip to content

Commit

Permalink
Micropython/ESP8266 sending data from TMP36 and CCS811 to MQTT
Browse files Browse the repository at this point in the history
  • Loading branch information
stigtsp committed Sep 2, 2018
0 parents commit ea561f5
Show file tree
Hide file tree
Showing 6 changed files with 86 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
config.json
3 changes: 3 additions & 0 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[submodule "CCS811"]
path = CCS811
url = https://github.com/stigtsp/CCS811-micropython
1 change: 1 addition & 0 deletions CCS811
Submodule CCS811 added at 710151
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# set the AMPY_PORT environment variable

upload:
ampy put CCS811/CCS811.py CCS811.py
ampy put config.json config.json
ampy put main.py main.py
14 changes: 14 additions & 0 deletions config.json.sample
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"wifi": {
"ssid" : "FIXME",
"psk" : "FIXME"
},
"mqtt": {
"server" : "FIXME",
"port" : 8883,
"ssl" : true,
"password" : "FIXME",
"user" : "FIXME"
"topic_prefix": "FIXME/"
}
}
61 changes: 61 additions & 0 deletions main.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
import network
import time
import machine
import ujson
import ubinascii
import sys
from umqtt.robust import MQTTClient
import CCS811
from machine import ADC

with open('config.json') as fp:
config = ujson.loads(fp.read())

station = network.WLAN(network.STA_IF)

station.active(True)
station.connect(config['wifi']['ssid'],config['wifi']['psk'])

while not station.isconnected():
machine.idle()

ap_if = network.WLAN(network.AP_IF)
if ap_if.active():
ap_if.active(False)

CLIENT_ID = ubinascii.hexlify(machine.unique_id())

c = MQTTClient(client_id = CLIENT_ID,
server = config['mqtt']['server'],
user = config['mqtt']['user'],
password = config['mqtt']['password'],
port = config['mqtt']['port'],
ssl = config['mqtt']['ssl']
)

c.connect()

topic_prefix = config['mqtt']['topic_prefix']

# CCS811 sensor
i2c = machine.I2C(scl=machine.Pin(5), sda=machine.Pin(4))
s = CCS811.CCS811(i2c, addr=91)

adc = ADC(0) # TMP36 sensor

def publish(c, k, v):
c.publish(topic_prefix + k, str(v), True)

while True:

if s.data_ready():
print('eCO2: %d ppm, TVOC: %d ppb' % (s.eCO2, s.tVOC))
publish(c, "eCO2", s.eCO2)
publish(c, "tVOC", s.tVOC)
temp = adc.read() / 10
print('Temp: %s' % temp)
publish(c,"temp", temp)

time.sleep_ms(3000)

c.disconnect()

0 comments on commit ea561f5

Please sign in to comment.