int JSON_RPC(read)(jsmn_node_t* pjn, fp_json_delegate_ack ack, void* ctx) //继承于fp_json_delegate_start { int ret = STATE_OK; char* err_msg = NULL; LOG_INFO("DELEGATE dht11.read.\r\n"); /* { "method":"dht11.read", "params":{} } */ //----温度高8位== U8T_data_H------ //----温度低8位== U8T_data_L------ //----湿度高8位== U8RH_data_H----- //----湿度低8位== U8RH_data_L----- //----校验 8位 == U8checkdata----- u8_t temp_h, temp_l, humi_h, humi_l, chk; if(STATE_OK != (ret = dht11_start()) || STATE_OK != (ret = dht11_read_byte(&humi_h))|| STATE_OK != (ret = dht11_read_byte(&humi_l))|| STATE_OK != (ret = dht11_read_byte(&temp_h))|| STATE_OK != (ret = dht11_read_byte(&temp_l))|| STATE_OK != (ret = dht11_read_byte(&chk))) {} dht11_stop(); if(STATE_OK != ret){ err_msg = "read time out."; LOG_WARN("dht11:%s %d.\r\n", err_msg, ret); goto exit_err; } if(chk != temp_h+temp_l+humi_h+humi_l){ ret = STATE_ERROR; err_msg = "checksum err."; LOG_WARN("dht11:%s.\r\n", err_msg); goto exit_err; } if(ack){ char json[128]; u32_t n = 0; n += _snprintf(json+n, sizeof(json)-n, "{\"result\":{\"temperature\":%u.%u,\"humidity\":%u.%u}}",temp_h, temp_l, humi_h, humi_l); jsmn_node_t jn = {json, n, NULL}; ack(&jn, ctx); //这里对json请求 回应json } return ret; //exit_safe: if(ack) jsmn.delegate_ack_result(ack, ctx, ret); return ret; exit_err: if(ack) jsmn.delegate_ack_err(ack, ctx, ret, err_msg); return ret; }
int8_t dht_getdata(int16_t *temperature, int16_t *humidity) { #endif uint8_t bits[5]; uint8_t i,j = 0; if(!RT_IO_ENABLED(IO_DHT)) return -1; memset(bits, 0, sizeof(bits)); #if 1 dht11_start(); #else //reset port DHT_DDR |= (1<<DHT_INPUTPIN); //output DHT_PORT |= (1<<DHT_INPUTPIN); //high _delay_ms(100); //send request DHT_PORT &= ~(1<<DHT_INPUTPIN); //low #if DHT_TYPE == DHT_DHT11 //_delay_ms(18); _delay_ms(20); #elif DHT_TYPE == DHT_DHT22 _delay_us(500); #endif DHT_PORT |= (1<<DHT_INPUTPIN); //high DHT_DDR &= ~(1<<DHT_INPUTPIN); //input _delay_us(40); //_delay_us(70); #endif #if 0 if(dht11_check_response()) return -1; #else //check start condition 1 if((DHT_PIN & (1<<DHT_INPUTPIN))) { DPUTS("no start condition 1"); return -1; } _delay_us(80); //check start condition 2 if(!(DHT_PIN & (1<<DHT_INPUTPIN))) { DPUTS("no start condition 2"); return -1; } _delay_us(80); //_delay_us(40); #endif //read the data uint16_t timeoutcounter = 0; for (j=0; j<5; j++) { //read 5 byte uint8_t result=0; for(i=0; i<8; i++) {//read every bit timeoutcounter = 0; while(!(DHT_PIN & (1<<DHT_INPUTPIN))) { //wait for an high input (non blocking) timeoutcounter++; if(timeoutcounter > DHT_TIMEOUT) { DPUTS("tmo 1"); return -1; //timeout } } _delay_us(30); if(DHT_PIN & (1<<DHT_INPUTPIN)) //if input is high after 30 us, get result result |= (1<<(7-i)); timeoutcounter = 0; while(DHT_PIN & (1<<DHT_INPUTPIN)) { //wait until input get low (non blocking) timeoutcounter++; if(timeoutcounter > DHT_TIMEOUT) { DPUTS("tmo 2"); return -1; //timeout } } } bits[j] = result; } #if 1 // reset port DHT_DDR |= (1<<DHT_INPUTPIN); //output DHT_PORT |= (1<<DHT_INPUTPIN); //low _delay_ms(100); #endif // check checksum if((uint8_t)(bits[0] + bits[1] + bits[2] + bits[3]) != bits[4]) { DPUTS("checksum"); return -1; } // return temperature and humidity #if DHT_TYPE == DHT_DHT11 *temperature = bits[2]; *humidity = bits[0]; #elif DHT_TYPE == DHT_DHT22 uint16_t rawhumidity = bits[0]<<8 | bits[1]; uint16_t rawtemperature = bits[2]<<8 | bits[3]; if(rawtemperature & 0x8000) { *temperature = (float)((rawtemperature & 0x7FFF) / 10.0) * -1.0; } else { *temperature = (float)(rawtemperature)/10.0; } *humidity = (float)(rawhumidity)/10.0; #endif return 0; }