Hi Jens,
try updating the DHT-11 program code with the following:
// it makes program to respond to webservices calls:
// - http://<hg_address>/api/Components.DHT11/0/Sensor.Temperature
When.WebServiceCallReceived("Components.DHT11/0/Sensor.Temperature", ( args ) =>
{
return "{ 'ResponseValue' : '" + Program.Parameter("Sensor.Temperature").Value + "' }";
});
// - http://<hg_address>/api/Components.DHT11/0/Sensor.Humidity
When.WebServiceCallReceived("Components.DHT11/0/Sensor.Humidity", ( args ) =>
{
return "{ 'ResponseValue' : '" + Program.Parameter("Sensor.Humidity").Value + "' }";
});
int[] data = new int[5];
var irpin = ConnectorPin.P1Pin7.ToProcessor();
MemoryGpioConnectionDriver driver = new MemoryGpioConnectionDriver();
while (Program.IsEnabled)
{
driver.Allocate(irpin, PinDirection.Output);
driver.Write(irpin, true);
//Pause(5);
for(int a = 0; a < 30; a++)
{
driver.Allocate(irpin, PinDirection.Output);
driver.Write(irpin, false);
Pause(0.018);
//driver.Write(irpin, true);
//Pause(0.001);
//
driver.Allocate(irpin, PinDirection.Input);
//
int i = 0;
data = new int[5];
int j = 0;
int counter = 0;
bool laststate = true;
//
// detect change and read data
for ( i=0; i<= 85; i++) {
counter = 0;
while (driver.Read(irpin) == laststate) {
//Raspberry.Timers.HighResolutionTimer.Sleep(0.000001m);
if (++counter == 255) {
break;
}
}
laststate = driver.Read(irpin);
if (counter == 255) break;
// ignore first 3 transitions
if ((i >= 4) && (i%2 == 0)) {
// shove each bit into the storage bytes
data[j/8] <<= 1;
if (counter > 40)
data[j/8] |= 1;
j++;
}
}
//
//Program.Notify("DHT-11", "J = " + j.ToString());
if ((j >= 40) && (data[4] == ((data[0] + data[1] + data[2] + data[3]) & 0xFF)) )
{
data[2] = data[2] - 3; // temperature adjustment, due to heat excess inside the HG box
double f = data[2] * 9D / 5D + 32;
//Console.WriteLine("
Humidity = {0}.{1} %
Temperature = {2}.{3} *C ({4} *F)
",
// data[0], data[1], data[2], data[3], f);
var hum = data[0] + "." + data[1];
var tempc = data[2] + "." + data[3];
var tempf = f.ToString();
//if (hum != Program.Parameter("Sensor.Humidity").Value)
{
Program.RaiseEvent("Sensor.Humidity", hum, "DHT-11 Sensor");
}
//if (tempc != Program.Parameter("Sensor.Temperature").Value)
{
Program.RaiseEvent("Sensor.TemperatureF", tempf, "DHT-11 Sensor");
Program.RaiseEvent("Sensor.Temperature", tempc, "DHT-11 Sensor");
}
break;
}
else
{
//Console.WriteLine(j + ") Data not good, skip
");
Pause(2);
}
}
//
//Console.WriteLine(data[0]);
//Console.WriteLine(data[1]);
//Console.WriteLine(data[2]);
//Console.WriteLine(data[3]);
//Console.WriteLine(data[4]);
//
var pause = Program.InputField("UpdateInterval").DecimalValue;
if (pause == 0) pause = 600; // default pause is 10 minutes
Pause(pause);
}
g.