Hmm, i can now run the dht command with full pathbut nothing happens. Below is the copy from dht.c that i used to compile the dht
IDX=3 and i use GPIO18
Code: Select all
/*
* dht.c:
* Read temperature and humidity from DHT11 or DHT22 sensor on a Orange/Raspberry PI and put the value's into Domoticz every 3 minutes.
*
* Customize the DHT_PIN and Domoticz IDX below, make a dummy temp/hum in Domoticz.
* Console output is default off, silent in console output, enable below.
* Its seems normal that you see "Data not good, skip" a few times, if enabled below.
* IP address is set to 127.0.0.1 change below.
*
*
* For compiling use : cd /root/scripts/gpio/ ; gcc -Wall -o dht dht.c -lwiringPi
* For console use : /root/scripts/gpio/dht
* For background use : /root/scripts/gpio/dht &
*
* More info : http://www.uugear.com/portfolio/read-dht1122-temperature-humidity-sensor-from-raspberry-pi/
*
* Updated : 04-03-2019 by ILoveIoT.
*
*/
#include <wiringPi.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#define MAX_TIMINGS 85
// gpio readall BCM10 | wPi24 | GPIO.18 | ALT3 | 0 | Physical 35 |
#define DHT_PIN 18 /* GPIO-18 */
// Set the Domoticz IDX from the dummy device (temp/hum) you created
#define DOM_IDX 3 /* Domoticz IDX 3 */
int data[5] = { 0, 0, 0, 0, 0 };
void read_dht_data()
{
uint8_t laststate = HIGH;
uint8_t counter = 0;
uint8_t j = 0, i;
data[0] = data[1] = data[2] = data[3] = data[4] = 0;
/* pull pin down for 18 milliseconds */
pinMode( DHT_PIN, OUTPUT );
digitalWrite( DHT_PIN, LOW );
delay( 30 );
/* prepare to read the pin */
pinMode( DHT_PIN, INPUT );
/* detect change and read data */
for ( i = 0; i < MAX_TIMINGS; i++ )
{
counter = 0;
while ( digitalRead( DHT_PIN ) == laststate )
{
counter++;
delayMicroseconds( 2 );
if ( counter == 255 )
{
break;
}
}
laststate = digitalRead( DHT_PIN );
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 > 16 )
data[j / 8] |= 1;
j++;
}
}
/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if ( (j >= 40) &&
(data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
{
float h = (float)((data[0] << 8) + data[1]) / 10;
if ( h > 100 )
{
h = data[0]; // for DHT11
}
float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
if ( c > 125 )
{
c = data[2]; // for DHT11
}
if ( data[2] & 0x80 )
{
c = -c;
}
// For console temp & hum readings uncomment these 2 lines
//float f = c * 1.8f + 32;
//printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );
// Making the string with sensor data for Domoticz
char str1[] = "/bin/bash -c 'curl -i -s \"http://192.168.1.209:8080/json.htm?type=command¶m=udevice&idx=3";
// Put variable c (Value celsius) into a string
char str2[25]; //size of the number
sprintf(str2, "%g", c);
// Some comma separated value's in between
char str3[] = ";";
// Put variable h (Value humidity) into a string
}
/*
* check we read 40 bits (8bit x 5 ) + verify checksum in the last byte
* print it out if data is good
*/
if ( (j >= 40) &&
(data[4] == ( (data[0] + data[1] + data[2] + data[3]) & 0xFF) ) )
{
float h = (float)((data[0] << 8) + data[1]) / 10;
if ( h > 100 )
{
h = data[0]; // for DHT11
}
float c = (float)(((data[2] & 0x7F) << 8) + data[3]) / 10;
if ( c > 125 )
{
c = data[2]; // for DHT11
}
if ( data[2] & 0x80 )
{
c = -c;
}
// For console temp & hum readings uncomment these 2 lines
//float f = c * 1.8f + 32;
//printf( "Humidity = %.1f %% Temperature = %.1f *C (%.1f *F)\n", h, c, f );
// Making the string with sensor data for Domoticz
char str1[] = "/bin/bash -c 'curl -i -s \"http://192.168.1.209:8080/json.htm?type=command¶m=udevice&idx=3";
// Put variable c (Value celsius) into a string
char str2[25]; //size of the number
sprintf(str2, "%g", c);
// Some comma separated value's in between
char str3[] = ";";
// Put variable h (Value humidity) into a string
char str4[25]; //size of the number
sprintf(str4, "%g", h);
// And the final end string
char str5[] = ";0\"' > /dev/null 2>&1";
// Setting the domoticz IDX var from above
char str6[25]; //size of the number
sprintf(str6, "%i", DOM_IDX);
char str7[] = "&";
char str8[] = "nvalue=0&svalue=";
// Concentrate all the str1/str8 and resulting in 1 string is stored in str1.
strcat(str6,str7);
strcat(str6,str8);
strcat(str1,str6);
strcat(str2,str3);
strcat(str2,str4);
strcat(str2,str5);
strcat(str1,str2);
// If you wanna view the output of the Domoticz string on console
//puts(str1);
// Case of invalid celsius temps, ignore more then +80 or -40 or 0, and recheck in 3sec
if ( c > -40 && c < 80 && c != 0 )
{
// Send the string/data to Domoticz server
system(str1);
//delay( 2000 ); /* wait 2 seconds before next read */
//delay( 300000 ); /* wait 5 minutes before next read */
delay( 180000 ); /* wait 3 minutes before next read */
} else {
delay( 3000 ); /* wait 3 seconds before next try */
}
} else {
sprintf(str4, "%g", h);
// And the final end string
char str5[] = ";0\"' > /dev/null 2>&1";
// Setting the domoticz IDX var from above
char str6[25]; //size of the number
sprintf(str6, "%i", DOM_IDX);
char str7[] = "&";
char str8[] = "nvalue=0&svalue=";
// Concentrate all the str1/str8 and resulting in 1 string is stored in str1.
strcat(str6,str7);
strcat(str6,str8);
strcat(str1,str6);
strcat(str2,str3);
strcat(str2,str4);
strcat(str2,str5);
strcat(str1,str2);
// If you wanna view the output of the Domoticz string on console
//puts(str1);
// Case of invalid celsius temps, ignore more then +80 or -40 or 0, and recheck in 3sec
if ( c > -40 && c < 80 && c != 0 )
{
// Send the string/data to Domoticz server
system(str1);
//delay( 2000 ); /* wait 2 seconds before next read */
//delay( 300000 ); /* wait 5 minutes before next read */
delay( 180000 ); /* wait 3 minutes before next read */
} else {
delay( 3000 ); /* wait 3 seconds before next try */
}
} else {
// Uncomment this line below if you dont see any data coming
//printf( "Data not good, skip\n" );
}
}
int main( void )
{
//printf( "Orange/Raspberry PI DHT11/DHT22 temperature/humidity test\n" );
if ( wiringPiSetup() == -1 )
exit( 1 );
while ( 1 )
{
read_dht_data();
delay( 5000 ); /* wait 5 seconds before next try */
}
return(0);
}
What have i done wrongly?
When i run the python code i can get the reading only once and that is instantly shown in webinterface of Domoticz, but the script stops and the temperature is not updating anymore.
How can i loop the python code?
this is the python code that works only once
Code: Select all
#!/usr/bin/python
# -*- coding: latin-1 -*-
# basé sur le script Adafruit et adapté pour Domoticz
# Copyright (c) 2014 Adafruit Industries
# Author: Tony DiCola
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.
import os
import sys
import Adafruit_DHT
from requests.auth import HTTPBasicAuth
import requests
############# Parametres #################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# les parametres de Domoticz
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
domoticz_ip='192.168.1.209'
domoticz_port='8080'
user=''
password=''
domoticz_idx=3
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# les parametres du DHT
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#sensor est 11,22,ou 2302
# pin est le numero d la pin que vous avez cablée
sensor=22
pin=18
############# Fin des parametres #################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# le fomat pour la temp hum est celui ci
#/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
def maj_widget(val_url):
requete='http://'+domoticz_ip+':'+domoticz_port+val_url
#print requete
r=requests.get(requete,auth=HTTPBasicAuth(user,password))
if r.status_code != 200:
print "Erreur API Domoticz"
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
# l URL Domoticz pour le widget virtuel
domoticz_ip='192.168.1.209'
domoticz_port='8080'
user=''
password=''
domoticz_idx=3
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# les parametres du DHT
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
#sensor est 11,22,ou 2302
# pin est le numero d la pin que vous avez cablée
sensor=22
pin=18
############# Fin des parametres #################################
#~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# le fomat pour la temp hum est celui ci
#/json.htm?type=command¶m=udevice&idx=IDX&nvalue=0&svalue=TEMP;HUM;HUM_STAT
def maj_widget(val_url):
requete='http://'+domoticz_ip+':'+domoticz_port+val_url
#print requete
r=requests.get(requete,auth=HTTPBasicAuth(user,password))
if r.status_code != 200:
print "Erreur API Domoticz"
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
if humidity is not None and temperature is not None:
print('Temp={0:0.1f}* Humidity={1:0.1f}%'.format(temperature, humidity))
# l URL Domoticz pour le widget virtuel
url='/json.htm?type=command¶m=udevice&idx='+str(domoticz_idx)
url+='&nvalue=0&svalue='
url+=str('{0:0.1f};{1:0.1f};2').format(temperature, humidity)
#print url
maj_widget(url)
else:
print('Probleme avec la lecture du DHT. Try again!')
sys.exit(1)