The ESP32 module, successor to the ESP8266, is a low cost (less than $15), low power microcontroller with integrated Wi-Fi & dual-mode Bluetooth. The ESP32 series employs a Tensilica Xtensa LX6 microprocessor in both dual-core and single-core variations. The ESP32 was created and developed by Espressif Sysytems, a Shanghai-based company with a proven record of quality microcontroller production and distribution.

By reading and following closely to this quick start guide you will be able to send data from the ESP32 module to Ubidots using the Publish method over MQTT. 

Requirements

Setup:

A. Setting up ESP32-DevkitC board
B. Setting up the project Pymakr plugin
C. Library Setup
D. Coding time

A. Setting up ESP32-DevkitC board

1. To begin, it is necessary to install a pre-build toolchain, please refer to the following complete step-by-step guides based on your OS to do so correctly. Windows, Linux or MacOS.

2. With the pre-built toolchain install complete, connect, with the USB cable, the ESP32-DevKitC board to your PC, and check the serial port where the device is connected. We will use the Port Identifier repeatedly below to communicate with your device. 

For Linux or MAC, execute the below function(s) in your computer’s terminal to determine the port you are communicating with.

Linux OS

ls /dev/tty*

Mac OS

macbook:user$ cd /dev
macbook:user$ ls -ltr /dev/*usb*

Windows OS

  • Press "windows" + "R"" 
  • Type “devmgmt.msc” into text field, and left click on “OK” 
  • Select and open “Ports (COM&LPT)” to see all serial ports available

3. Next, flash any firmware of the ESP32 with the below commands.
NOTE: It’s possible the name of the port changes for you.

Flash the device

esptool.py --chip esp32 --port /dev/{PORT-IDENTIFIER} erase_flash

4. Download the latest firmware for the ESP32 module to your PC, and upload the firmware to the ESP32 using the below command.

Upload the latest firmware

esptool.py --chip esp32 --port /dev/{PORT-IDENTIFIER} write_flash -z 0x1000 <firmware>.bin

B. Setting up the project Pymakr plugin

Pymakr is a plugin developed by Pycom, that work wells with the two popular text editors, Atom and Visual Studio. The Pymakr plugin makes everything much simpler when programming with micropython.

In this guide, we will be working with Atom, but feel free to choose the one that is most suitable to you.

1- Open the Atom editor, and activate the Pymakr plugin.

2- Connect the board with the Atom editor. Once the board is connected you will see the message "connected" in the status:

3- To begin, create a new directory called "ESP32" to manage the codes and libraries. Once the directory is created in the Atom editor, select Open a Project, and search for the directory "ESP32," (just created) and open it.

4- In the folder "ESP32" we are going to manage the libraries and main/boot codes. First, we are going to add the main and boot files. Click on the folder "ESP32" and select "New File", assign "boot.py" as the file name. 

5- Repeat this steps to create the main file called "main.py".

6- Now synchronize the project. In the upper toolbar, select Packages > Pymakr > Synchronize project.


C. Library Setup

Now, that we have the project built and synchronized, it's time to code the ESP32 to send data over the MQTT protocol.

1- To publish data over MQTT, install the libraries umqtt.robust and umqtt.simple

First, we are going to add the folders to hold the libraries. Right click on the folder "ESP32" and select "New folder", and assign "lib" as the name.

2- Next, we have to add the library “umqtt” into the "lib" folder. Right click on the folder "lib" and select "New Folder" and assign it the name: "umqtt".

3- Then, click on the folder “umqtt” and select “New File”, assigning the name “simple.py”. Copy the code from the Micropython “simple.py”repository and paste it into “simple.py” file. Next, repeat these instructions but with “robust.py” as the file name and paste this “robust.py” Micropython library into the file.

**Save your simple.py and robust.py code files when finished. 

4- Complete the setup by synchronizing the project. In the upper toolbar, select Packages > Pymakr > Synchronize project.

D. Coding time

1- Now that we have the project built and synchronized, it's time to code. To send data to Ubidots copy, paste, and save the below code into the “boot.py” file.

**Save the file once you’ve pasted the code in the "boot.py" file.

import time

import network

import machine as m

sta_if = network.WLAN(network.STA_IF); sta_if.active(True)

sta_if.scan() # Scan for available access points

sta_if.connect("SSID", "PASSWORD") # Connect to an AP

sta_if.isconnected()

time.sleep(3)

2- Next, the sample code below will create the functions to check the Internet connection, publish data, and upload the data to Ubidots. In this step, it is necessary to obtain your private Ubidots TOKEN and replace your token where indicated in the code below. 

Copy, paste, and save the below code into the "main.py" file. Where indicated, replace the text with your unique Ubidots TOKEN in the code (be sure to leave the quotation marks). 

from umqtt.robust import MQTTClient

import machine as m

ubidotsToken = “YOUR-UBIDOTS-TOKEN-HERE”

clientID = “RANDOM-ALPHA-NUMERIC-NAME_OR_IMEI DEVICE ID”

client = MQTTClient("clientID", "industrial.api.ubidots.com", 1883, user = ubidotsToken, password = ubdotsToken)

def checkwifi():

    while not sta_if.isconnected():

        time.sleep_ms(500)

        print(".")

        sta_if.connect()

pin13 = m.Pin(13, m.Pin.IN, m.Pin.PULL_UP)

def publish():

    while True:

        checkwifi()

        client.connect()

        lat = 6.2

        lng = -75.56

        var = repr(pin13.value())

        msg = b'{"Switch": {"MQTT-test":%s, "context":{"lat":%s, "lng":%s}}}' % (var, lat, lng)

        print(msg)

        client.publish(b"/v1.6/devices/ESP32", msg)

        time.sleep(20)

publish()

3- Confirm all codes have been saved properly; then, synchronize the project once more. Repeat steps 6 and 4 above respectively. Once the project has been synchronized with the device, the module will run automatically.

Results

Go to your Ubidots account to visualize the device and data just created.

In less than 30 minutes, the ESP32 module is now available for publishing data to the Ubidots cloud over MQTT protocol, using WiFi. Now it’s time to build a dashboard and create widgets to manage the variables of your ESP32 module. 

Did this answer your question?