Hi,
I am beginning with Arduino and I2C but I think I am not too bad in programmation. I think some technical consideration affect the good functionning of my code.
I figure out how to setup the Temperature / humidity sensor DHT22 with my Arduino Uno Rev 3 and I was able to read the temperature and humidity.
I want to be able to send the values to a Raspberry and display the temperature on a web page and eventually manage a power relay to control the heater in my house.
I am able to send the data to the Raspberry, but after 2 or 3 request, the loop() function stop to run and each request return the last read value.
I tried many possibility and the only way I am able to send the data is when I read the data in the loop. I tried to read the temperature and humidity in the callback request just before the write to the Raspberry, but for any reason, the Rasberry doesn't read anything.
// Example testing sketch for various DHT humidity/temperature sensors
// Written by ladyada, public domain
#include "DHT.h"
#include <Wire.h>
#define DHTPIN 2 // what pin we're connected to
#define SLAVE_ADDRESS 0x2A // define slave address (0x2A = 42)
String strVal;
float t, h;
String rVal = "";
// Uncomment whatever type you're using!
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302)
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
// Connect pin 1 (on the left) of the sensor to +5V
// Connect pin 2 of the sensor to whatever your DHTPIN is
// Connect pin 4 (on the right) of the sensor to GROUND
// Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(115200);
Serial.println("DHTxx test!");
Wire.begin(SLAVE_ADDRESS);
Wire.onRequest(request);
Wire.onReceive(OnReceive);
strVal = "";
dht.begin();
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
strVal = getHumiditeTemperature();
Serial.println(strVal);
delay(1000);
}
void request()
{
String strToSend = "";
Serial.println("Request received");
strToSend = strVal;
char bufVal[10];
memset(bufVal, 0, 10);
strToSend.toCharArray(bufVal,10);
// Serial.println(bufVal);
Wire.write(bufVal);
free(bufVal);
}
void OnReceive(int count)
{
strVal = "";
Serial.print("Process OnReceived: ");
char c = Wire.read();
strVal = getHumiditeTemperature();
}
String getHumiditeTemperature()
{
String rVal = "";
t = dht.readTemperature() - 2;
h = dht.readHumidity();
if (isnan(t) || isnan(h))
{
Serial.println("Failed to read from DHT");
} else {
char * bufHum;
char * bufTemp;
bufHum=(char *) malloc (12);
bufTemp=(char *) malloc (12);
dtostrf (h, 1, 1, bufHum);
dtostrf (t, 1, 1, bufTemp);
String strHum=bufHum;
String strTemp=bufTemp;
rVal=(strHum+":"+strTemp+"\0");
free(bufTemp);
free(bufHum);
return(rVal);
}
}
The Raspberry is Sending 1 byte to the Arduino and it wait 2 second before to request the data, my OnReceive callback is then reading the values in the DHT22 and set the strVal value. But again, if I remove the strVal = getHumiditeTemperature(); from the main loop. The data is not sent properly to the Raspberry.
My main problem is not to request the temperature every 1 second in the main loop, but it is when I request teh data from the Raspberry 2 or 3 times, the main loop just stop to loop. It is like the request callback function never resume to the loop. If I send more request, the callback is still executing, but the temperature is nto updated.
Any clue what cause my callback not resuming in the loop() function?
Thanks for any help.