Skip to content

Instantly share code, notes, and snippets.

@ypetya
Last active August 29, 2015 14:10
Show Gist options
  • Save ypetya/eecfb1fc0deb362973b2 to your computer and use it in GitHub Desktop.
Save ypetya/eecfb1fc0deb362973b2 to your computer and use it in GitHub Desktop.
Playing with Hardware-stuff - POC

Monitoring DHT11 humidity and temperature sensor

Step 1. install preresquites on ubuntu

apt-get install arduino arduino-core
# add necessary usergroups relogin or restart
arduino

Load the "blink" example and try to upload to arduino for a quick test

Step 2. install DHT11 sensor software to arduino

download dht11.cpp and dht11.h put them under ~/sketchbook/libraries/DHT11 folder

burn the sketch dht11_test1.pde to the arduino

Step 3. connect the wires like this :

https://www.virtuabotix.com/virtuabotix-dht11-pin-out-reference-guide/

monitor the serial port at 115200 baud, you should see the data like this :

Read sensor: OK 
Humidity (%): 36.00 
Temperature (°C): 26.00 
Temperature (°F): 78.80 
Temperature (°K): 299.15 
Dew Point (°C): 9.80 
Dew PointFast (°C): 9.76

Step 4. connect them to your raspberry via USB

Checking the connected system to work

  1. the new raspberry B+ does not have enough power on USB ?? you will need an external power-supply for your arduino
  2. the older raspberry B has enough power for the arduino on USB
# check the new device is connected
$ lsusb
# get the device path
$ dmesg
# setup the connection baudrate to 115200
$ stty -F /dev/ttyACM0 raw ispeed 115200 ospeed 115200 -ignpar cs8 -cstopb -echo
# and lets see the output
$ cat /dev/ttyACM0 

DHT11 TEST PROGRAM 
LIBRARY VERSION: 0.4.1



Read sensor: OK
Humidity (%): 36.00
Temperature (°C): 25.00
Temperature (°F): 77.00
Temperature (°K): 298.15
Dew Point (°C): 8.91
Dew PointFast (°C): 8.88

Step 5. configuring web server

apt-get install lighttpd 

Add a cgi script and configure

Setup crontab

$ crontab -e

* * * * * date > /tmp/temperature && grep -m1 -A2 OK /dev/ttyACM0 >> /tmp/temperature

Create a CGI script

#!/bin/bash

echo "Content-type: text/html"
echo

echo "<html><body><pre style='font-size:72px'>"
cat /tmp/temperature
echo "</pre></body></html>"
echo

Burn the pie

dd if=path_to_img/raspberry.img of=/dev/sdb

Install nodejs

  • this is for another project actually - you wont need this
# curl -sL https://deb.nodesource.com/setup | sudo bash -
git clone --depth=1 https://github.com/joyent/node
pushd node && ./configure && make && sudo make install && popd 

sudo apt-get install -y nodejs

=> does not work, should check the following:

http://blog.tomg.co/post/21322413373/how-to-install-node-js-on-your-raspberry-pi

//
// FILE: dht11.h
// VERSION: 0.4.1
// PURPOSE: DHT11 Temperature & Humidity Sensor library for Arduino
// LICENSE: GPL v3 (http://www.gnu.org/licenses/gpl.html)
//
// DATASHEET: http://www.micro4you.com/files/sensor/DHT11.pdf
//
// URL: http://playground.arduino.cc/Main/DHT11Lib
//
// HISTORY:
// George Hadjikyriacou - Original version
// see dht.cpp file
//
#ifndef dht11_h
#define dht11_h
#if defined(ARDUINO) && (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#define DHT11LIB_VERSION "0.4.1"
#define DHTLIB_OK 0
#define DHTLIB_ERROR_CHECKSUM -1
#define DHTLIB_ERROR_TIMEOUT -2
class dht11
{
public:
int read(int pin);
int humidity;
int temperature;
};
#endif
//
// END OF FILE
//
//
// FILE: dht11_test1.pde
// PURPOSE: DHT11 library test sketch for Arduino
//
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
// fast integer version with rounding
//int Celcius2Fahrenheit(int celcius)
//{
// return (celsius * 18 + 5)/10 + 32;
//}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
// dewPoint function NOAA
// reference (1) : http://wahiduddin.net/calc/density_algorithms.htm
// reference (2) : http://www.colorado.edu/geography/weather_station/Geog_site/about.htm
//
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
// delta max = 0.6544 wrt dewPoint()
// 6.9 x faster than dewPoint()
// reference: http://en.wikipedia.org/wiki/Dew_point
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 2
void setup()
{
Serial.begin(115200);
Serial.println("DHT11 TEST PROGRAM ");
Serial.print("LIBRARY VERSION: ");
Serial.println(DHT11LIB_VERSION);
Serial.println();
}
void loop()
{
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (°C): ");
Serial.println((float)DHT11.temperature, 2);
Serial.print("Temperature (°F): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);
Serial.print("Temperature (°K): ");
Serial.println(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point (°C): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (°C): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
delay(2000);
}
//
// END OF FILE
//
@ypetya
Copy link
Author

ypetya commented Dec 15, 2014

further plans:

  • JSONP endpoint
  • other sensors

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment