Green Robot MachineryVIBHAV
Published

Temperature Logging Using MQTT and MongoDB

Monitor temperature from LinkIt, communicate it to node.js server over MQTT, then finally log it into a MongoDB database.

IntermediateFull instructions provided8 hours27,462
Temperature Logging Using MQTT and MongoDB

Things used in this project

Hardware components

DHT11 Temperature & Humidity Sensor (4 pins)
DHT11 Temperature & Humidity Sensor (4 pins)
×1

Software apps and online services

Mosquitto Broker
OpenSSL DLLs
PthreadVC2 DLL
node.js
MongoDB

Story

Read more

Schematics

IMG_20160310_125627.jpg

Code

Linkit Code

C/C++
Code that runs in Linkit that monitors temperature and sends the value to Node.js server using MQTT
/*************************
 * Green Robot Machinery
 * Linkit Code that senses temperature and publishes to MQTT broker
 * **********************/

#include <SPI.h>        //includes the Serial Peripheral Interface library
#include <PubSubClient.h>    // A client library that provides support for MQTT
#include <dht11.h>      // library for DHT11 Temperature and Humidity sensor

#include <HttpClient.h>   // library for posting HTTP requests


// MediaTek LinkIt libraries for WiFi connectivity
#include <LTask.h>    
#include <LWiFi.h>
#include <LWiFiClient.h>




#define WIFI_AP "WIFI_NAME"    // write your WiFi name in between double quotes
#define WIFI_PASSWORD "WIFI_PASSWORD"  // write the WiFi password in between double quotes
#define WIFI_AUTH LWIFI_WPA  // choose from LWIFI_OPEN, LWIFI_WPA, or LWIFI_WEP.


LWiFiClient wifiClient;  
byte localserver[] = {192, 168, 43, 19 };  //this is ip address of the machine where the mqtt broker is running



String clientName = String("d:quickstart:arduino:");  //client name can be of your choice
String topicName = String("demo/device/");   //similarly topic name can be of your choice but make sure the same topic name is used in the node.js program
dht11 DHT11; // activate the Temp and Humidity sensor
float tempF = 0.0;  // initialise a variable for Temperature in Fahrenheit
float tempC = 0.0;  // initialise a variable for Temperature in Celsius
float humid = 0.0; // initialise a variable for Humidity

PubSubClient client(localserver, 1883, 0, wifiClient); //communicating the LinkIt One with the mosquitto broker (port no. 1883) running on the localserver, LinkIt One communicates to the broker via WiFi hence the WiFiClient  

void setup()
{
    Serial.begin(115200);
    DHT11.attach(3);  //Temp and Humidity sensor is attached to the digital pin 3
  
    // activating the WiFi module of the LinkIt One Board  
    LTask.begin();
    LWiFi.begin();
    Serial.println("Temperature sensor on...");
    delay(3000);
    while(!Serial) delay(1000);
    Serial.println("Connecting to AP");
    while (0 == LWiFi.connect(WIFI_AP, LWiFiLoginInfo(WIFI_AUTH, WIFI_PASSWORD)))
                              {
                                delay(1000);
     }
  Serial.println("calling connection");
}


void loop()
{

  char clientStr[34];   
  clientName.toCharArray(clientStr,34);
  char topicStr[26];
  topicName.toCharArray(topicStr,26);
  
getData();

//checking for client connections
  if (!client.connected()) {
    Serial.print("Trying to connect to: ");
    Serial.println(clientStr);
    client.connect(clientStr);
  }
  if (client.connected() ) {
    String json = buildJson();
    char jsonStr[200];
    json.toCharArray(jsonStr,200);
    boolean pubresult = client.publish(topicStr,jsonStr);
    Serial.print("attempt to send ");
    Serial.println(jsonStr);
    Serial.print("to ");
    Serial.println(topicStr);
    if (pubresult)
      Serial.println("successfully sent");
    else
      Serial.println("unsuccessfully sent");
  }
  delay(5000);
}



// sending data(Temperature in Celsius and Fahrenheit and also the humidity level) to mqtt server running on port 1883 using ‘json’ format

String buildJson() {
  String data = "{";
  data+="\n";
  data+= "\"d\": {";
  data+="\n";
  data+="\"myName\": \"Arduino DHT11\",";
  data+="\n";
  data+="\"temperature (F)\": ";
  data+=(int)tempF;
  data+= ",";
  data+="\n";
  data+="\"temperature (C)\": ";
  data+=(int)tempC;
  data+= ",";
  data+="\n";
  data+="\"humidity\": ";
  data+=(int)humid;
  data+="\n";
  data+="}";
  data+="\n";
  data+="}";
  return data;
  
}


//function that reads the DHT 11 Sensor data 
void getData() {
  
  static float tF = 86.0;
  static float tC = 30.0;
  static float tH = 1.0;
  tempF = tF++;
  tempC = tC++;
  humid = tH++;
  
  int chk = DHT11.read();
  switch (chk)
  {
  case 0: 
    Serial.println("Read OK"); 
    humidity = (float)DHT11.humidity;
    tempF = DHT11.fahrenheit();
    tempC = DHT11.temperature;
    break;
  case -1: 
    Serial.println("Checksum error"); 
    break;
  case -2: 
    Serial.println("Time out error"); 
    break;
  default: Serial.println("Unknown error"); 
    break;
  }
  
}

Node.js code that subscribe to temperature measurement from Linkit

JavaScript
Subscribe to MQTT message from Linkit. Pushes the data to MongoDB
var mqtt = require('mqtt'); //includes mqtt server 
var mongodb = require('mongodb'); // includes mongoDB 
var mongodbClient = mongodb.MongoClient; //initialises the mongoDB client
var mongodbURI = 'mongodb://localhost:27017/TempMonitor'; //activating the MongoDB port 27017, here TempMontor is the name of the database
var deviceRoot = "demo/device/"; //deviceroot is topic name given in arduino code 
var collection,client; //initialise collection and client

mongodbClient.connect(mongodbURI, setupCollection); //connect the database with collecion
     
function setupCollection(err, db) {
   if(err) throw err;
   collection=db.collection("test_mqtt"); //name of the collection in the database
   client=mqtt.connect({ host: 'localhost', port: 1883 }); //connecting the mqtt server with the MongoDB database
   client.subscribe(deviceRoot+"+"); //subscribing to the topic name 
   client.on('message', insertEvent); //inserting the event
}

//function that displays the data in the MongoDataBase
function insertEvent(topic,message) {
   var key=topic.replace(deviceRoot,'');

   collection.update(
   { _id:key }, 
   { $push: { events: { event: {  value:message, when:new Date() } } } }, 
   { upsert:true },

   function(err,docs) {  
   if(err) {
      console.log("Insert fail")// Improve error handling		
	 }
 }

 );

}

Credits

Green Robot Machinery

Green Robot Machinery

7 projects • 41 followers
Green Robot Machinery Private Limited
VIBHAV

VIBHAV

3 projects • 6 followers

Comments