def run(): global wifi, mqtt, sensor_pin, state, t0 # Connect Wifi wifi = core.WifiWrapper(config) wifi.connect() # Connect MQTT mqtt = core.MQTTClientWrapper(config) mqtt.connect() # Init sensorpin sensor_pin = machine.Pin(config.PIN_SENSOR, machine.Pin.IN) sensor_pin.irq(trigger=machine.Pin.IRQ_RISING, handler=motion_sens_callback) t0 = utime.ticks_ms() while True: utime.sleep(1) if abs(utime.ticks_ms() - t0) >= 10000: if state: payload = {'name': 'motion_state', 'value': False} mqtt.publish(config.MQTT_TOPIC + '/out', ujson.dumps(payload), qos=2) state = False
def run(): wifi = None mqtt = None adc = machine.ADC(0) # analog digital converter while True: try: # connect wifi if wifi is None: wifi = core.WifiWrapper(config) if not wifi.isconnected: wifi.connect() # connect mqtt if mqtt is None: mqtt = core.MQTTClientWrapper(config) mqtt.connect() # get values from sensor val = 100 * adc.read() / 1024 # publish value payload = {'name': 'moisture', 'value': val, 'unit': 'pct'} mqtt.publish(config.MQTT_TOPIC, ujson.dumps(payload)) mqtt.disconnect() core.sleep(config) except Exception as e: print(e)
def run(): wifi = None mqtt = None i2c = machine.I2C(scl=machine.Pin(config.I2C_SCL_PIN), sda=machine.Pin(config.I2C_SDA_PIN)) adr = i2c.scan()[0] bme = bme280.BME280(i2c=i2c, address=adr) while True: try: # connect wifi if wifi is None: wifi = core.WifiWrapper(config) if not wifi.isconnected: wifi.connect() # connect mqtt if mqtt is None: mqtt = core.MQTTClientWrapper(config) mqtt.connect() # get values from sensor vals = bme.values temp = vals[0][:-1] pressure = vals[1][:-3] humidity = vals[2][:-1] # publish temperature utime.sleep_ms(500) payload = {'name': 'temperature', 'value': temp, 'unit': 'C'} mqtt.publish(config.MQTT_TOPIC + '/temp', ujson.dumps(payload)) # publish pressure utime.sleep_ms(500) payload = {'name': 'pressure', 'value': pressure, 'unit': 'bar'} mqtt.publish(config.MQTT_TOPIC + '/pressure', ujson.dumps(payload)) # publish humidity utime.sleep_ms(500) payload = {'name': 'humidity', 'value': humidity, 'unit': 'pct'} mqtt.publish(config.MQTT_TOPIC + '/humidity', ujson.dumps(payload)) mqtt.disconnect() core.sleep(config) except Exception as e: print(e)
def run(): global mqtt # Bring up Wifi try: wifi = core.WifiWrapper(config) wifi.connect() except core.WifiConnectionError: machine.reset() # Start WebREPL try: webrepl.start() except Exception as e: print(e) # Connect to MQTT Broker try: mqtt = core.MQTTClientWrapper(config) mqtt.connect() mqtt.subscribe(config.MQTT_TOPIC + '/in', handle) except core.MQTTConnectionError: print('Could not connect to Mosquitto server. Performing reset ' 'in {} seconds.'.format(config.ERROR_RESET_TIME_S)) utime.sleep(config.ERROR_RESET_TIME_S) machine.reset() # Workloop ping_ticks_ms = utime.ticks_ms() while True: try: mqtt.check_msg() if (abs(utime.ticks_ms() - ping_ticks_ms) >= (1000 * config.MQTT_KEEPALIVE / 2.0)): print('Sending Ping...', end='') mqtt.ping() print('done') ping_ticks_ms = utime.ticks_ms() except Exception as e: print(e) print('Performing reset in {} seconds.'.format( config.ERROR_RESET_TIME_S)) utime.sleep(config.ERROR_RESET_TIME_S) machine.reset() utime.sleep_ms(config.REFRESH_TIME_MS)
def run(): wifi = None mqtt = None while True: try: if wifi is None: wifi = core.WifiWrapper(config) wifi.connect() if mqtt is None: mqtt = core.MQTTClientWrapper(config) mqtt.connect() ow_pin = config.ONEWIRE_PIN ow = onewire.OneWire(machine.Pin(ow_pin)) print('searching for Onewire Sensors on pin {}...'.format(ow_pin), end='') temp_sens = ds18x20.DS18X20(ow) rom = temp_sens.scan() if len(rom) > 0: print('done. found {} sensors. reading from first.'.format( len(rom))) temp_sens.convert_temp() temp = temp_sens.read_temp(rom[0]) print('temperature: {:.2f}°C'.format(temp)) # create payload payload = {'value': '{:.2f}'.format(temp), 'unit': 'C'} mqtt.publish(config.MQTT_TOPIC, ujson.dumps(payload)) else: print('failed. none found.') if config.ENABLE_DEEPSLEEP: mqtt.disconnect() core.sleep(config) except Exception as e: print(e)
# This file is executed on every boot (including wake-boot from deepsleep) # import esp # esp.osdebug(None) from config import config import main import core global wifi, mqtt # Connect Wifi wifi = core.WifiWrapper(config) wifi.connect() # Connect MQTT mqtt = core.MQTTClientWrapper(config) mqtt.connect() main.run()