Have you ever thought of using your ESP8266 ESP-01 WiFi Module as a standalone board for three (3) components namely DHT11 Sensor (for Temperature and Humidity), 1-Channel Relay Module and a Sound Sensor? Well, lucky for you, this is possible! Plus, there’s more: All these components are controlled and monitored via one of the IoT platforms available there: Cayenne IoT Platform.
Here is what we need:
- ESP8266 ESP-01 WiFi Module
- FTDI Breakout Board + Mini USB cable
- ESP8266 ESP-01 Breakout Board (Optional)
- 1-Channel Relay Module
- DHT11 Temperature and Humidity Sensor
- Sound Sensor
- Breadboards (I will be using 2 small breadboards)
- 5V to 3.3V Stepdown Buck Module (Optional)
- Plenty of jumper wires
Firstly, we need to study the pinouts for these components. Here are the main modules that we are going to use for this exercise. Please have time to study them, especially the pin assignments. You might have a different variant of the components as well, so please take note of the pin labels:
ESP8266 ESP-01 Wifie Module
We need to be careful since this board runs on a 3.3V logic. VCC (although I tried powering it with 5V and nothing happened) should be connected to 3.3V. Same goes to CH_PD. RX should be connected to TX of the FTDI breakout board, through a voltage divider. You can set the jumper in your FTDI Module to 3.3V if it has one. The FTDI module I am using gives you the option to choose between 5V and 3.3V. Just in case your power supply only provides 5V, refer to the voltage divider below. You may choose the correct resistors to provide ample power to your ESP8266 module.
1-Channel Relay Module

DHT11 Temperature and Humidity Sensor

Sound Sensor

FTDI USB to TTL Serial Converter

In the picture below, I have set the FTDI module to 3.3V by changing the set up of its jumper:
When the 1st and 2nd pins are connected, the FTDI module will provide a 5-volt power (same goes with TX pin as well). When the 2nd and 3rd pins are connected, it will provide 3.3-vols power (same with TX pin as well).
5V to 3.3V Stepdown Power Supply Buck Module (OPTIONAL)

