Project tutorial
MQTT Protocol with ThingsBoard Cloud Using Arduino MKR1010

MQTT Protocol with ThingsBoard Cloud Using Arduino MKR1010 © GPL3+

Using MQTT protocol with Arduino MKR1010.

  • 82 views
  • 0 comments
  • 2 respects

Components and supplies

About this project

This project is aimed to use MQTT protocol IoT with an Arduino MKR1010 WiFi. It was made by programming with 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.

A network sensor system consisting of an 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.

Code

MQTT MKR1010Arduino
#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);
  }

}

Schematics

MKR1010 Sensors
Mkr tml5p80xtc

Comments

Similar projects you might like

Your Environmental Data on Arduino IoT Cloud

Project tutorial by 3 developers

  • 16,217 views
  • 25 comments
  • 59 respects

BLE Sharp Dust Sensor - ARTIK IoT Cloud

Project tutorial by Elijah Scheele

  • 5,980 views
  • 1 comment
  • 20 respects

Arduino IoT Cloud Amazon Alexa Integration

Project tutorial by 4 developers

  • 13,827 views
  • 15 comments
  • 60 respects

Arduino Cloud Sensor Tower

Project tutorial by Arduino_Genuino

  • 34,065 views
  • 5 comments
  • 118 respects

Octopod: Smart IoT Home/Industry Automation Project

Project tutorial by Saksham Bhutani

  • 13,687 views
  • 11 comments
  • 44 respects
Add projectSign up / Login