Connecting Sparkfun ESP32 Board and CCS811/BME280 Environmental Sensor with IoT Ensemble
In this tutorial, we will be taking a generic ESP32 board, connecting a simple environment sensor, and send messages with IoT Ensemble. For simplicity sake, I have chosen to use Sparkfun's "Qwiic" connect ecosystem, which is a plug-and-play method of connecting boards and sensors.
Things you will need
- SparkFun Thing Plus - ESP32 WROOM Board (You may also use a different generic ESP32 board, but it won't use the Qwiic system)
- A Qwiic Cable (Any length is fine for this tutorial)
- Your computer/laptop
- A Micro-USB cable to connect the ESP32 to your computer
- SparkFun Environmental Combo Breakout - CCS811/BME280 (Again, you may choose to use a more generic version of this sensor, but it won't use the Qwiic system)
- Arduino IDE installed on your computer
- A Fathym IoT Ensemble account (we’re using the free, shared version)
Part 1 - Hook Up Your Hardware
First, we need to attach the sensor to your board. Take the qwiic cable, and plug it into the ESP32 "Qwiic" port. Take the other end of the cable, and plug it into either of the two "Qwiic" ports on the Environmental Sensor. Your hardware is ready to go!
Part 2 - Installing Arduino IDE and Necessary Software
Next, we will need to install all of the required software/libraries on your computer
Installing Arduino IDE
Download your version of Arudino IDE here. Follow all of the steps in the wizard to complete installation (You can keep all of the default options the same)
Add Sensor Libraries
Once that is complete, we need to install some libraries. Click the following links to download each zip folder
Once you have downloaded those, go to your Arduino IDE screen. In the top toolbar, select Sketch -> Include Library -> Add .ZIP Library, as shown below:
This will open a file browser. Navigate to your downloaded package, select the zip folder, and click "Open". Repeat this step for the second package.
Add Additional Library
In the top toolbar navigate to Tools -> Manage Libraries. On the next screen, type "NTPClient" into the search bar. A library with the same name should show up in the list. Click "Install", and then "Close"
Add ESP32 Board Definition
In order for us to work with the ESP32, we need to add a board "definition".
Copy the following link:
Back in your Arduino IDE, in the top toolbar, click File -> Preferences. You will be taken to the follow screen:
Take the link from the previous step, and paste it into the "Additional Boards Manager URLs" field (highlighted above in red). Click "OK"
- Next, in the top toolbar, click Tools -> Board: "Name of Board" -> Boards Manager..., as shown below:
In the next screen, type "esp32" into the search bar. A board definition with the same name will appear, click "Install", then click "Close" (as shown below)
- Next, navigate to Tools -> Board: "Name of Board" -> ESP32 Arduino and select Adafruit ESP32 Feather, as shown below:
Determine Communication Port Number
Now that we have all the necessary libraries and dependencies, we need to tell Arduino IDE which port to use to communicate with your ESP32.
- Before plugging in the ESP32 to your computer, click Tools -> Port in the top toolbar of Arduino IDE. This will display a list of ports that are currently being used. Generally, there will only be one or two ports listed, depending on what you have plugged into your computer. Take note of the ports in this list. The picture below shows a list of ports before the ESP32 board is plugged in.
- Next, plug your ESP32 board directly into one of your computer's USB ports. After this, follow the previous step to view the list of available ports. You should now see an additional port that wasn't in the list before. This is the port that your ESP32 board is using. Click the port to select it. In the picture below, "COM3" is the port that is new in the list.
Get Code Onto your ESP32 board
Now that your Arduino can talk to your ESP32, it's time to put some code on your board!
First, copy the following code:
Next, in the ArduinoIDE, delete the existing template code. Then, paste the code you just copied.
Before we can continue, we need to register your ESP32 device with Iot Ensemble
Part 5 - Configuring IoT Ensemble
Before we can tell your device where to send data, we first need somewhere to send the data. There are a number of different ways this can be accomplished, with IoT Ensemble the focus is helping you leverage best practice cloud IoT technology. Here we'll be using the Azure IoT Hub to connect devices to a shared data flow, and then make it avaiable downstream for use in other applications.
Follow these steps to create a new device in IoT Ensemble. For more details on the full IoT Ensemble experience, check out our full documentation.
Start by navigating to the IoT Ensemble Dashboard and sign in or sign up. For the purposes of moving forward, you will only need the Free license and no credit card will be required.
Enroll a Device
In the Connected Devices section, click the Enroll New Device button, provide a name for your device (i.e. my-first-device) and click Enroll Device. That’s it! Your device is now registered and should be visible in the dashboard, along with its associated connection string.
Click on the button to copy your connection string to your clipboard. Your connection string should look something like this:
In addition to the whole connection string, there is one key part that we need: the YourDeviceID portion. This value needs to be a part of the data payload. Let's add them now.
Configure the Code
Back in the Arduino IDE, near the top of the code, your should see a section of four values that need to be filled in, like this:
First, fill in the WiFi name and password of the network you plan on using.
With this particular ESP32 board, it can only connect to 2.4 Ghz Wifi networks. The board CAN NOT connect to 5 Ghz networks. If you attempt to connect to a 5 Ghz network, this code will not work.
Next, take your connection string from Iot Ensemble, and paste it into the "connectionString" variable.
Finally, take the YourDeviceID portion of your connection string, and paste it into the "DeviceID" variable. Save your code file.
Verify and Upload Your Code
Now it is time to bring your ESP32 to life! In the top left corner of the Arduino IDE, click the "Verify" button, which looks like a checkbox (shown below)
This will compile your code, and ensure that your code has no errors like missing libraries or incorrect syntax.
Once this is complete, click on the "Upload" button, which looks like a horizontal arrow, and is right next to the "Verify" button (shown below)
This will take your code, and flash it to the ESP32 board. You will see some red text outputted to the terminal on the bottom of the screen. The toolbar will say "Done Uploading" once complete, and should look something like this:
Your ESP32 should now be taking sensor readings, and sending the information up to Iot Ensemble! If you want to see a live view of your code running, click Tools -> Serial Monitor in the top toolbar. You should be able to see your sensor readings every 30 seconds. In the Serial Monitor window, make sure that you have the baud rate set to "9600", as shown below:
Once you confirm that messages are sending correctly, you can now go to IoT Ensemble and see your messages in real time. Messages will appear under the "Device Telemetry" section, as shown below:
Just make sure that you have the Device Telemetry toggle set to "Enabled". For more information on Device Telemetry, check out our docs.
Hooking up the hardware is just the beginning of Iot Ensemble. There are a number of options for accessing and displaying your data easily.