Ahmad Radhy
Published © GPL3+

MQTT Protocol with ThingsBoard Cloud Using Arduino MKR1010

Using MQTT protocol with Arduino MKR1010.

IntermediateFull instructions provided82
MQTT Protocol with ThingsBoard Cloud Using Arduino MKR1010

Things used in this project

Hardware components

DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Integrated Ambient Light Sensor, I2C
Integrated Ambient Light Sensor, I2C
×1
Arduino MKR WiFi 1010
Arduino MKR WiFi 1010
×1

Story

Read more

Schematics

MKR1010 Sensors

Code

MQTT MKR1010

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

}

Credits

Ahmad Radhy

Ahmad Radhy

11 projects • 9 followers
I'm founder CeriTech that is coffee processing technology, now taking master degree in Physics Department Institut Teknologi Bandung (ITB).

Comments