unsigned long read_cnt(long offset, int argc) { long count; int i; count = 0; while( sizecvt(digitalRead(data_pin)) ); digitalWrite(clock_pin, LOW); delayMicroseconds(.2); for(i=0;i<24 ; i++) { digitalWrite(clock_pin, HIGH); delayMicroseconds(.2); count = count << 1; if ( sizecvt(digitalRead(data_pin)) > 0 ) { count++; } digitalWrite(clock_pin, LOW); delayMicroseconds(.2); } digitalWrite(clock_pin, HIGH); delayMicroseconds(.2); digitalWrite(clock_pin, LOW); delayMicroseconds(.2); // count = ~0x1800000 & count; // count = ~0x800000 & count; if (count & 0x800000) { count |= (long) ~0xffffff; } // if things are broken this will show actual data if (argc < 2 ) { for (i=32;i;i--) { printf("%d ", ((count-offset) & ( 1 << i )) > 0 ); } printf("n: %10d - ", count - offset); printf("\n"); } return (count - offset); }
void set_gain(int r) { int i; // r = 0 - 128 gain ch a // r = 1 - 32 gain ch b // r = 2 - 63 gain ch a while( sizecvt(digitalRead(data_pin)) ); for (i=0;i<24+r;i++) { digitalWrite(clock_pin, HIGH); delayMicroseconds(1); digitalWrite(clock_pin, LOW); delayMicroseconds(1); } }
static int read_dht22_dat() { uint8_t laststate = LOW; uint8_t counter = 0; uint8_t j = 0, i, ref, max = 255; float t, h; dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; // Prepare to send start signal pinMode( DHTPIN, OUTPUT ); digitalWrite( DHTPIN, HIGH ); delay( 100 ); // Simulate pull-up resistor // Send start signal digitalWrite( DHTPIN, LOW ); delay( 10 ); // 1-10 ms digitalWrite( DHTPIN, HIGH ); delayMicroseconds( 20 ); // 20-40 us // Prepare to read pinMode( DHTPIN, INPUT ); // Detect change and read data for ( i = 0; i < MAXTIMINGS; i++ ) { counter = 0; while ( sizecvt( digitalRead( DHTPIN ) ) == laststate ) { delayMicroseconds( 4 ); if ( ++counter > max ) { break; } } laststate = sizecvt( digitalRead( DHTPIN ) ); if ( counter == 255 ) break; // Signal HIGH if ( i%2 == 1 ) { // Data bit if ( i > 2 ) { dht22_dat[j/8] <<= 1; if ( counter >= ref ) dht22_dat[j/8] |= 1; j++; // Reference signal 80us } else if ( i == 2 ) { max = counter; } // Signal LOW - 50us reference } else { ref = counter; } } // Calculate checksum, humidity and temperature i = ( ( dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3] ) & 0xFF ); h = ( ( int ) dht22_dat[0] * 256 + ( int ) dht22_dat[1] ) / 10.0; t = ( ( int )( dht22_dat[2] & 0x7F ) * 256 + ( int ) dht22_dat[3] ) / 10.0; if ( (dht22_dat[2] & 0x80 ) != 0 ) t *= -1; // Data OK! 40 bits + checksum if ( (j >= 40 ) && ( dht22_dat[4] == i ) ) { printf( "Temperature: %.1f*C \tHumidity: %.1f%%\n", t, h ); return 1; // Error } else { fprintf( stderr, "Invalid data: %d bits [%02x %02x %02x %02x %02x] Chk=%02x T:%.1f H:%.1f\n", j, dht22_dat[0], dht22_dat[1], dht22_dat[2], dht22_dat[3], dht22_dat[4], i, t, h ); return 0; } }
static int read_dht22_dat() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; // pull pin down for 18 milliseconds pinMode(DHTPIN, OUTPUT); digitalWrite(DHTPIN, HIGH); delay(10); digitalWrite(DHTPIN, LOW); delay(18); // then pull it up for 40 microseconds digitalWrite(DHTPIN, HIGH); delayMicroseconds(40); // prepare to read the pin pinMode(DHTPIN, INPUT); // detect change and read data for ( i=0; i< MAXTIMINGS; i++) { counter = 0; while (sizecvt(digitalRead(DHTPIN)) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = sizecvt(digitalRead(DHTPIN)); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes dht22_dat[j/8] <<= 1; if (counter > 16) dht22_dat[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) && (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) { float t, h; h = (float)dht22_dat[0] * 256 + (float)dht22_dat[1]; h /= 10; t = (float)(dht22_dat[2] & 0x7F)* 256 + (float)dht22_dat[3]; t /= 10.0; if ((dht22_dat[2] & 0x80) != 0) t *= -1; printf("{\"data\": {\"humidity\": %.2f, \"temperature\": %.2f}}\n", h, t ); return 1; } else { printf("{\"err\": {\"message\": \"DATA_NOT_GOOD\"}}\n"); return 0; } }
static void *dht22Parse(void *param) { struct protocol_threads_t *node = (struct protocol_threads_t *)param; struct JsonNode *json = (struct JsonNode *)node->param; struct JsonNode *jid = NULL; struct JsonNode *jchild = NULL; int *id = 0; int nrid = 0, y = 0, interval = 10, nrloops = 0; double temp_offset = 0.0, humi_offset = 0.0, itmp = 0.0; dht22_threads++; if((jid = json_find_member(json, "id"))) { jchild = json_first_child(jid); while(jchild) { if(json_find_number(jchild, "gpio", &itmp) == 0) { id = REALLOC(id, (sizeof(int)*(size_t)(nrid+1))); id[nrid] = (int)round(itmp); nrid++; } jchild = jchild->next; } } if(json_find_number(json, "poll-interval", &itmp) == 0) interval = (int)round(itmp); json_find_number(json, "temperature-offset", &temp_offset); json_find_number(json, "humidity-offset", &humi_offset); while(dht22_loop) { if(protocol_thread_wait(node, interval, &nrloops) == ETIMEDOUT) { pthread_mutex_lock(&dht22lock); for(y=0;y<nrid;y++) { int tries = 5; unsigned short got_correct_date = 0; while(tries && !got_correct_date && dht22_loop) { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i = 0; int dht22_dat[5] = {0,0,0,0,0}; // pull pin down for 18 milliseconds pinMode(id[y], OUTPUT); digitalWrite(id[y], HIGH); usleep(500000); // 500 ms // then pull it up for 40 microseconds digitalWrite(id[y], LOW); usleep(20000); // prepare to read the pin pinMode(id[y], INPUT); // detect change and read data for(i=0; (i<MAXTIMINGS && dht22_loop); i++) { counter = 0; delayMicroseconds(10); while(sizecvt(digitalRead(id[y])) == laststate && dht22_loop) { counter++; delayMicroseconds(1); if(counter == 255) { break; } } laststate = sizecvt(digitalRead(id[y])); if(counter == 255) break; // ignore first 3 transitions if((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes dht22_dat[(int)((double)j/8)] <<= 1; if(counter > 16) dht22_dat[(int)((double)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) && (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF))) { got_correct_date = 1; double h = dht22_dat[0] * 256 + dht22_dat[1]; double t = (dht22_dat[2] & 0x7F)* 256 + dht22_dat[3]; t += temp_offset; h += humi_offset; if((dht22_dat[2] & 0x80) != 0) t *= -1; dht22->message = json_mkobject(); JsonNode *code = json_mkobject(); json_append_member(code, "gpio", json_mknumber(id[y], 0)); json_append_member(code, "temperature", json_mknumber(t/10, 1)); json_append_member(code, "humidity", json_mknumber(h/10, 1)); json_append_member(dht22->message, "message", code); json_append_member(dht22->message, "origin", json_mkstring("receiver")); json_append_member(dht22->message, "protocol", json_mkstring(dht22->id)); if(pilight.broadcast != NULL) { pilight.broadcast(dht22->id, dht22->message); } json_delete(dht22->message); dht22->message = NULL; } else { logprintf(LOG_DEBUG, "dht22 data checksum was wrong"); tries--; protocol_thread_wait(node, 1, &nrloops); } } } pthread_mutex_unlock(&dht22lock); } } pthread_mutex_unlock(&dht22lock); FREE(id); dht22_threads--; return (void *)NULL; }
static int readData() { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; dht22Data[0] = dht22Data[1] = dht22Data[2] = dht22Data[3] = dht22Data[4] = 0; // pull pin down for 18 milliseconds pinMode(dht22Pin, OUTPUT); digitalWrite(dht22Pin, HIGH); delay(10); digitalWrite(dht22Pin, LOW); delay(18); // then pull it up for 40 microseconds digitalWrite(dht22Pin, HIGH); delayMicroseconds(40); // prepare to read the pin pinMode(dht22Pin, INPUT); // detect change and read data for (i = 0; i < MAX_TIMINGS; i++) { counter = 0; while (sizecvt(digitalRead(dht22Pin)) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = sizecvt(digitalRead(dht22Pin)); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i % 2 == 0)) { // shove each bit into the storage bytes dht22Data[j / 8] <<= 1; if (counter > 16) dht22Data[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) && (dht22Data[4] == ((dht22Data[0] + dht22Data[1] + dht22Data[2] + dht22Data[3]) & 0xFF))) { float t, h; h = (float) dht22Data[0] * 256 + (float) dht22Data[1]; h /= 10; t = (float) (dht22Data[2] & 0x7F) * 256 + (float) dht22Data[3]; t /= 10.0; if ((dht22Data[2] & 0x80) != 0) { t *= -1; } time_t ts = time(NULL); printf("<dht22><humidity>%.2f</humidity><temperature>%.2f</temperature><timestamp>%ld</timestamp></dht22>\n", h, t, ts); return 1; } else { return 0; } }
static int readDHT(int pin, int *temp, int *rh) { uint8_t laststate = HIGH; uint8_t counter = 0; uint8_t j = 0, i; dht22_dat[0] = dht22_dat[1] = dht22_dat[2] = dht22_dat[3] = dht22_dat[4] = 0; // pull pin down for 18 milliseconds pinMode(pin, OUTPUT); // digitalWrite(pin, HIGH); // delay(10); digitalWrite(pin, LOW); delay(18); // then pull it up for 40 microseconds digitalWrite(pin, HIGH); delayMicroseconds(40); // prepare to read the pin pinMode(pin, INPUT); // detect change and read data for ( i=0; i< MAXTIMINGS; i++) { counter = 0; while (sizecvt(digitalRead(pin)) == laststate) { counter++; delayMicroseconds(1); if (counter == 255) { break; } } laststate = sizecvt(digitalRead(pin)); if (counter == 255) break; // ignore first 3 transitions if ((i >= 4) && (i%2 == 0)) { // shove each bit into the storage bytes dht22_dat[j/8] <<= 1; if (counter > 16) dht22_dat[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) && (dht22_dat[4] == ((dht22_dat[0] + dht22_dat[1] + dht22_dat[2] + dht22_dat[3]) & 0xFF)) ) { *rh = dht22_dat[0] * 256 + dht22_dat[1]; *temp = (dht22_dat[2] & 0x7F)* 256 + dht22_dat[3]; /* negative temp */ if ((dht22_dat[2] & 0x80) != 0) *temp = -*temp; return 1; } else { //printf("Data not good, skip\n"); return 0; } }