Page 1 of 1

Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Thu Mar 28, 2013 8:11 am
by arnohr
Good morning fellows,
a few days ago I started my own DHT22 http://www.adafruit.com/products/385 temprature und humidity datalogging project based on the Adafruit Demo DHT-Driver http://goo.gl/nUrTZ.
But instead of using Google Docs, I decided to use Cosm to log and display the data. Because I'm not very familiar with Python, the project should be realizied in C/C++. For transfering the data directly to Cosm, the curl library seemed to be the right choice http://curl.haxx.se/libcurl/.

Finally this is the result of my work: https://github.com/fraterv/dht22-cosm-d ... t_logger.c

It works fine, but only for ~ 24-26 hours. After 24 hours the process stops working without any other error. It just halts and consumes ~ 30% of the available CPU time and thats it...

I'm using a Rev. 1 Model B Rasperry Pi and an up to date Raspian Linux. I worked through the code several times, but I'm not able to identify the problem. And because the error occures only every 24 hours, try and error is a poor solution.

I'm happy about any suggestion!

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Sat Mar 30, 2013 3:33 pm
by ptica
Hi,
perhaps it is not the answer you would like to hear. But I'm also testing DTH22. My idea is to use Adafruit DHT driver unmodified.

I'm intend to run driver like:
sudo ./Adafruit_DHT 22 4 > dht.out

After that you have output in file dht.out and than you can read and parse file, and send data to COSM or something else.

In next days I'm planing to check data from sensor every 15 minutes (via crontab). I'll let you know if after a day still works fine for me.

Regards

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Sat Mar 30, 2013 11:18 pm
by awesomeasmus
I tried to copy your code and compile the program, however "#include <iostream>" and curl itself is causing problems.
Can you provide me with a step by step guide to install curl(the guide on their website is very confusing) and a guide on how to compile a c program with a <iostream> include, a quick google search tells me it´s c++ exclusive?
Right now i'm using geany to write and compile my programs and so far i have succeded to use the Raspberry Pi to control servo motors via a PCA9685 chip and to do some sensor fusion(Gyro and acc) in order to build a balance robot, but i need live data to tune my PID.

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Fri Apr 05, 2013 8:39 pm
by ptica
I had testing DHT 22 now for a few days. It all worked perfectly until I accidentally unplug DHT 22 sensor. And at that time Adafruit_DHT driver hung up. I was doing some research and in code oa Adafruit_DHT.c https://github.com/adafruit/Adafruit-Ra ... ruit_DHT.c at line 93 there is endless loop. I've changed loop
from:

Code: Select all

  while (bcm2835_gpio_lev(pin) == 1) {
    usleep(1);
  }
to:

Code: Select all

  int loopcnt = 0;
  while (bcm2835_gpio_lev(pin) == 1 && loopcnt<100) {
    usleep(1);
    loopcnt++;
  }
For me works this solution. And I post data to COSM, as said in my previous post. I left Adafruit driver like it is and posting data to COSM with shell command curl.

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Fri Apr 19, 2013 2:21 am
by Skitzmixer
ptica wrote:For me works this solution. And I post data to COSM, as said in my previous post. I left Adafruit driver like it is and posting data to COSM with shell command curl.
Are you able to post up the shell command you use to post the data to cosm? I've got it reading the temps correctly but im having trouble actually getting the data to cosm.

Thanks.

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Sun May 05, 2013 11:51 am
by arnohr
Geronimo!

I've managed to identify the problem! I'm using a WLAN adapter to connect with my local LAN. The WLAN signal is very weak at the location where I've placed my Pi. If the Pi loses the WLAN connection (caused by the weak WLAN signal), it won't reconnect automatically. No WLAN connection, no logging at Cosm. The logging program I've written works at least 7 days / 24 hours, until I accidentally cut off main power... We will see if there are any further problems/bugs.

Next step for me is to figure out how I realize an automatic reconnect after losing the signal.

@Skitmizer
Here you find an example how to use commandline curl https://cosm.com/docs/quickstart/curl.html

@ptica
The loop isn't really endless... While bcm2835_gpio_lev(pin) returns the value 1, the program sleeps 1 ms and after that loop starts checking again bcm2835_gpio_lev(pin) . If bcm2835_gpio_lev(pin) returns 0, the loop ends.

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Wed May 08, 2013 10:55 am
by ptica
arnohr wrote:
The loop isn't really endless... While bcm2835_gpio_lev(pin) returns the value 1, the program sleeps 1 ms and after that loop starts checking again bcm2835_gpio_lev(pin) . If bcm2835_gpio_lev(pin) returns 0, the loop ends.

Well this is bad loop for me anyway! Like I said when the sensor doesn't respond the loop is never ended. There must be another check lets say if after second or two you're still in same loop, you should end this loop. And exactly this check i added.

Re: Logging DHT22 temp/humid data using C, libcurl and Cosm

Posted: Sat May 25, 2013 2:07 pm
by arnohr
@ptica
OK, that is an argument.
@awesomeasmus
I think you have tried to compile the code with the gcc compiler. But the iostream library is a C++ library so you should use the g++ compiler instead:

Code: Select all

g++ dht_logger.c -lbcm2835 -lcurl -o dht_logger