Skip to main content
All CollectionsConnect your Devices
Connect your ESP32 to Ubidots over HTTP using MicroPython
Connect your ESP32 to Ubidots over HTTP using MicroPython

Program your Espressif ESP32 module to communicate with Ubidots using HTTP and MicroPython code.

Sergio M avatar
Written by Sergio M
Updated over 3 months ago

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 HTTP. 

Requirements

Setup:

A. Setting up ESP32-DevkitC board
B. Setting up the project Pymakr plugin
C. 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. Coding Time

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

1- 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 network

import machine as m

import socket

import time

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). 

def checkwifi():

    while not sta_if.isconnected():

        time.sleep_ms(500)

        print("Awating connection")

        sta_if.connect()

def send_data(token, body):

    s = socket.socket()

    s.connect(("industrial.api.ubidots.com", 80))

    request=bytes('POST /api/v1.6/devices/ESP32 HTTP/1.1\r\nHost: industrial.api.ubidots.com\r\nX-Auth-Token: %s\r\nContent-Type: application/json\r\nContent-Length: %s\r\n\r\n%s\r\n' % (token, len(body), body), 'utf8')

    print("Sending data to Ubidots")

    s.send(request)

    dump_socket(s)

def dump_socket(s):

    try:

        while True:

            data = s.recv(100)

            if data:

                print(str(data, 'utf8'), end='')

            else:

                print('')  # end with newline

                s.close()

                break

    except:

        s.close()

        raise

import machine as m

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

ubidotsToken = “YOUR-UBIDOTS-TOKEN”

def Update():

    checkwifi()

    body='{"var2": ' + repr(pin13.value()) +'}'

    send_data("ubidotsToken", body)

while(True):

    Update()

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 just created.

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

Did this answer your question?