Connect ESP32 to Cloud MQTT Broker (TTGO T-Call ESP32 SIM800L)

Содержание

This guide shows how to connect the TTGO T-Call ESP32 SIM800L board to the Internet using a SIM card data plan and publish/subscribe to a cloud MQTT broker without using Wi-Fi. The cloud MQTT Mosquitto broker will be installed on a Digital Ocean server. We’ll also use Node-RED to visualize the readings and control the outputs from anywhere. The board will be programmed using Arduino IDE.

Connect ESP32 to Cloud MQTT Broker TTGO T-Call ESP32 SIM800L

With this setup, you can monitor and control your ESP32 from anywhere in the world, and the ESP32 doesn’t need to be connected to a wireless router because it connects to the internet using a SIM card data plan.

Introducing the TTGO T-Call ESP32 SIM800L

The TTGO T-Call is an ESP32 development board that combines a SIM800L GSM/GPRS module. You can get if for approximately $11.

TTGO T-Call ESP32 SIM800L GPS GPRS Board

Besides Wi-Fi and Bluetooth, you can communicate with this ESP32 board using SMS or phone calls and you can connect it to the internet using your SIM card data plan. This is great for IoT projects that don’t have access to a nearby router.

Important: the SIM800L works on 2G networks, so it will only work in your country, if 2G networks are available. Check if you have 2G network in your country, otherwise it won’t work.

To use the capabilities of this board you need to have a nano SIM card with data plan and a USB-C cable to upload code to the board.

ESP32 SIM800L Board USB-C cable for charging and data

The package includes some header pins, a battery connector, and an external antenna that you should connect to your board.

ESP32 SIM800L Board Package

However, we had some issues with that antenna, so we decided to switch to another type of antenna and all the problems were solved. The following figure shows the new antenna.

ESP32 SIM800L Board alternative antenna

Project Overview

The idea of this project is to connect your ESP32 to a Cloud MQTT broker to subscribe to an MQTT topic and publish sensor data to MQTT topics. The ESP32 doesn’t need to have access to a router via Wi-Fi, because it connects to the internet using a SIM card data plan.

Connect ESP32 to Cloud MQTT Broker TTGO T-Call ESP32 SIM800L Project overview and how it works

In a previous project, we created our own server with a database to plot sensor readings in charts that you can access from anywhere in the world.

In this project, we’ll publish sensor readings to a server via MQTT and we’ll use Mosquitto broker. You can publish your sensor readings to any other cloud broker.

In summary, here’s how the project works:

  1. The T-Call ESP32 SIM800L board is connected to the internet using a SIM card data plan.
  2. The T-Call ESP32 SIM800L board publishes the sensor readings via MQTT and the readings are displayed in Node-RED Dashboard.
  3. Through Node-RED Dashboard, you can press buttons to send on and off commands to control the ESP32 GPIOs.

We’ll be using a BME280 sensor, but you can use any other sensor that best suits your needs.

Cloud MQTT Broker

You can search for a free cloud MQTT broker, however we’ll be using our own cloud MQTT broker. We recommend using Digital Ocean with MQTT Mosquitto Broker, because it can handle all the project requirements. We’ll also use Node-RED software to visualize the readings in gauges and publish MQTT messages to the ESP32.

Prerequisites

1. ESP32 add-on Arduino IDE

We’ll program the ESP32 using Arduino IDE. So, you need to have the ESP32 add-on installed in your Arduino IDE. Follow the next tutorial, if you haven’t already.

2. Preparing your Cloud MQTT Broker

In this project we’ll show you how to exchange data via MQTT using a cloud MQTT broker.

We’ll be using our own cloud MQTT broker. However, if you find a free MQTT broker with all your desired features it will also work…

If you want to follow this exact project, you should follow the next two tutorials to prepare your own server and Node-RED software.

The ESP32 is publishing temperature readings every 30 seconds on the esp/temperature and esp/humidity topics. It’s subscribed to these two topics esp/output1 and esp/output2 to update the state of the output LEDs.

Having Node-RED running, go to your server IP address followed by :1880.

Wire your nodes as shown below:

ESP32 SIM800L GPRS GSM Cloud MQTT Broker Node-RED Arduino

Finally, deploy your flow (press the button on the upper right corner).

Alternatively, you can go to Menu Import and copy the following to your Clipboard to create your Node-RED flow.

3. SIM Card with data plan

To use the TTGO T-Call ESP32 SIM800L board, you need a nano SIM card with a data plan. We recommend using a SIM card with a prepaid or monthly plan, so that you know exactly how much you’ll spend.

4. APN Details

To connect your SIM card to the internet, you need to have your phone plan provider APN details. You need the domain name, username and password.

In my case, I’m using vodafone Portugal. If you search for GPRS APN settings followed by your phone plan provider name, (in my case its: “GPRS APN vodafone Portugal”), you can usually find in a forum or in their website all the information that you need.

I’ve found this website that can be very useful to find all the information you need.

It might be a bit tricky to find the details if you don’t use a well known provider. So, you might need to contact your provider directly.

5. Libraries

You need to install these libraries in your Arduino IDE to proceed with this project: Adafruit_BME280, Adafruit_Sensor, TinyGSM, and PubSubClient. Follow the next instructions to install these libraries.

Installing the Adafruit BME280 Library

Open your Arduino IDE and go to Sketch Include Library Manage Libraries. The Library Manager should open.

