This project is aimed to use MQTT protocol IoT with Arduino MKR1010 WiFi.

This project is aimed to use MQTT protocol IoT with Arduino MKR1010 WiFi. it was made by programming arduino IDE, PubSubClient and WiFiNINA library. We can send the data to the ThingsBoard cloud in real-time. The data can be visualized in Thingsboard.

Using MQTT Protocol with Arduino MKR1010

A network sensor system consisting of Arduino MKR1010 WiFi module as internet connectivity and used to measure air temperature, and humidity using DHT22 sensor module, light intensity using BH1750 module. Data transmission used in a communication protocol is Message Queuing Telemetry Transport (MQTT) using pubsubclient library in which sensors are connected to the Thingsboard cloud.

IMG_20200217_060944.jpg
        #include <PubSubClient.h>
#include <SPI.h>
#include <WiFiNINA.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <Wire.h>
#include <BH1750FVI.h>

#define TOKEN ""
#define DHTPIN 5
#define DHTTYPE DHT22

const char* ssid = "";
const char* password = "";
const char mqtt_server[] = "demo.thingsboard.io"; 
const char publishTopic[] = "v1/devices/me/telemetry"; 

WiFiClient mkr1010Client;
PubSubClient client(mkr1010Client);
long lastData = 0;
DHT dht(DHTPIN, DHTTYPE);
BH1750FVI lightMeter(BH1750FVI::k_DevModeContLowRes);

void setup_wifi(){

  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);

  while( WiFi.status() != WL_CONNECTED){

    delay(500);
    Serial.print(".");   
  }

  randomSeed(micros());
  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
 
}

void reconnect(){

  while(!client.connected()){
    
    Serial.print("Attempting MQTT connection ....");

    if (client.connect("ClientID", TOKEN, NULL)) { 
      
      Serial.println("Connected to MQTT Broker");
      digitalWrite(LED_BUILTIN, HIGH);
    }

    else
    {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println("try again in 5 second");
      digitalWrite(LED_BUILTIN, LOW);
      delay(5000);
    }
    
    
  }
  
}

void setup() {

  pinMode(5, OUTPUT);
  pinMode(LED_BUILTIN, OUTPUT);
  Wire.begin(); //(SDA, SCL);
  lightMeter.begin();
  Serial.begin(9600);
  dht.begin(); 
  setup_wifi();
  client.setServer(mqtt_server, 1883);

}

void loop() {

  if (!client.connected()) {
    reconnect();
  }

  client.loop();

  float t = dht.readTemperature();
  float h = dht.readHumidity();
  uint16_t l = lightMeter.GetLightIntensity();
  
  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor");
    return;
  }

  String temperature = String(t);
  String humidity = String(h);
  String intensity = String(l);
  
  String payload = "{\"temperature\":";
  payload += temperature;
  payload += ",\"humidity\":";
  payload += humidity;
  payload += ",\"light_intensity\":";
  payload += intensity;
  payload += "}";

  char attributes[1000];
  long now = millis();

  if(now - lastData > 5000){

    lastData = now;
    payload.toCharArray(attributes, 1000);
    client.publish(publishTopic, attributes);
    Serial.println(attributes);
  }

}
    
Ahmad Radhy
I am Founder of CeriTech that is a IoT-based solution , now taking master degree in Physics Department Institut Teknologi Bandung (ITB)

Tags