Before proceeding with coding, we need to have the following set up in Arduino IDE. For the libraries, download them from the link provided for each library (see below) at Github.
- ESP8266 Boards – For step by step instructions, please see next section.
- SimpleDHT Libraries – SimpleDHT libraries can be downloaded here.
- SimpleTimer Libraries – Simple Libraries can be downloaded here.
ESP8266 Boards Set Up in Arduino IDE
Setting up the ESP8266 boards is quite easy. Please follow the steps below:
- Open your Arduino IDE.
- Go to File > Preferences.
- From the Additional Boards Manager URLs text field, place this URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json. Click OK afterwards.
- Now, go to Tools > Board > Boards Manager.
- In Boards Manager, search for esp8266, and install the esp8266 by ESP8266 Community Version x.x.x. The close the Boards Manager window.
- Go to Tools > Board then scroll through the list, until you find Generic ESP8266 Module.
Adding Required Libraries
- Go to the link provided above for the two (2) required libraries (SimpleDHT and SimpleTimer.
- For each link, click Clone or Download. A small window appears, then click Download Zip. Refer to the images shown below:
- Save the ZIP file for each library.
- In the Arduino IDE, go to Sketch > Include Library > Add .Zip Library.
- Locate and add each library (one at a time).
Adding the Cayenne Libraries
Cayenne Libraries can be installed to the Arduino IDE via the Library Manager. To do this, follow the steps below:
- In the Arduino IDE, go to Sketch > Include Library. Click Manage Libraries.
- In the windows that will appear, type Cayenne in the search field. Install the Cayenne by myDevices Version x.x.x and CayenneMQTT by myDevices Version x.x.x. Close the window afterwards. These libraries are also available at Github (Cayenne and CayenneMQTT libraries).
Programming Mode
Initially, we need to set up our ESP8266 ESP-01 to programming mode. We don’t need to connect our components yet, as connecting them might cause the uploading of the sketches to fail. The table below details what pins are connected to each other between the ESP8266 ESP-01 WiFi module and the FTDI USB to TTL Serial Converter:
You need to set the jumper of the FTDI module to 3.3V. This way, we won’t be needing any voltage divider or the step down buck module. Your USB to Serial Converter might have different pin assignments. The above description is demonstrated in the schematic below:
That’s it! We are now ready to set up our dashboard and upload the sketches:
Proceed to the next section below, after successfully
Create the Cayenne Dashboard
- Log on to your Cayenne account (register for FREE if you haven’t one yet at Cayenne Signup Page):
- Once logged in, the next step would be to choose the device we are going to use.If this is your first device in your dashboard, choose All Devices:
The, choose Generic ESP8266 under Microcontrollers:
If you already have existing devices added in previously, you need to add a device by clicking the Add Devices/Widget menu:
Then choose ESP8266 since this is our development board:
- In the succeeding page, you are shown with the MQTT Username, MQTT Password, and Client ID. We need these details in the sketch later. You will also see at the bottom portion of the page that it is waiting for the device to connect.
- Now, we proceed with the Sketch. Open the Arduino IDE. Go to File > Examples > Cayenne-MQTT-ESP8266 > ESP8266. This sketch will be our base sketch.
- Go back to the Cayenne Portal. Take note of the following, because we are going to use it in our sketch:
- MQTT USERNAME
- MQTT PASSWORD
- CLIENT ID
- Now, go back to the Arduino IDE. Paste the values in the section highlighted in the screenshot below:
- Also, supply the WiFi name (SSID) and password in this section of the code:
- By the way, we need to include the needed libraries: SimpleDHT and SimpleTimer. The SimpleDHT will allow us to use the DHT Temperature. The SimpleTimer will allow us to execute methods outside the loop() method:
- We now need to declare the pins to be used by the sensors.
- DHT11 Pin = Pin 2 (GPIO2 of ESP8266 ESP-01)
- Sound Pin = Pin 3 (GPIO3 of ESP8266 ESP-01)
- Relay Pin = Pin 1 (GPIO1 of ESP8266 ESP-01)
- Since we are going to send data to the Cayenne IoT platform using MQTT API, we need to pass the sensor value to the virtual pins. The virtual pins will be used by the widgets in our dashboard. We are not going to use V1, because we assigned this as an output (to control the relay – used in the Cayenne dashboard widgets):
- V2 – Humidity (data from DHT11)
- V3 – Temperature (data from DHT11)
- V4 – Sound (data from Sound Sensor)
- Upload the sketch to the ESP8266 ESP-01 Board. Be sure that the chosen board is Generic ESP8266 Module:
Complete Code
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data. // Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling. //#define CAYENNE_DEBUG #define CAYENNE_PRINT Serial #include <CayenneMQTTESP8266.h> #include <SimpleTimer.h> // Download from https://github.com/jfturcot/SimpleTimer #include <SimpleDHT.h> // Download from https://github.com/adafruit/DHT-sensor-library // WiFi network info. char ssid[] = "<your ssid/wifi name>"; char wifiPassword[] = ""<your ssid/wifi password>""; // Cayenne authentication info. This should be obtained from the Cayenne Dashboard. char username[] = "<your MQTT Username>"; char password[] = "<your MQTT Password>"; char clientID[] = "<your client id>"; // DHT11 Pin int pinDHT11 = 2; SimpleDHT11 dht11; // Sound Pin int soundPin = 3; // Relay Pin int relayPin = 1; // Timer SimpleTimer timer; void setup() { Serial.begin(9600); Cayenne.begin(username, password, clientID, ssid, wifiPassword); pinMode(relayPin, OUTPUT); // Relay digitalWrite(relayPin, HIGH); pinMode(soundPin, INPUT); // Sound timer.setInterval(200L, transmitData); // Method to execute every 200ms } void loop() { Cayenne.loop(); timer.run(); } CAYENNE_IN(relayPin) { if (getValue.asInt() == 1) { // NOTE: Channel = Virtual Pin digitalWrite(relayPin, LOW); } else { digitalWrite(relayPin, HIGH); } } void transmitData() { byte temperature = 0; byte humidity = 0; int err = SimpleDHTErrSuccess; if ((err = dht11.read(pinDHT11, &temperature, &humidity, NULL)) != SimpleDHTErrSuccess) { Cayenne.virtualWrite(V4, 0); Cayenne.virtualWrite(V2, 0); } else { Cayenne.virtualWrite(V4, (int)temperature); Cayenne.virtualWrite(V2, (int)humidity); } if (digitalRead(soundPin) == HIGH) { Cayenne.virtualWrite(V3, HIGH); } else { Cayenne.virtualWrite(V3, LOW); } }
Connecting the Sensors and Modules
At this stage, you should be able to upload your sketch successfully. Now, remove the board from power. Also, remove the connection GND from GPIO0. We don’t need GPIO0 connected to GND, unless we are going to update the sketch. Refer to the tables for each sensor and module below:
All these components’ power supplies are connected to the output voltage of the step down module, meaning they are powered via 3.3V. Main power supply is the FTDI breakout board. The schematic below shows the complete connections, after successfully uploading the sketch:
NOTE: The RX and TX pins of the FTDI module aren’t connected to the TX and RX pins of the wifi module, and so is GPIO0 (from GND).
The image below is the actual set up of the schematics shown above:
That’s it! We’re done with these setups: Boards, Libraries and Components. We are now ready to configure our project with Cayenne using MQTT. By the way, MQTT stands for MQ Telemetry Transport. Simply speaking, it is a messaging transport protocol between machines, and Internet of Things! More on this at Cayenne Docs.
Power the board once again. The ESP8266 ESP-01 Wifi Module should be able to connect to your Wifi. Now, go back to the Cayenne portal. You should be able to see the dashboard with pre-populated initial widgets which you can add to the dashboard You may now customize the dashboard.
Initially, this is how the dashboard looks like when the ESP8266 ESP-01 WiFi module starts to send data to the Cayenne Dashboard (under the configured device there):
Add the initial widgets shown above, by clicking the “+” icon at the upper right hand corner of each initial widget:
Once added, we will now customize each widget. This is how it looks like when these initial widgets are added:
To customize each widget, click on the right-most icon, then select Settings. Refer to screenshot below:
A pop-up window appears, and will allow us to change the Widget Name, Channel, Widget Icon and Number of Decimal Places. We will leave Channel as it is. Don’t change the value of Channel, since the value here is the Virtual Pin (V0, V1, V2…) used in the code for this widget. Virtual Pin 4 (V4) is the temperature, Virtual Pin 2 (V2) is the humidity, and Virtual Pin 3 (V3) is the value of the sound sensor (0/1).
Below are the configuration/setting for each initial widget:
Here is how they look afterwards:
Now, we still need to add one more widget. This widget is for the relay, to turn it on/off. On the left side portion of the dashboard (upper-left), click Add New, then click Device/Widget:
Scroll down further down, and look for Custom Widgets. Select Button:
We need to change the settings for the Button widget:
- Name = Relay Switch
- Device = Generic ESP8266 (or your named device if you have already)
- Data = Digital Actuator
- Unit = Digital (0/1)
- Channel = 1 (this is GPIO1 of the ESP8266 ESP-01 WiFi Module – review code)
- Choose Icon = Button (you may choose what icon you desire)
Below is the screenshot for the Button settings. Click Add Widget afterwards:
The final dashboard now looks like this:
And we’re DONE! Now, try to switch on/off the Relay Switch. The relay connected to the ESP8266 ESP-01 WiFi Module will turn on or off – you will hear clicking sounds. Try to make some noise. The sound widget will register 1 (if noise is detected, 0 if not). Try observing the Temperature and Humidity widgets – these values change according to what are “sensed” from where the DHT11 module is.
We’ve just used the Cayenne MQTT API to connect our device with components attached to it.
Comments, suggestions and even corrections are very much welcome.
This article serves as a documentation for myself, and for others who might want to try Cayenne myDevices as their IoT platform of choice.
Thank you very much.