Search for “adafruit bme280 ” on the Search box and install the library.

Installing BME280 library in Arduino IDE

Installing the Adafruit Sensor Library

To use the BME280 library, you also need to install the Adafruit_Sensor library. Follow the next steps to install the library in your Arduino IDE:

Go to Sketch Include Library Manage Libraries and type “Adafruit Unified Sensor” in the search box. Scroll all the way down to find the library and install it.

Installing Adafruit Unified Sensor Driver library

Installing the TinyGSM Library

In the Arduino IDE Library Manager search for TinyGSM. Select the TinyGSM library by Volodymyr Shymanskyy.

Installing TinyGSM library

Installing the PubSubClient Library

Search for PubSubClient and scroll down. Select the PubSubClient library by Nick O’Leary.

Installing PubSubClient library in Arduino IDE

After installing the libraries, restart your Arduino IDE.

Parts Required

To build this project, you need the following parts:

You can use the preceding links or go directly to MakerAdvisor.com/tools to find all the parts for your projects at the best price!

Schematic Diagram

Wire the BME280 sensor and two LEDs to the T-Call ESP32 SIM800L board as shown in the following schematic diagram.

TTGO T-Call ESP32 SIM800L with BME280 sensor outputs wiring schematic diagram

We’re connecting the BME280’s SDA pin to GPIO 18 and the SCL pin to GPIO 19 .

Copy the following code to your Arduino IDE but don’t upload it yet. First, you need to make some modifications to make it work.

Before uploading the code, you need to insert your APN details, SIM card PIN (if applicable) and your cloud MQTT server details.

How the Code Works

Insert your GPRS APN credentials in the following variables:

In our case, the APN is internet.vodafone.pt . Yours should be different. We explained previously how to get your APN details.

Enter your SIM card PIN if applicable:

You also need to type the cloud MQTT server details in the following variables. It can be your own cloud MQTT domain or any other MQTT server that you want to use.

The ESP32 is subscribed to the esp/output1 and esp/output2 topics to update the outputs with the latest value:

The ESP32 publishes the temperature and humidity readings to these topics esp/temperature and esp/humidity every 30 seconds:

The code is heavily commented so that you understand the purpose of each line of code.

The following lines define the pins used by the SIM800L module:

Define the BME280 I2C pins. In this example we’re using GPIO 18 and GPIO 19 .

Define a serial communication for the Serial Monitor and another to communicate with the SIM800L module:

Configure the TinyGSM library to work with the SIM800L module.

Include the following libraries to communicate with the SIM800L.

And the MQTT library and the BME280 sensor libraries:

Instantiate an I2C communication for the SIM800L (battery power management IC).

And another I2C communication for the BME280 sensor.

Initialize a TinyGsmClient for internet connection.

setup()

In the setup() , initialize the Serial Monitor at a baud rate of 115200:

Start the I2C communication for the SIM800L module and for the BME280 sensor module:

Setup the SIM800L pins in a proper state to operate:

Initialize a serial communication with the SIM800L module.

Initialize the SIM800L module and unlock the SIM card PIN if needed.

Initialize the BME280 sensor.

Finally, in the setup() is where we’ll actually connect to the internet. The following lines connect the module to the internet:

mqttConnect()

The mqttConnect() function is responsible to connect your board to the MQTT broker using username and password. It also subscribes the ESP32 to the esp/output1 and esp/output2 MQTT topics.

In the loop() , there’s a timer that publishes the temperature and humidity readings to your MQTT broker every 30 seconds.

mqttCallback()

In the mqttCallback() function, the ESP32 receives the MQTT messages of the subscribed topics. Accordingly to the MQTT topic and message, it turns the outputs on or off:

Upload the Code

After inserting all the necessary details, you can upload the code to your board.

To upload code to your board, go to Tools Board and select ESP32 Dev module. Go to Tools Port and select the COM port your board is connected to. Finally, press the upload button to upload the code to your board.

Note: at the moment, there isn’t a board for the T-Call ESP32 SIM800L, but we’ve selected the ESP32 Dev Module and it’s been working fine.

Demonstration

Open the Serial Monitor at baud rate of 115200 and press the board RST button.

First, the module initializes and then it tries to connect to the internet. Please note that this can take some time (in some cases it took almost 1 minute to complete).

After connecting to the internet, it will connect to your MQTT broker.

ESP32 SIM800L connect to the internet using SIM card data plan demonstration Cloud MQTT broker

In this example, it publishes new sensor readings every 30 seconds, but for testing purposes you can use a shorter period of time.

Then, open a browser and type your server domain on the /ui URL. You should see the charts with the latest sensor readings and the toggle switches to control the outputs.

ESP32 SIM800L GPRS GSM Cloud MQTT Broker Node-RED Dashboard demonstration

Troubleshooting

If at this point, you can’t make your module connect to the internet, it can be caused by one of the following reasons:

  • The APN credentials might not be correct;
  • The antenna might not be working properly. In our case, we had to replace the antenna;
  • You might need to go outside to get a better signal coverage;
  • Or you might not be supplying enough current to the module. If you’re connecting the module to your computer using a USB hub without external power supply, it might not provide enough current to operate.

Wrapping Up

We hope you liked this project. In our opinion, the T-Call SIM800 ESP32 board can be very useful for IoT projects that don’t have access to a nearby router via Wi-Fi. You can connect your board to the internet quite easily using a SIM card data plan.


Источник: randomnerdtutorials.com