forked from stigtsp/esp8266-environment-monitor
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Micropython/ESP8266 sending data from TMP36 and CCS811 to MQTT
- Loading branch information
0 parents
commit ea561f5
Showing
6 changed files
with
86 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
config.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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/" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() |