Hackster is hosting Hackster Holidays, Ep. 7: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Friday!Stream Hackster Holidays, Ep. 7 on Friday!
Amol Disale
Published © GPL3+

Temperature Humidity Data Logger

This project helps to monitor temperature and humidity values of a particular location along with device battery level.

IntermediateFull instructions provided7,392
Temperature Humidity Data Logger

Things used in this project

Story

Read more

Schematics

Project connection diagram

Code

LinkIt ONE CODE

Arduino
DHT Library can be downloaded from:
https://github.com/Seeed-Studio/Grove_Starter_Kit_For_LinkIt/tree/master/libraries/Humidity_Temperature_Sensor
#include <LBattery.h>
#include <LGPRS.h>
#include <LGPRSClient.h>
#include "DHT.h"

#define URL    "things.ubidots.com"
#define TOKEN ""  // replace with your Ubidots token generated in your profile tab
#define VARID1 ""    //Temperature variable id           
#define VARID2 ""    //Humidity variable id
#define VARID3 ""    //Battery Level variable id 


// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)  
#define DHTPIN 2     // Digital pin 2 where DHT 22 is connected to
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);

char buff[256];  //Info about battery level and charging

//Temperature and Humidity sensor readings from DHT 22
float t = 0.0;
float h = 0.0;

//Location where this logger is placed!! 
String loclat="19.21833";
String loclng="72.978088";
String Location;

//Initializing gprs
LGPRSClient client;  //GPRS Client

void setup()
{
  Serial.begin(9600);
  Serial.println("Temperature Humidity Logger by Er.Amol");
  dht.begin();

// Creating a string containing latitude and longitude information of the Logger's location
  Location="{\"lat\":";
  Location=Location+loclat;
  Location += " ,\"lng\":";
  Location=Location+loclng+ "}";
  
  Serial.println("Attach to GPRS network by APN setting");
  //Note add Access point name, username and password according to your sim card manufacturer/service provide
   while (!LGPRS.attachGPRS("APN","Username","Password"))
  {
    delay(500);
   }

  LGPRSClient globalclient ;
  client = globalclient;

  delay(2000);
}

void loop()
{
  Serial.println("Smart Trash Can Project by Er.Amol");
  if(dht.readHT(&t, &h)) //Reading Temperature and humidity from DHT 22
 {
  delay(2000);
  dht.readHT(&t, &h);
  Serial.println("------------------------------");
  Serial.print("Temperature: ");
  Serial.println(t);

  Serial.print("Humidity: ");
  Serial.println(h);  

  sprintf(buff,"Battery level = %d", LBattery.level() );
  Serial.println(buff);
 
 String payload = "[{\"variable\":\"" VARID1 "\",\"value\":"+ String(t)+",\"context\":"+Location+"},{\"variable\":\"" VARID2 "\",\"value\":" + String(h) + "},{\"variable\":\"" VARID3 "\",\"value\":" + String(LBattery.level()) + "}]";
 
 String le = String(payload.length());
  
  // if you get a connection, report back via serial:
  Serial.print("Connect to ");
  Serial.println(URL);
  if (client.connect(URL, 80))
  {
    // Build HTTP POST request
    Serial.println("connected");    
    client.print(F("POST /api/v1.6/collections/values/?token="));
    client.print(TOKEN);
    client.println(F(" HTTP/1.1"));
    client.println(F("Content-Type: application/json"));
    client.print(F("Content-Length: "));
    client.println(le);
    client.print(F("Host: "));
    client.println(URL);
    client.println(); 
    client.println(payload);  
  }
  else
  {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
  delay(100);
  // if there are incoming bytes available
  // from the server, read them and print them:
  if (client.available())
  {
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.available() && !client.connected())
  {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();
  }

  //Delay of about 1 minute or 60 seconds
   for (int minutes = 0; minutes < 7; minutes++) 
   {
     delay(10000);  //10 seconds delay or 10 thousand milli-seconds
   }
   
  }
}

Credits

Amol Disale
9 projects • 100 followers
I am passionate about the idea of open-source hardware and software. I am ready to help in prototyping IoT, Smart Home, and other products.

Comments