Previously, we have seen how to interface the sensors with LattePanda 3 Delta and transfer the data to Node-Red also the data visualization. This article will guide you to transfer the sensor data to the cloud via MQTT.
For this tutorial, I'm going to use some of the previous and some new sensors.
Sensors used in this tutorial:
By using the below code we can connect these sensors with Node-Red.
#include <dht.h>
#include "Air_Quality_Sensor.h"
const int pinAdc = A1;
AirQualitySensor sensor(A2);
dht DHT;
#define DHT11_PIN 5
void setup() {
Serial.begin(115200);
if (sensor.init()) {
Serial.println("Sensor ready.");
} else {
Serial.println("Sensor ERROR!");
}
}
void loop() {
int quality = sensor.slope();
long sum = 0;
for (int i = 0; i < 32; i++)
{
sum += analogRead(pinAdc);
}
sum >>= 5;
int value = analogRead(A0);
int chk = DHT.read11(DHT11_PIN);
value = map(value, 0, 800, 0, 10);
Serial.print("Air Quality Sensor value: ");
Serial.println(sensor.getValue());
Serial.print("Air Quality Status: ");
if (quality == AirQualitySensor::FORCE_SIGNAL) {
Serial.println("High pollution! Force signal active.");
} else if (quality == AirQualitySensor::HIGH_POLLUTION) {
Serial.println("High pollution!");
} else if (quality == AirQualitySensor::LOW_POLLUTION) {
Serial.println("Low pollution!");
} else if (quality == AirQualitySensor::FRESH_AIR) {
Serial.println("Fresh air.");
}
Serial.print("Sound Sensor Value : ");
Serial.println(sum);
Serial.print("Light Sensor Value : ");
Serial.println(value);
Serial.print("Humidity Sensor Value : ");
Serial.println(DHT.humidity, 1);
Serial.print("Temperature Sensor Value : ");
Serial.println(DHT.temperature, 1);
delay(2000);
}
This code will simply sense the data and print it via serial port, But our need is JSON.
So, I have added the JSON transform codes in the above sensor interface code.
#include <dht.h>
#include "Air_Quality_Sensor.h"
#include <ArduinoJson.h>
const int pinAdc = A1;
AirQualitySensor sensor(A2);
dht DHT;
#define DHT11_PIN 5
void setup() {
// put your setup code here, to run once:
Serial.begin(115200);
if (sensor.init()) {
Serial.println("Sensor ready.");
} else {
Serial.println("Sensor ERROR!");
}
}
void loop() {
// put your main code here, to run repeatedly:
int quality = sensor.slope();
long sum = 0;
for (int i = 0; i < 32; i++)
{
sum += analogRead(pinAdc);
}
sum >>= 5;
int value = analogRead(A0);
int chk = DHT.read11(DHT11_PIN);
value = map(value, 0, 800, 0, 10);
// Serial.print("Air Quality Sensor value: ");
// Serial.println(sensor.getValue());
//
// Serial.print("Air Quality Status: ");
// if (quality == AirQualitySensor::FORCE_SIGNAL) {
// Serial.println("High pollution! Force signal active.");
// } else if (quality == AirQualitySensor::HIGH_POLLUTION) {
// Serial.println("High pollution!");
// } else if (quality == AirQualitySensor::LOW_POLLUTION) {
// Serial.println("Low pollution!");
// } else if (quality == AirQualitySensor::FRESH_AIR) {
// Serial.println("Fresh air.");
// }
//
// Serial.print("Sound Sensor Value : ");
// Serial.println(sum);
// Serial.print("Light Sensor Value : ");
// Serial.println(value);
// Serial.print("Humidity Sensor Value : ");
// Serial.println(DHT.humidity, 1);
// Serial.print("Temperature Sensor Value : ");
// Serial.println(DHT.temperature, 1);
StaticJsonDocument<200> doc;
doc["Temperature"] = DHT.temperature ;
doc["Humidity"] = DHT.humidity;
doc["Light LUX"] = value ;
doc["Sound"] = sum;
doc["Air Quality"] = sensor.getValue();
serializeJson(doc, Serial);
Serial.println();
delay(2000);
}
Here is the complete response.
Next, start the Node-Red and use the serial in node and try to get the Arduino core sensor data.
Here are the flow connections.
Once connected the nodes try to deploy and debug the nodes.
Next, let's add some visuals to see these in clearly.
Now all our data are inside the Node-Red, the next stage is to connect the Node-Red to the cloud. For this, we have to use MQTT nodes. You can see those nodes in the node pallets.
We can use the MQTT OUT node to send our data to the cloud.
Then open the mqtt out node's properties. We have to add our MQTT broker's credentials.
And click on the edit new mqtt broker.
Now we have to add our server details.
Qubitro Cloud Setup:In this, we are going to use Qubitro as a cloud service to store and transfer the data.
Login to Qubitro by creating a new account and open the portal.
Next, create a new project with all your details.
Once you created the project, and add the data source point, you can see multiple data routes. In this, we are going to use MQTT so, select the MQTT as a data point.
Finally, you will see the credentials. We are going to use these credentials to connect the Node-Red with Qubitro.
Use these configurations with the MQTT Out node.
Next, navigate to the security tab and enter the Device ID and token.
That's all. Now we are all good. Again go back to the MQTT Out node and there you can see the topic. Use the Qubitro Device-ID as mqtt topic here.
Finally, deploy the nodes. You can see the connected notification.
Navigate to the Qubitro portal and look at the device page, It will show you the all node-red data.
Also, Qubitro supports a good dashboard to visualize our data. Navigate to the Qubitro Dashboard and create a new dashboard.
Finally, if you want to share your data with the public means you can share via the public dashboard option in Qubitro.
Here is my sample demo dashboard.
So, now we know how to use sensors and MQTT with node Red, in the upcoming tutorial will build a complete automation system via LattePanda 3 Delta and Node-Red.
Comments