Upgrade to Pro — share decks privately, control downloads, hide ads and more …

MicroPython

 MicroPython

An introduction to building cheap IoT devices with MicroPython. Talk given at BrightonPy on 12th October 2017.

Jamie Matthews

October 12, 2017
Tweet

More Decks by Jamie Matthews

Other Decks in Programming

Transcript

  1. What we are trying to build: A standalone temperature and

    humidity sensor that publishes readings into Home Assistant. Cheap, quick and easy.
  2. “MicroPython is a lean and efficient implementation of the Python

    3 programming language…” “…that includes a small subset of the Python standard library…” “…and is optimised to run on microcontrollers and in constrained environments.” Bytecode compiler + runtime Runs directly on the microcontroller hardware - no Linux
  3. Tip: Don’t bother trying to do any of this on

    a Mac. Buy a Raspberry Pi and do this over SSH (or something)
  4. from time import sleep from umqtt.robust import MQTTClient import dht

    import machine import network import ubinascii import ujson WIFI_SSID = "<password>" WIFI_PASSWORD = "<ssid>" MQTT_BROKER = "192.168.86.126" # static ip of pi MQTT_USERNAME = "<username>" MQTT_PASSWORD = "<password>" SENSOR_PIN = 4 CLIENT_ID = b"esp8266_" + ubinascii.hexlify(machine.unique_id()) MQTT_TOPIC = b"home" + b"/" + CLIENT_ID SECONDS_BETWEEN_READINGS = 600 def connect_to_wifi(): sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to network...') sta_if.active(True) sta_if.connect(WIFI_SSID, WIFI_PASSWORD) while not sta_if.isconnected(): pass print('network config:', sta_if.ifconfig()) def setup_mqtt_client(): client = MQTTClient(CLIENT_ID, MQTT_BROKER, user=MQTT_USERNAME, password=MQTT_PASSWORD) client.connect() print("Connected to {}”.format(MQTT_BROKER)) return client def setup_dht_sensor(): return dht.DHT11(machine.Pin(SENSOR_PIN)) def send_readings(mqtt_client, dht_sensor): dht_sensor.measure() temperature = dht_sensor.temperature() humidity = dht_sensor.humidity() print("Temperature: {}, humidity: {}".format(temperature, humidity)) payload = ujson.dumps({"temperature": temperature, "humidity": humidity}) mqtt_client.publish(MQTT_TOPIC, payload) def main(): connect_to_wifi() mqtt_client = setup_mqtt_client() dht_sensor = setup_dht_sensor() while True: send_readings(mqtt_client, dht_sensor) sleep(SECONDS_BETWEEN_READINGS) if __name__ == "__main__": main() ~ 60 lines
  5. from time import sleep from umqtt.robust import MQTTClient import dht

    import machine import network import ubinascii import ujson
  6. WIFI_SSID = "<password>" WIFI_PASSWORD = "<ssid>" MQTT_BROKER = "192.168.86.126" #

    static ip of pi MQTT_USERNAME = "<username>" MQTT_PASSWORD = "<password>" SENSOR_PIN = 4 CLIENT_ID = b"esp8266_" + ubinascii.hexlify(machine.unique_id()) MQTT_TOPIC = b"home" + b"/" + CLIENT_ID SECONDS_BETWEEN_READINGS = 600
  7. def main(): connect_to_wifi() mqtt_client = setup_mqtt_client() dht_sensor = setup_dht_sensor() while

    True: send_readings(mqtt_client, dht_sensor) sleep(SECONDS_BETWEEN_READINGS) if __name__ == "__main__": main()
  8. def main(): connect_to_wifi() mqtt_client = setup_mqtt_client() dht_sensor = setup_dht_sensor() while

    True: send_readings(mqtt_client, dht_sensor) sleep(SECONDS_BETWEEN_READINGS) if __name__ == "__main__": main()
  9. def connect_to_wifi(): sta_if = network.WLAN(network.STA_IF) if not sta_if.isconnected(): print('connecting to

    network...') sta_if.active(True) sta_if.connect(WIFI_SSID, WIFI_PASSWORD) while not sta_if.isconnected(): pass print('network config:', sta_if.ifconfig()) (boilerplate)
  10. def main(): connect_to_wifi() mqtt_client = setup_mqtt_client() dht_sensor = setup_dht_sensor() while

    True: send_readings(mqtt_client, dht_sensor) sleep(SECONDS_BETWEEN_READINGS) if __name__ == "__main__": main()
  11. def main(): connect_to_wifi() mqtt_client = setup_mqtt_client() dht_sensor = setup_dht_sensor() while

    True: send_readings(mqtt_client, dht_sensor) sleep(SECONDS_BETWEEN_READINGS) if __name__ == "__main__": main()
  12. def main(): connect_to_wifi() mqtt_client = setup_mqtt_client() dht_sensor = setup_dht_sensor() while

    True: send_readings(mqtt_client, dht_sensor) sleep(SECONDS_BETWEEN_READINGS) if __name__ == "__main__": main()
  13. def send_readings(mqtt_client, dht_sensor): dht_sensor.measure() temperature = dht_sensor.temperature() humidity = dht_sensor.humidity()

    payload = ujson.dumps({ "temperature": temperature, "humidity": humidity }) mqtt_client.publish(MQTT_TOPIC, payload)
  14. ESP8266 x5 £18.99 Sensor x5 £7.19 USB power plug x5

    £9.25 6” USB cable x5 £6.95 Plastic project box x5 £8.75 Total £51.13 Per unit: £10.23