int main(int argc, char **argv) { if (!bcm2835_init()) return 1; if (argc != 3) { printf("usage: %s [11|22|2302] GPIOpin#\n", argv[0]); printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]); return 2; } int type = 0; if (strcmp(argv[1], "11") == 0) type = DHT11; if (strcmp(argv[1], "22") == 0) type = DHT22; if (strcmp(argv[1], "2302") == 0) type = AM2302; if (type == 0) { printf("Select 11, 22, 2302 as type!\n"); return 3; } int dhtpin = atoi(argv[2]); if (dhtpin <= 0) { printf("Please select a valid GPIO pin #\n"); return 3; } fprintf(stderr, "Using pin #%d\n", dhtpin); readDHT(type, dhtpin); return 0; } // main
/* This example read humidity and temperature from DHT11 or DHT22 sensor */ int main(int argc, char **argv) { setupUUGear(); setShowLogs(0); UUGearDevice dev = attachUUGearDevice ("UUGear-Arduino-7853-2668"); if (dev.fd != -1) { int i; for (i = 0; i < 200; i ++) { time_t mytime = time(NULL); int data = readDHT(&dev, 4); float humidity = ((float)(data >> 16)) / 10; if ( humidity > 100 ) { humidity = (data >> 24); // for DHT11 } float temperature = ((float)(data & 0x7FFF)) / 10; if ( temperature > 125 ) { temperature = ((data & 0x7F00) >> 8); // for DHT11 }
int ICACHE_FLASH_ATTR settingsCGI(HttpdConnData *connData) { char buff[256]; float * r = readDHT(); float lastTemp = r[0]; float lastHum = r[1]; os_sprintf(buff, "{\"power\":\"%s\",\"mode\":\"%s\",\"temp\":\"%d\",\"fan\":\"%s\",\"swing\":\"%s\",\"dht_temp\":\"%d\",\"dht_humid\":\"%d\"}", sysCfg.power, sysCfg.mode, sysCfg.temp, sysCfg.fan, sysCfg.swing, (int)(lastTemp * 100), (int)(lastHum * 100) ); httpdStartResponse(connData, 200); httpdHeader(connData, "Content-Type", "text/html"); httpdEndHeaders(connData); httpdSend(connData, buff, -1); return HTTPD_CGI_DONE; }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; if (argc != 2) { printf("usage: %s GPIOpin#\n", argv[0]); printf("example: %s 4 - Read from an DHT11 connected to GPIO #4\n", argv[0]); return 2; } int type = DHT11; int dhtpin = atoi(argv[1]); if (dhtpin <= 0) { printf("Please select a valid GPIO pin #\n"); return 3; } // printf("Using pin #%d\n", dhtpin); readDHT(type, dhtpin); return 0; } // main
int ICACHE_FLASH_ATTR dht_temp_str(char *buff) { struct sensor_reading* result = readDHT(); if(result->success) return os_sprintf(buff, "%d.%d", (int)(result->temperature), abs((int)(result->temperature*100)) - abs(((int)result->temperature)*100) ); else return os_sprintf(buff, "N/A" ); }
int ICACHE_FLASH_ATTR dht_humi_str(char *buff) { struct sensor_reading* result = readDHT(); if(result->success) return os_sprintf(buff, "%d.%d", (int)(result->humidity),(int)((result->humidity - (int)result->humidity)*100)); else return os_sprintf(buff, "N/A" ); }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; int type = DHT22; int dhtpin = atoi(argv[1]); printf("Using pin #%d\n", dhtpin); readDHT(type, dhtpin); return 0; } // main
static void ICACHE_FLASH_ATTR pollThermostatCb(void * arg) { unsigned long epoch = sntp_time+(sntp_tz*3600); int year=get_year(&epoch); int month=get_month(&epoch,year); int day=day=1+(epoch/86400); int dow=wd(year,month,day); epoch=epoch%86400; unsigned int hour=epoch/3600; epoch%=3600; unsigned int min=epoch/60; int minadj = (min*100/60); int currtime = hour*100+minadj; if(sysCfg.thermostat1state == 0) { os_printf("Thermostat switched off, abandoning routine.\n"); return; } long Treading=-9999; if(sysCfg.sensor_dht22_enable) { struct sensor_reading* result = readDHT(); if(result->success) { Treading=result->temperature*100; if(sysCfg.thermostat1_input==2) // Humidistat Treading=result->humidity*100; } } else { if(sysCfg.sensor_ds18b20_enable && sysCfg.thermostat1_input==0 ) { struct sensor_reading* result = read_ds18b20(); if(result->success) { int SignBit, Whole, Fract; Treading = result->temperature; SignBit = Treading & 0x8000; // test most sig bit if (SignBit) // negative Treading = (Treading ^ 0xffff) + 1; // 2's comp Whole = Treading >> 4; // separate off the whole and fractional portions Fract = (Treading & 0xf) * 100 / 16; if (SignBit) // negative Whole*=-1; Treading=Whole*100+Fract; } }//ds8b20 enabled }
static ICACHE_FLASH_ATTR void MQTTbroadcastReading(void* arg){ if(sysCfg.mqtt_enable==1) { //os_printf("Sending MQTT\n"); if(sysCfg.sensor_dht22_enable) { struct sensor_reading* result = readDHT(); if(result->success) { char temp[32]; char topic[128]; int len; dht_temp_str(temp); len = os_strlen(temp); os_sprintf(topic,"%s",sysCfg.mqtt_dht22_temp_pub_topic); MQTT_Publish(&mqttClient,topic,temp,len,0,0); os_printf("Published \"%s\" to topic \"%s\"\n",temp,topic); dht_humi_str(temp); len = os_strlen(temp); os_sprintf(topic,"%s",sysCfg.mqtt_dht22_humi_pub_topic); MQTT_Publish(&mqttClient,topic,temp,len,0,0); os_printf("Published \"%s\" to topic \"%s\"\n",temp,topic); } } if(sysCfg.sensor_ds18b20_enable) { struct sensor_reading* result = read_ds18b20(); if(result->success) { char temp[32]; char topic[128]; int len; ds_str(temp,0); len = os_strlen(temp); os_sprintf(topic,"%s",sysCfg.mqtt_ds18b20_temp_pub_topic); MQTT_Publish(&mqttClient,topic,temp,len,0,0); os_printf("Published \"%s\" to topic \"%s\"\n",temp,topic); } } } }
int main(int argc, char **argv) { if (!bcm2835_init()) return 1; if (argc < 3) { printf("usage: %s {11|22|2302} GPIOpin# [ROS Parameters]\n", argv[0]); printf("example: %s 2302 4 - Read from an AM2302 connected to GPIO #4\n", argv[0]); return 2; } int type = 0; if (strcmp(argv[1], "11") == 0) type = DHT11; if (strcmp(argv[1], "22") == 0) type = DHT22; if (strcmp(argv[1], "2302") == 0) type = AM2302; if (type == 0) { printf("Select 11, 22, 2302 as type!\n"); return 3; } int dhtpin = atoi(argv[2]); if (dhtpin <= 0) { printf("Please select a valid GPIO pin #\n"); return 3; } printf("Using pin #%d\n", dhtpin); ros::init(argc, argv, "raspi_dht"); ros::NodeHandle n; ros::Publisher temperature_pub = n.advertise<std_msgs::Float32>("temperature", 2); ros::Publisher humidity_pub = n.advertise<std_msgs::Float32>("humidity", 2); ros::Rate loop_rate(0.016); ros::Duration retry_interval(5.0); ros::Duration backoff_interval(200); bool last_had_data = true; while (ros::ok()) { std_msgs::Float32 temperature_msg, humidity_msg; float t, h; if (readDHT(type, dhtpin, &t, &h)) { temperature_msg.data = t; humidity_msg.data = h; #ifdef DEBUG printf("Publishing data: t=%f, h=%f\n", t, h); ROS_INFO("BOS_RASPI_DHT_NODE: %f, %f", t, h); #endif temperature_pub.publish(temperature_msg); humidity_pub.publish(humidity_msg); ros::spinOnce(); loop_rate.sleep(); last_had_data = true; } else { if (last_had_data) { ROS_INFO("BOS_raspi_DHT_node: no data, retrying soon"); retry_interval.sleep(); } else { ROS_INFO("BOS_raspi_DHT_node: no data, waiting a while"); backoff_interval.sleep(); } last_had_data = false; } } return 0; } // main
int main (int argc, char *argv[]) { // int tries = 10; FILE * f; int newTemp, newRh; if (wiringPiSetup () == -1) exit(EXIT_FAILURE) ; mkdir(DATADIR, 0755 ); if ( chown(DATADIR, getuid(), -1) == -1 ) { fprintf(stderr, "Was not able to create /var/run/dht\n"); exit(1); } if (setuid(getuid()) < 0) { perror("Dropping privileges failed\n"); exit(EXIT_FAILURE); } //chdir(DATADIR); f = fopen("/var/run/dht/pid", "w" ); fprintf(f, "%d", getpid() ); fclose(f); while(1) { // while (read_dht22_dat() == 0 && tries--) // { // delay(1000); // wait 1sec to refresh // } // if (readRHT03 (DHTPIN, &newTemp, &newRh)) if ( readDHT(DHTPIN, &newTemp, &newRh) ) { static int t2, h2, ft = 1, terror=0, herror=0; if (ft || (terror>4) || (herror>4) ) { ft=0; h2=newRh; t2=newTemp; terror=herror=0; } if ( (newRh > 0) && (newRh < 950) && (abs(h2-newRh)<10) ) { f=fopen("/var/run/dht/humidity.x", "w"); fprintf(f, "%d", newRh ); fclose(f); rename("/var/run/dht/humidity.x", "/var/run/dht/humidity"); h2 = newRh; herror=0; } else herror++; if ( ( newTemp < 600 ) && (abs(abs(t2)-abs(newTemp))<10) ) { f=fopen("/var/run/dht/temp.x", "w"); fprintf(f, "%d", newTemp + (TEMPERATURECORRECTION)); fclose(f); rename("/var/run/dht/temp.x", "/var/run/dht/temp"); t2 = newTemp; terror=0; } else terror++; } sleep(INTERVAL); } return 0 ; }