Skip to content

AzureAdvocateBit/MicroPython-ESP8266-Environmental-Data-Streaming-1

 
 

Repository files navigation

Streaming Data from ESP8266 using MicroPython and MQTT

Resources

Other

Overview of ESP8266 flashing process

  1. Install esptool
  2. Erase Flash
  3. Deploy MicroPython Firmware

See Flashing MicroPython Flasing How-to Tutorial

MicroPython and Publish over MQTT

Deploy the solution to the ESP8266 MicroPython flash as follows.

See Adafruit MicroPython Tool (ampy) for information on copying files to the ESP32.

pip install adafruit-ampy

ampy --port /dev/ttyUSB0 put boot.py
ampy --port /dev/ttyUSB0 put main.py
ampy --port /dev/ttyUSB0 put config.py

ampy --port /dev/ttyUSB0 put bme280.py
ampy --port /dev/ttyUSB0 put sht30.py

ampy --port /dev/ttyUSB0 put config_default.json

ampy --port /dev/ttyUSB0 put sensor_bme280.py
ampy --port /dev/ttyUSB0 put sensor_sht30.py
ampy --port /dev/ttyUSB0 put sensor_fake.py
# boot.py

import network
sta_if = network.WLAN(network.STA_IF)
sta_if.active(True)
sta_if.connect("Wifi SSID", "Wifi password")
# main.py

# http://docs.micropython.org/en/latest/esp8266/index.html
# http://garybake.com/wemos-oled-shield.html
# http://docs.micropython.org/en/latest/esp8266/esp8266/quickref.html#adc-analog-to-digital-conversion

from umqtt.robust import MQTTClient
from machine import I2C, Pin
import utime as time
import gc
import ssd1306 as oled
from machine import ADC
import esp
import config


# Wifi connect established in the boot.py file. Uncomment if needed
# import network
# sta_if = network.WLAN(network.STA_IF)
# sta_if.active(True)


#upip packages - see README.md
# upip.install('micropython-umqtt.simple')
# upip.install('micropython-umqtt.robust')

BuiltinLedPin = 2

cfg = config.Config('config_default.json')

sta_if.connect(cfg.wifiSsid, cfg.wifiPwd)
client = MQTTClient(str(esp.flash_id()), cfg.mqttBroker)
mySensor = cfg.sensor.Sensor()

builtinLed = Pin(BuiltinLedPin, Pin.OUT)
adc = ADC(0)            # create ADC object on ADC pin
i2c = I2C(scl=Pin(5), sda=Pin(4)) 

esp.sleep_type(esp.SLEEP_LIGHT)


def initDisplay(i2c):
    i2cDevices = I2C.scan(i2c)
    if 0x3c in i2cDevices:
        display = oled.SSD1306_I2C(64, 48, i2c)
        return True
    else:        
        print('No OLED Display found')
        return False

def initialise():
    blinkcnt = 0
    checkwifi()
    while blinkcnt < 50:
        builtinLed.value(blinkcnt % 2)
        blinkcnt = blinkcnt + 1
        time.sleep_ms(100)

def checkwifi():
    blinkcnt = 0
    while not sta_if.isconnected():
        time.sleep_ms(500)
        builtinLed.value(blinkcnt % 2)
        blinkcnt = blinkcnt + 1

def setContrast():
    lightlevel = adc.read()
    if lightlevel < 200:
        return 0
    if lightlevel < 600:
        return 100
    return 255

def publish():
    count = 1
    while True:
        builtinLed.value(0)
        checkwifi()
        
        temperature, pressure, humidity = mySensor.measure()

        freeMemory = gc.mem_free()

        if oledDisplay:
            display.fill(0)
            display.contrast(setContrast())
            display.text(v[0], 0, 0)
            display.text(v[1], 0, 10)
            display.text(v[2], 0, 20)
            display.text(str(freeMemory), 0, 30)
            display.text(str(count), 0, 40)
            display.show()    

        msg = b'{"DeviceId":"%s",MsgId":%u,"Mem":%u,"Celsius":%s,"Pressure":%s,"Humidity":%s}' % (cfg.deviceId, count, freeMemory, temperature, pressure, humidity)
        client.publish(b"home/weather/%s" % cfg.deviceId, msg)

        builtinLed.value(1)
        count = count + 1
        
        time.sleep(cfg.sampleRate)


oledDisplay = initDisplay(i2c)

initialise()

client.reconnect()

publish()

Connecting to ESP32 with Putty

Install Putty for your platform. Connect at 115200 baud rate

See Adafruit Serial REPL Tutorial.

About

MicroPython ESP8266 Environmental Data Streaming over MQTT Sample

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • Python 98.4%
  • Batchfile 1.2%
  • Shell 0.4%