Sun, 10 Aug 2014 16:56:22 +0200
Added room temperature and humidity to the web interface
/***************************************************************************** * Copyright (C) 2014 * * Michiel Broek <mbroek at mbse dot eu> * * This file is part of the mbsePi-apps * * This is free software; you can redistribute it and/or modify it * under the terms of the GNU General Public License as published by the * Free Software Foundation; either version 2, or (at your option) any * later version. * * mbsePi-apps is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with thermferm; see the file COPYING. If not, write to the Free * Software Foundation, 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *****************************************************************************/ #include "thermferm.h" #include "devices.h" #include "rc-switch.h" #include "panel.h" #include "xutil.h" extern int debug; extern sys_config Config; extern int my_shutdown; #ifdef HAVE_WIRINGPI_H #define MAXTIMINGS 100 int dht11_pin = -1; int dht11_temperature = -1; int dht11_humidity = -1; int dht11_valid = FALSE; int dht11_t_offset = 0; int dht11_h_offset = 0; static uint8_t sizecvt(const int read_value) { /* * digitalRead() and friends from wiringpi are defined as returning a value * < 256. However, they are returned as int() types. This is a safety function */ if (read_value > 255 || read_value < 0) { syslog(LOG_NOTICE, "invalid data from wiringPi library"); } return (uint8_t)read_value; } /* * DHT11 sensor read. This should be used in a thread loop. */ void dht11Read(void) { int tries = 5; unsigned short got_correct_data = 0; if (dht11_pin == -1) return; while (tries && !got_correct_data) { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i = 0; int dht11_dat[5] = {0,0,0,0,0}; /* * Select output mode to send the start signal. */ pinMode(dht11_pin, OUTPUT); digitalWrite(dht11_pin, HIGH); usleep(1000); /* * Low for at least 18 milliseconds */ digitalWrite(dht11_pin, LOW); usleep(20000); digitalWrite(dht11_pin, HIGH); pinMode(dht11_pin, INPUT); /* * Detect change and read data */ for (i=0; i<MAXTIMINGS; i++) { counter = 0; delayMicroseconds(10); while (sizecvt(digitalRead(dht11_pin)) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = sizecvt(digitalRead(dht11_pin)); if (counter == 255) break; /* * ignore first 3 transitions */ if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes dht11_dat[(int)((double)j/8)] <<= 1; if (counter > 16) dht11_dat[(int)((double)j/8)] |= 1; j++; } } /* * If there is no sensor, j = 0 */ if ((counter == 255) && (j == 0)) { if (dht11_temperature != -1) { syslog(LOG_NOTICE, "dht11 sensor disappeared"); } else { syslog(LOG_NOTICE, "dht11 sensor not present"); } dht11_temperature = -1; dht11_humidity = -1; dht11_valid = FALSE; return; } /* * check we read 40 bits (8bit x 5 ) + verify checksum in the last byte * print it out if data is good */ if ((j >= 40) && (dht11_dat[4] == ((dht11_dat[0] + dht11_dat[1] + dht11_dat[2] + dht11_dat[3]) & 0xFF))) { got_correct_data = 1; int h = dht11_dat[0] + dht11_dat[1]; int t = (dht11_dat[2] & 0x7F) + dht11_dat[3]; t += dht11_t_offset; h += dht11_h_offset; if ((dht11_dat[2] & 0x80) != 0) t *= -1; dht11_temperature = t; dht11_humidity = h; dht11_valid = TRUE; } else { tries--; if (tries == 0) syslog(LOG_INFO, "dht11 data checksum was wrong 5 times"); usleep(100000); } } } #endif int device_out(char *uuid, int value) { devices_list *device; time_t now; #ifdef HAVE_WIRINGPI_H int i, rc; char buf[40]; #endif if (uuid == NULL) return 0; now = time(NULL); for (device = Config.devices; device; device = device->next) { if (strcmp(uuid, device->uuid) == 0) { /* * Execute command if different then the old value. But also * every 2 minutes because commands can get lost of devices were * disconnected, or with radio problems. */ if ((value != device->value) || (((int)now - (int)device->timestamp) > 120)) { #ifdef HAVE_WIRINGPI_H rc = 0; if ((device->type == DEVTYPE_RC433) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { enableTransmit(device->gpiopin); usleep(10000); snprintf(buf, 39, "%s,%d", device->address, value ? 1:0); for (i = 0; i < strlen(buf); i++) if (buf[i] == '-') buf[i] = ','; rc = toggleSwitch(buf); syslog(LOG_NOTICE, "RC433 command %s rc=%d", buf, rc); if (debug) fprintf(stdout, "RC433 command %s rc=%d\n", buf, rc); usleep(50000); disableTransmit(); } device->value = value; device->timestamp = time(NULL); return rc; if ((device->type == DEVTYPE_GPIO) && (device->gpiopin != -1) && (device->present == DEVPRESENT_YES)) { } #endif if ((device->type == DEVTYPE_W1) && (device->direction == DEVDIR_OUT_BIN) && (device->present == DEVPRESENT_YES)) { } } else { return 0; } } } return 0; } /* * Auto detect hotplugged or known to be present devices */ int devices_detect(void) { struct dirent *de; DIR *fd; devices_list *device, *ndev; int found, subdevices, i, rc = 0; char buf[40]; uuid_t uu; #ifdef HAVE_WIRINGPI_H int pin; #endif /* * Scan for 1-wire devices */ if ((fd = opendir((char *)"/sys/bus/w1/devices"))) { while ((de = readdir(fd))) { if (de->d_name[0] != '.') { found = FALSE; for (device = Config.devices; device; device = device->next) { if (strcmp(device->address,de->d_name) == 0) { found = TRUE; break; } } if (found == FALSE) { strncpy(buf, de->d_name, 2); buf[2] = '\0'; subdevices = 1; if (strcmp(buf, (char *)"29") == 0) subdevices = 8; if (strcmp(buf, (char *)"3a") == 0) subdevices = 2; for (i = 0; i < subdevices; i++) { ndev = (devices_list *)malloc(sizeof(devices_list)); ndev->next = NULL; ndev->version = 1; ndev->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, ndev->uuid); ndev->type = DEVTYPE_W1; ndev->direction = DEVDIR_UNDEF; if (strcmp(buf, (char *)"10") == 0) { ndev->direction = DEVDIR_IN_ANALOG; ndev->description = xstrcpy((char *)"DS18S20 Digital thermometer"); } else if (strcmp(buf, (char *)"28") == 0) { ndev->direction = DEVDIR_IN_ANALOG; ndev->description = xstrcpy((char *)"DS18B20 Digital thermometer"); } else if (strcmp(buf, (char *)"29") == 0) { ndev->description = xstrcpy((char *)"DS2408 8 Channel addressable switch/LCD"); } else if (strcmp(buf, (char *)"3a") == 0) { ndev->description = xstrcpy((char *)"DS2413 Dual channel addressable switch"); } else if (strcmp(buf, (char *)"w1") == 0) { ndev->description = xstrcpy((char *)"Master System device"); } else { ndev->description = xstrcpy((char *)"Unknown device family "); ndev->description = xstrcat(ndev->description, buf); } ndev->value = ndev->inuse = 0; ndev->present = DEVPRESENT_YES; ndev->address = xstrcpy(de->d_name); ndev->subdevice = i; ndev->gpiopin = -1; ndev->comment = xstrcpy((char *)"Auto detected device"); ndev->timestamp = time(NULL); if (Config.devices == NULL) { Config.devices = ndev; } else { for (device = Config.devices; device; device = device->next) { if (device->next == NULL) { device->next = ndev; break; } } } rc++; } } } } closedir(fd); } #ifdef HAVE_WIRINGPI_H if (piBoardRev() == 2) { /* * Support rev B boards only */ found = FALSE; for (device = Config.devices; device; device = device->next) { if (device->type == DEVTYPE_GPIO) { found = TRUE; break; } } if (found == FALSE) { /* * There were no GPIO devices found. */ subdevices = 12; pin = 0; for (i = 0; i < subdevices; i++) { if (i == 8) pin = 17; ndev = (devices_list *)malloc(sizeof(devices_list)); ndev->next = NULL; ndev->version = 1; ndev->uuid = malloc(37); uuid_generate(uu); uuid_unparse(uu, ndev->uuid); ndev->type = DEVTYPE_GPIO; ndev->value = digitalRead(pin); ndev->present = DEVPRESENT_YES; ndev->address = xstrcpy((char *)"GPIO"); snprintf(buf, 39, "Raspberry GPIO %d", i); ndev->description = xstrcpy(buf); ndev->subdevice = i; ndev->gpiopin = pin; ndev->timestamp = time(NULL); if (i == PANEL_LED) { ndev->direction = DEVDIR_OUT_BIN; ndev->inuse = 1; ndev->comment = xstrcpy((char *)"Frontpanel LED"); } else if (i == PANEL_ENTER) { ndev->direction = DEVDIR_IN_BIN; ndev->inuse = 1; ndev->comment = xstrcpy((char *)"Frontpanel Enter key"); } else if (i == PANEL_DOWN) { ndev->direction = DEVDIR_IN_BIN; ndev->inuse = 1; ndev->comment = xstrcpy((char *)"Frontpanel Down key"); } else if (i == PANEL_UP) { ndev->direction = DEVDIR_IN_BIN; ndev->inuse = 1; ndev->comment = xstrcpy((char *)"Frontpanel Up key"); } else if (i == 7) { ndev->direction = DEVDIR_INTERN; ndev->inuse = 1; ndev->comment = xstrcpy((char *)"1-Wire bus"); } else { ndev->direction = DEVDIR_IN_BIN; ndev->inuse = 0; ndev->comment = xstrcpy((char *)"Raspberry GPIO"); } pin++; if (Config.devices == NULL) { Config.devices = ndev; } else { for (device = Config.devices; device; device = device->next) { if (device->next == NULL) { device->next = ndev; break; } } } rc++; } } } #endif return rc; } #ifdef HAVE_WIRINGPI_H PI_THREAD (my_devices_loop) #else void *my_devices_loop(void *threadid) #endif { devices_list *device; char *addr = NULL, line[60], *p = NULL; FILE *fp; int temp, rc; syslog(LOG_NOTICE, "Thread my_devices_loop started"); if (debug) fprintf(stdout, "Thread my_devices_loop started\n"); /* * Loop forever until the external shutdown variable is set. */ for (;;) { /* * Process all devices. */ for (device = Config.devices; device; device = device->next) { if (my_shutdown) break; switch (device->type) { case DEVTYPE_W1: if (strncmp(device->address, (char *)"28", 2) == 0) { /* Read DS18B20 sensor */ addr = xstrcpy((char *)"/sys/bus/w1/devices/"); addr = xstrcat(addr, device->address); addr = xstrcat(addr, (char *)"/w1_slave"); if ((fp = fopen(addr, "r"))) { if (device->present != DEVPRESENT_YES) { syslog(LOG_NOTICE, "sensor %s is back", device->address); device->present = DEVPRESENT_YES; } /* * The output looks like: * 72 01 4b 46 7f ff 0e 10 57 : crc=57 YES * 72 01 4b 46 7f ff 0e 10 57 t=23125 */ fgets(line, 50, fp); line[strlen(line)-1] = '\0'; if ((line[36] == 'Y') && (line[37] == 'E')) { /* CRC is Ok, continue */ fgets(line, 50, fp); line[strlen(line)-1] = '\0'; strtok(line, (char *)"="); p = strtok(NULL, (char *)"="); rc = sscanf(p, "%d", &temp); if ((rc == 1) && (device->value != temp)) { device->value = temp; device->timestamp = time(NULL); } } else { syslog(LOG_NOTICE, "sensor %s CRC error", device->address); device->present = DEVPRESENT_ERROR; } fclose(fp); } else { if (device->present != DEVPRESENT_NO) { syslog(LOG_NOTICE, "sensor %s is missing", device->address); device->present = DEVPRESENT_NO; } } free(addr); addr = NULL; } break; #ifdef HAVE_WIRINGPI_H case DEVTYPE_DHT: if (device->subdevice == 0) { dht11_pin = device->gpiopin; dht11Read(); if (dht11_valid) { device->value = dht11_temperature * 1000; device->timestamp = time(NULL); device->present = DEVPRESENT_YES; } else { device->present = DEVPRESENT_ERROR; } } else if (device->subdevice == 1) { if (dht11_valid) { device->value = dht11_humidity * 1000; device->timestamp = time(NULL); device->present = DEVPRESENT_YES; } else { device->present = DEVPRESENT_ERROR; } } break; #endif default: break; } } usleep(10000); } syslog(LOG_NOTICE, "Thread my_devices_loop stopped"); if (debug) fprintf(stdout, "Thread my_devices_loop stopped\n"); return 0; }