021 - ESP32 MicroPython: MQTT Part 1: Publish

Instruction

Are you interested in Internet of Things? Have you heard of MQTT? If yes, please stay and watch this video. Because in this video, we will learn to create a basic setup of MQTT system using the ThingSpeak server and an ESP32 using MicroPython language.

In tutorial 18, we learned to use the Thingspeak IoT platform to store and diplay DHT sensor readings. We use ESP32 to send data to Thingspeak server by using RESTful API which is through the HTTP Protocol.

Now in this video, we will achieve the same by using the esp32 to send dht sensor data to Thingspeak server but this time using MQTT Protocol.

MQTT stands for MQ Telemetry Transport. MQ refers to the MQ series, a product develop by IBM to support the MQTT protocol.

Bill Of Materials

  1. ESP32 development board or any microcontroller with MicroPython firmware installed.
  2. DHT22 or any sensor with similar capability.

Hardware Instruction

  1. Connect the DHT22 VCC pin to ESP32 3.3V pin.
  2. Connect the DHT22 Data pin to ESP32 D23 pin.
  3. Connect the DHT22 GND pin to ESP32 GND pin.

Video Demonstration

Call To Action

For any concern, write your message in the comment section.

You might also like to support my journey on Youtube by Subscribing. Click this to Subscribe to TechToTinker.

Thank you and have a good days ahead.

See you,

– George Bantique | tech.to.tinker@gmail.com

Source Code

1. Example # 1:

 1# **************************************#
 2#  MQTT in MicroPython with Thingspeak  #
 3# **************************************#
 4# Author: George Bantique               #
 5#         TechToTinker Youtube Channel  #
 6#         TechToTinker.blogspot.com     #
 7#         tech.to.tinker@gmail.com      #
 8# Date: Dec.5, 2020                    #
 9# Please feel free to modify the code   #
10# according to your needs.              #
11# **************************************#
12
13# **************************************#
14# Load necessary libraries
15import machine
16import network
17import wifi_credentials
18from umqtt.simple import MQTTClient
19import dht
20import time
21
22# **************************************#
23# Objects:
24led = machine.Pin(2,machine.Pin.OUT)
25d = dht.DHT22(machine.Pin(23))
26
27# **************************************#
28# Configure the ESP32 wifi as STAtion.
29sta = network.WLAN(network.STA_IF)
30if not sta.isconnected():
31  print('connecting to network...')
32  sta.active(True)
33  #sta.connect('wifi ssid', 'wifi password')
34  sta.connect(wifi_credentials.ssid, wifi_credentials.password)
35  while not sta.isconnected():
36    pass
37print('network config:', sta.ifconfig())
38
39# **************************************#
40# Global variables and constants:
41SERVER = "mqtt.thingspeak.com"
42client = MQTTClient("umqtt_client", SERVER)
43CHANNEL_ID = "1249898"
44WRITE_API_KEY = "PJX6E1D8XLV18Z87"
45# topic = "channels/1249898/publish/PJX6E1D8XLV18Z87"
46topic = "channels/" + CHANNEL_ID + "/publish/" + WRITE_API_KEY
47UPDATE_TIME_INTERVAL = 5000 # in ms unit
48last_update = time.ticks_ms()
49
50# **************************************#
51# Main loop:
52while True:
53    if time.ticks_ms() - last_update >= UPDATE_TIME_INTERVAL:
54        d.measure()
55        t = d.temperature()
56        h = d.humidity()
57    
58        #payload = "field1=" + str(t) + "&field2=" + str(h)
59        payload = "field1={}&field2={}" .format(str(t), str(h))
60
61        client.connect()
62        client.publish(topic, payload)
63        client.disconnect()
64
65        print(payload)
66        led.value(not led.value())
67        last_update = time.ticks_ms()

2. wifi_credentials.py :

1ssid = "your wifi ssid"
2password = "your wifi password"

References And Credits

  1. Thingspeak: thingspeak.com

  2. Mathworks registration page: https://www.mathworks.com/mwaccount/register



Posts in this series



No comments yet!

GitHub-flavored Markdown & a sane subset of HTML is supported.