Control and Automation with Ubidots
This tutorial is a subset of the previous tutorial: HVAC Monitoring and Control with a Siemens IoT2040. Building upon previous application setup, it is now time to implement your desired automation logic. To create autonomous devices and process in your application we will use a GET request with Ubidots conditional and complex Events Engine.
1. Event Setup
An Ubidots Event is a triggered action (or alert) when a variable's data fulfills or exceeds a design rule. For this tutorial we will be using the temperature variable to activate/deactivate the device associated with the variable labeled "fan." Then using a GET request, when the fan variable is activated a message is sent to our device controlling the physical fan in the HVAC system.
1. From the Device section of your Ubidots account, select the device you're working with and add a Default Variable named “fan.”
2. With the raw variable now available to receive data, go to the Event section, and select the plus icon to create a new event.
3. Follow the intuitive on screen setup to configure the variable temperature ºF to send an event when the variable value of temperature (ºF) is greater than or equal to 84:
If the variable value of “temperature (ªF)” is greater than or equal to 84.
If your application requires additional and/or conditional logic, check out this simple guide for some additional assistance.
4. Save the changes press the blue right icon at the central-end of the page:
6. Next, select the orange "plus" icon to create the desired triggered action.
7. Select “SetVariable” as the action.
8. Then, select the variable to be set which is “fan” (the last variable created) and assign the value “1” as shown below:
9. Save all changes with the green "check" icon
10. Repeat steps 1-9 to program an event to turn OFF the "fan" variable. Our applications assigns the below logic:
If triggers: If variable temperature(ºF) is less than or equal to 80 ºF.
Then action: Set variable Fan to 0.
Pro Tip: to know when the events was triggered or not, reference your event logs.
2.Create a GET request from Ubidots to control a device.
The events configured in the previously step are setting a value (1 or 0) in the fan variable. Next, we will use the values posted in the fan variable to send a GET to a device containing a relay attached motor on the physical fan.
Any device that you are familiar with can be used activate the physical fan. Many industrial HVAC systems contain embedded relays to activate process making this step even simpler.* As a simple example we decided to use a NodeMCU ESP8266 linked to a DC Motor, but feel free to choose the hardware most familiar to you and able for your application.
NOTE: If you are going to use another device to receive the GET request, please reference to the Ubidots REST API Reference or MQTT documentation for additional resources.
8.1. NodeMCU ESP8266 Hardware Setup
1. Follow the table below to establish the wire connection between the NodeMCU and a DC motor.
NOTE: Please note that direction of the fan is correlative to the cable positioning. If the cables are reversed, the fan will spin in the opposite direction.
8.2. Firmware Setup
1. Begin with the NodeMCU ESP8266 setup instructions with the Arduino IDE and install the libraries required. Once you are able to run the example properly and received the right behavior using Ubidots, please refer back to this guide.
2. From the Arduino IDE, copy and paste the following code in the Arduino IDE and assign your Ubidots TOKEN and WiFi credentials where indicated.
/****************************************
* Include Libraries
****************************************/
#include "UbidotsESPMQTT.h"
/****************************************
* Define Constants
****************************************/
#define TOKEN "xxxxxx" // Your Ubidots TOKEN
#define WIFINAME "xxxxxx" //Your SSID
#define WIFIPASS "xxxxxx" // Your Wifi Pass
Ubidots client(TOKEN);
int fan = D6;
/****************************************
* 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]);
}
if ((char)payload[0]=='1'){
digitalWrite(fan, HIGH);
}else{
digitalWrite(fan, LOW);
}
Serial.println();
}
/****************************************
* Main Functions
****************************************/
void setup() {
// put your setup code here, to run once:
client.ubidotsSetBroker("business.api.ubidots.com"); // Sets the broker properly for the business account
client.setDebug(false); // Pass a true or false bool value to activate debug messages
Serial.begin(115200);
pinMode(fan, OUTPUT);
client.wifiConnection(WIFINAME, WIFIPASS);
client.begin(callback);
client.ubidotsSubscribe("siemens","fan"); //Insert the dataSource and Variable's Labels
}
void loop() {
// put your main code here, to run repeatedly:
if(!client.connected()){
client.reconnect();
client.ubidotsSubscribe("siemens","fan"); //Insert the dataSource and Variable's Labels
}
client.loop();
}
3. Verify your code with the "check-mark" icon in the top left corner of the Arduino IDE.
4. Upload the code into your NodeMCU ESP8266 with the "right-arrow" icon beside the check-mark icon.
With your code uploaded and your device in place, you now have a, autonomous system triggered by your programmed Events in Ubidots. When the variable “Temperature(ºF)” increase to 84 ºF, the fan will turn ON. When the temperature decrease to 80 ºF the fan will turn OFF, ensuring the rooms never gets too warm.
For additional insights to the Siemens IoT2040 device and Ubidots position in the Industrial IoT Landscape, check out the April 10th, Webinar: Data Drives Decisions; how to extract value from the IIoT and the Cloud.