The ESP8266 Core and Wi-Fi module (xChip CW01) allows users to send data from XinaBox’s modular xChips to the cloud. This data can be monitored remotely in Ubidots, where users can take advantage of their range of IoT tools.
The xChip SW01 Advanced Weather Sensor (Bosch BME280) measures temperature, humidity and atmospheric pressure, from which altitude, cloud base and dew point can also be calculated.
In this tutorial we use HTTP protocol to send sensor data to Ubidots. This could also be done using MQTT protocol.
By the end of this guide, you will able to monitor and measure weather conditions at your XinaBox device from anywhere remotely using Ubidots.
Requirements
Steps-by-Step
Hardware Setup
Setting up the Arduino IDE
Understanding the code
Login to Ubidots
Create Dashboards in Ubidots
Summary
1.Hardware Setup
Connect CW01, SW01 and IP01 together using the XC10 xBUS connectors. You may connect it as shown in the diagram below. Please see this guide on how to assemble xChips generally.
Then, connect your device and PC through the IP01’s USB. For this, you will need to use the xFlasher software to flash the code once ready. See this guide on using the xFlasher.
2. Setting up the Arduino IDE
1. Install Arduino IDE 1.8.8
2. Install these libraries to Arduino: ESP8266 Arduino, Ubidots ESP8266, xCore, xSW01.
NOTE: If you are not familiar with how to Install libraries, please refer to the link: Installing Arduino libraries
3. With the ESP8266 platform installed, select the ESP8266 device you are working with. In the case, we are working with a “CW01(ESP12F module)”. To select your board from the Arduino IDE, select Tools > Board “NodeMCU 1.0(ESP12E module)”.
NOTE: ESP12F and ESP12E are interchangeable for this purpose.
3. Understanding the code
Including libraries:
#include "UbidotsESPMQTT.h"
#include <xSW01.h>
#include <xCore.h>
Enter your Wi-Fi and Ubidots Credentials:
#define TOKEN "Your-Token" // Put here your Ubidots TOKEN
#define WIFISSID "Your-SSID" // Put here your Wi-Fi SSID
#define PASSWORD "password-of-ssid" // Put here your Wi-Fi password
Your unique Ubidots TOKEN is obtained from your Ubidots account. Refer to the following link to learn where to find your Ubidots TOKEN.
Define the Ubidots device and variables labels:
#define DEVICE_LABEL "xinabox-cw01-sw01" // Ubidots Device Label
#define VARIABLE_LABEL_1 "altitude" // Ubidots Variables Label
#define VARIABLE_LABEL_2 "pressure"
#define VARIABLE_LABEL_3 "humidity"
#define VARIABLE_LABEL_4 "temperature-c"
#define VARIABLE_LABEL_5 "temperature-f"
One time setup, see the comments for self-explanation:
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Pass a true or false bool value to activate debug messages
client.setDebug(true);
// Connect to the Access Point
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
// Set the I2C Pins for CW01
#ifdef ESP8266
Wire.pins(2, 14);
Wire.setClockStretchLimit(15000);
#endif
// Start the I2C Comunication
Wire.begin();
// Start the SW01 Sensor
SW01.begin();
//Delay for sensor to normalise
delay(5000);
}
Loop the operation, to keep it running and updating continuously:
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
}
// Read and calculate data from SW01 sensor
SW01.poll();
// Request SW01 to get the altitude measurement and store in
// the altitude variable
float altitude = SW01.getAltitude(101325);
// Request SW01 to get the pressure measurement and store in
// the pressure variable
float pressure = SW01.getPressure();
// Request SW01 to get the humidity measurement and store in
// the humidity variable
float humidity = SW01.getHumidity();
// Request SW01 to get the temperature measurement and store in
// the temperature variable
float tempC = SW01.getTempC(); // Temperature in Celcuis
float tempF = SW01.getTempF(); // Temperature in Farenheit
// Display the recoreded data over the Serial Monitor
Serial.print("Altitude: ");
Serial.print(altitude);
Serial.println(" m");
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" Pa");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" C");
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" F");
// Save variables to be sent to Ubidots
client.add(VARIABLE_LABEL_1, altitude);
client.add(VARIABLE_LABEL_2, pressure);
client.add(VARIABLE_LABEL_3, humidity);
client.add(VARIABLE_LABEL_4, tempC);
client.add(VARIABLE_LABEL_5, tempF);
// Publish data to Ubidots
client.ubidotsPublish(DEVICE_LABEL);
client.loop();
// Loop delay
delay(5000);
}
The complete code:
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
#include <xSW01.h>
#include <xCore.h>
/****************************************
* Define Constants
****************************************/
#define TOKEN ".........." // Your Ubidots TOKEN
#define WIFINAME ".........." //Your SSID
#define WIFIPASS ".........." // Your Wifi Pass
#define DEVICE_LABEL "xinabox-cw01-sw01" // Ubidots Device Label
#define VARIABLE_LABEL_1 "altitude" // Ubidots Variables Label
#define VARIABLE_LABEL_2 "pressure"
#define VARIABLE_LABEL_3 "humidity"
#define VARIABLE_LABEL_4 "temperature-c"
#define VARIABLE_LABEL_5 "temperature-f"
Ubidots client(TOKEN);
xSW01 SW01;
/****************************************
* Auxiliar Functions
****************************************/
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i=0;i<length;i++) {
Serial.print((char)payload[i]);
}
Serial.println();
}
/****************************************
* Main Functions
****************************************/
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
// Pass a true or false bool value to activate debug messages
client.setDebug(true);
// Connect to the Access Point
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
// Set the I2C Pins for CW01
#ifdef ESP8266
Wire.pins(2, 14);
Wire.setClockStretchLimit(15000);
#endif
// Start the I2C Comunication
Wire.begin();
// Start the SW01 Sensor
SW01.begin();
//Delay for sensor to normalise
delay(5000);
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
}
// Read and calculate data from SW01 sensor
SW01.poll();
// Request SW01 to get the altitude measurement and store in
// the altitude variable
float altitude = SW01.getAltitude(101325);
// Request SW01 to get the pressure measurement and store in
// the pressure variable
float pressure = SW01.getPressure();
// Request SW01 to get the humidity measurement and store in
// the humidity variable
float humidity = SW01.getHumidity();
// Request SW01 to get the temperature measurement and store in
// the temperature variable
float tempC = SW01.getTempC(); // Temperature in Celcuis
float tempF = SW01.getTempF(); // Temperature in Farenheit
// Display the recoreded data over the Serial Monitor
Serial.print("Altitude: ");
Serial.print(altitude);
Serial.println(" m");
Serial.print("Pressure: ");
Serial.print(pressure);
Serial.println(" Pa");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.println(" %");
Serial.print("Temperature: ");
Serial.print(tempC);
Serial.println(" C");
Serial.print("Temperature: ");
Serial.print(tempF);
Serial.println(" F");
// Save variables to be sent to Ubidots
client.add(VARIABLE_LABEL_1, altitude);
client.add(VARIABLE_LABEL_2, pressure);
client.add(VARIABLE_LABEL_3, humidity);
client.add(VARIABLE_LABEL_4, tempC);
client.add(VARIABLE_LABEL_5, tempF);
// Publish data to Ubidots
client.ubidotsPublish(DEVICE_LABEL);
client.loop();
// Loop delay
delay(5000);
}
4. Login to Ubidots
Open your Ubidots account. You will see a device named “xinabox-cw01-sw01” with 5 variables (see image below).
NOTE: If you desired to send a different device name you must update the device label assigned at the beginning of the code.
Device visualization
Variables visualization
5. Creating Dashboards in Ubidots
Dashboards (static and dynamic) are user interfaces to organize and present a device's data and the insights derived from the data. Dashboards contain widgets that display the data as charts, indicators, controls, tables, graphs, and other size, shapes, and forms.
To create a new Dashboard in your Ubidots account, refer to the following Ubidots tutorial to learn how to do it.
Just as a reference, once your Ubidots Dashboard is created you should have something similar to the image below:
PRO TIP: There are also a range of graphing and reporting tools. If you want to learn more about this, we recommend you check this guide.
6. Summary
In this tutorial, we have shown how to code and connect an XinaBox Weather station to Ubidots. This enables remote monitoring and can be completed within 10-15 minutes.
Other readers have also found useful...