Streams-mqtt-gateway-master is source code for data streams to the tangle, you can download on https://github.com/iot2tangle/Streams-mqtt-gateway.
Setup: Fisrt, you have to download the folder
git clone https://github.com/iot2tangle/Streams-mqtt-gateway.git
Precise sensing is an IoT monitoring system to collect and measure air temperature, humidity, and pressure by using BME280 sensor, integrated with IOTA Tangle.
This project is to aim at how to use Arduino Nano 33 IoT WiFi as internet connectivity with IoT MQTT protocol standard. I wrote code in Arduino IDE and use Pubsubclient to send the data to the MQTT broker, using JSON packet data at ArduinoJson library. It has been successfully sent to raspberry Pi 3+ as an MQTT broker every 5 seconds.
Runnig the Gateway:
Run the streams-gateway:
cargo run --release
This starts the server which will forward messages from the devices to the Tangle
The Output will be something like this:
Starting.... Channel root: "47d504e1a825e142dd899dda81ff787c7cfad3b83977feec3545eaef4315c8a50000000000000000:fd93e57d937910f429cdd211" To read the messages copy the channel root into https://explorer.iot2tangle.io/ Listening for topic iot2tangle on http://localhost:1883
The Tech: apps infrastructure is a network sensor system consisting of the Nano 33 IoT (MCU) built-in/integrated with WiFi module as internet connectivity and raspberry 3+ as IoT edge device (MQTT-Streams) programmed in the Linux Operating System on computers, which is used to measure air temperature, humidity, and pressure on the environment monitoring system. The standard communication protocol (Machine to Machine) used is MQTT to send all of the data from sensors to the local server (MQTT-streams). Communication between edge-device raspberry, IOTA tangle network, and web-dashboard could be made remotely.
By using IOTA, the monitoring and control system is also directly connected and recorded in the cloud database. All data from the sensors connected to cloud computing is visualized in time-series data. The application process is used to manage all of the data in the database in order to access device id, location, production activity details, and local time. Real-time data of all processes can be visualized using either a mobile application or web dashboards. Streams-mqtt-gateway-master is source code for data streams to the tangle, you can download on https://github.com/iot2tangle/Streams-mqtt-gateway.
The pool server is a worker getting data from IOT2Tangle every 1 hour where the main node always performs to process the data from IoT2Tangle. https://explorer.iot2tangle.io/
#include <Arduino.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Wire.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
void initialize_wifi();
boolean reconnect();
void initialize_sensor();
void update_data();
struct dataSense
{
float temperature = 0.0F;
float humidity = 0.0F;
float pressure = 0.0F;
};
static char payload[512];
static dataSense data;
//StaticJsonDocument<256> doc;
const char ssid[] = "";
const char password[] = "";
WiFiClient net;
PubSubClient mqttClient(net);
const char mqtt_broker[] = "";
const char publish_topic[] = "iot2tangle";
#define CLIENT_ID ""
#define USERNAME ""
#define PASSWORD ""
long lastReconnectAttempt = 0;
Adafruit_BME280 bme;
long lastData = 0;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
initialize_sensor();
initialize_wifi();
mqttClient.setServer(mqtt_broker, 1883); //12143
lastReconnectAttempt = 0;
}
void loop() {
if (WiFi.status() != WL_CONNECTED)
{
initialize_wifi();
}
if (!mqttClient.connected())
{
long now = millis();
if (now - lastReconnectAttempt > 5000)
{
lastReconnectAttempt = now;
if (reconnect())
{
lastReconnectAttempt = 0;
}
}
} else
{
mqttClient.loop();
}
update_data();
}
void initialize_wifi(){
delay(100);
WiFi.disconnect();
Serial.println();
Serial.print("Firmware version: ");
Serial.println(WiFi.firmwareVersion());
Serial.print("Connecting WiFi to: ");
Serial.println(ssid);
while(WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, password);
Serial.print("Attempting WiFi connection .....");
if (WiFi.status() == WL_CONNECTED)
{
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
Serial.println("\nWiFi RSSI: ");
Serial.println(WiFi.RSSI());
digitalWrite(LED_BUILTIN, HIGH);
}
else
{
Serial.print("Failed to connect to WiFi");
Serial.println(", Try again in 5 seconds");
digitalWrite(LED_BUILTIN, LOW);
delay(5000);
}
}
}
boolean reconnect(){
Serial.println("Attempting to connect MQTT");
if (mqttClient.connect(CLIENT_ID, USERNAME, PASSWORD))
{
Serial.println("Connected to MQTT broker");
}
return mqttClient.connected();
}
void initialize_sensor()
{
Wire.begin();
unsigned status;
status = bme.begin();
if (!status)
{
Serial.println("Could not find a valid BME280.");
while(1) delay(10);
}
}
void update_data()
{
float t = bme.readTemperature();
float h = bme.readHumidity();
float p = bme.readPressure()/100.0F;
data.temperature = t;
data.humidity = h;
data.pressure = p;
const size_t capacity = JSON_ARRAY_SIZE(1) + JSON_ARRAY_SIZE(3) + 3*JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(2) + JSON_OBJECT_SIZE(3);
DynamicJsonDocument doc(capacity);
JsonArray iot2tangle = doc.createNestedArray("iot2tangle");
JsonObject iot2tangle_0 = iot2tangle.createNestedObject();
iot2tangle_0["sensor"] = "BME280";
JsonArray iot2tangle_0_data = iot2tangle_0.createNestedArray("data");
JsonObject iot2tangle_0_data_0 = iot2tangle_0_data.createNestedObject();
iot2tangle_0_data_0["temperature"] = data.temperature;
JsonObject iot2tangle_0_data_1 = iot2tangle_0_data.createNestedObject();
iot2tangle_0_data_1["humidity"] = data.humidity;
JsonObject iot2tangle_0_data_2 = iot2tangle_0_data.createNestedObject();
iot2tangle_0_data_2["pressure"] = data.pressure;
doc["device"] = "susukuda_device1";
doc["timestamp"] = "";
serializeJson(doc, payload);
long now = millis();
if (now - lastData > 5000)
{
lastData = now;
Serial.println(payload);
mqttClient.publish(publish_topic, payload);
}
}