int main(void) { // init sys SystemInit(); // init peripheral UART_Configuration(); delay_init(); KEY_GPIO_Init(); TIM3_Int_Init(100,7199); Motor_Init(); RGB_LED_Init(); DHT11_Init(); IR_Init(); // init vars GenProtocolInit(); BuzProtocolInit(); UartInit(); while(1) { // handle uart msg HandleMsg(); // handle key events HandleKey(); // business logic tick BuzTick(); } }
void Hal_Init(void) { RGB_LED_Init(); printf("RGB LED Init OK\r\n"); LED_GPIO_Init(); printf("LED Init OK\r\n"); TIM3_Int_Init(7199,9); //1ms SystemTimeCount + 1 printf("SystemTime Init OK\r\n"); KEY_GPIO_Init(); printf("KEY Init OK\r\n"); Motor_Init(); printf("Motor Init OK\r\n"); Delay_Init(72); DHT11_Init(); printf("DHT11 Init OK\r\n"); IR_Init(); printf("IR Init OK\r\n"); OLED_Init(); OLED_ShowString(40, 0, "GoKit"); OLED_ShowString(0, 32, "www.gizwits.com"); }
OSStatus temp_hum_sensor_init(void) { OSStatus err = kUnknownErr; uint8_t ret = 0; err = bme280_sensor_init(); if(kNoErr != err){ // bme280 not work, use DHT11 bme280_sensor_deinit(); ret = DHT11_Init(); if(0 != ret){ // init error err = kNoResourcesErr; } else{ temp_hum_sensor_type = MICOKIT_TEMP_HUM_SENSOR_DHT11; err = kNoErr; } } else{ // use bme280 temp_hum_sensor_type = MICOKIT_TEMP_HUM_SENSOR_BME280; } return err; }
//从DHT11读取一次数据 //temp:温度值(范围:0~50°) //humi:湿度值(范围:20%~90%) //返回值:0,正常;1,读取失败 uint8_t DHT11_Read_Data(uint8_t *temp,uint8_t *temp0,uint8_t *humi,uint8_t *humi0) { uint8_t buf[5]; uint8_t i; DHT11_Init(); DHT11_Rst(); if(DHT11_Check()==0) { for(i=0;i<5;i++)//读取40位数据 { buf[i]=DHT11_Read_Byte(); } if((buf[0]+buf[1]+buf[2]+buf[3])==buf[4]) { *humi=buf[0]; *humi0=buf[1]; *temp=buf[2]; *temp0=buf[3]; } }else return 1; return 0; }
/* user main function, called by AppFramework after system init done && wifi * station on in user_main thread. */ OSStatus user_main( app_context_t * const app_context ) { user_log_trace(); OSStatus err = kUnknownErr; json_object *send_json_object = NULL; const char *upload_data = NULL; uint8_t ret = 0; uint8_t dht11_temperature = 0; uint8_t dht11_humidity = 0; // update 2~4 lines on OLED char oled_show_line[OLED_DISPLAY_MAX_CHAR_PER_ROW+1] = {'\0'}; require(app_context, exit); // init humiture sensor DHT11 ret = DHT11_Init(); if(0 != ret) // init error { err = kNoResourcesErr; user_log("DHT11 init failed!"); goto exit; } else { err = kNoErr; } while(1) { mico_thread_sleep(2); // data acquisition && upload every 2 seconds // check fogcloud connect status if(!app_context->appStatus.fogcloudStatus.isCloudConnected) { continue; } // fogcloud connected, do data acquisition ret = DHT11_Read_Data(&dht11_temperature, &dht11_humidity); if(0 != ret) { err = kReadErr; } else { err = kNoErr; // temperature/humidity display on OLED, each line 16 chars max OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_2, "Demo Read H/T "); // clean line2 memset(oled_show_line, '\0', OLED_DISPLAY_MAX_CHAR_PER_ROW+1); snprintf(oled_show_line, OLED_DISPLAY_MAX_CHAR_PER_ROW+1, "T: %2dC ", dht11_temperature); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_3, (uint8_t*)oled_show_line); memset(oled_show_line, '\0', OLED_DISPLAY_MAX_CHAR_PER_ROW+1); snprintf(oled_show_line, OLED_DISPLAY_MAX_CHAR_PER_ROW+1, "H: %2d%% ", dht11_humidity); OLED_ShowString(OLED_DISPLAY_COLUMN_START, OLED_DISPLAY_ROW_4, (uint8_t*)oled_show_line); // create json object && upload to cloud send_json_object = json_object_new_object(); if(NULL == send_json_object) { user_log("create json object error!"); err = kNoMemoryErr; } else { // create json object && data string to upload json_object_object_add(send_json_object, "dht11_temperature", json_object_new_int(dht11_temperature)); json_object_object_add(send_json_object, "dht11_humidity", json_object_new_int(dht11_humidity)); upload_data = json_object_to_json_string(send_json_object); if(NULL == upload_data) { user_log("create upload data string error!"); err = kNoMemoryErr; } else { // upload data string to fogcloud, the seconde param(NULL) means send to defalut topic: '<device_id>/out' MiCOFogCloudMsgSend(app_context, NULL, (unsigned char*)upload_data, strlen(upload_data)); user_log("upload data success!\r\ntopic=%s/out\tdht11_temperature=%d, dht11_humidity=%d", app_context->appConfig->fogcloudConfig.deviceId, dht11_temperature, dht11_humidity); err = kNoErr; } // free json object memory json_object_put(send_json_object); send_json_object = NULL; } } } exit: user_log("ERROR: user_main exit with err=%d", err); return err; }
/* Message upload thread * Get DHT11 temperature/humidity data && upload to cloud */ void user_upstream_thread(void* arg) { user_log_trace(); OSStatus err = kUnknownErr; app_context_t *app_context = (app_context_t *)arg; json_object *send_json_object = NULL; const char *upload_data = NULL; uint8_t ret = 0; require(app_context, exit); /* init humiture sensor DHT11 */ do{ ret = DHT11_Init(); if(0 != ret){ // init error user_log("DHT11 init failed!"); err = kNoResourcesErr; mico_thread_sleep(1); // sleep 1s then retry } else{ err = kNoErr; break; } }while(kNoErr != err); /* thread loop */ while(1){ // do data acquisition ret = DHT11_Read_Data(&dht11_temperature, &dht11_humidity); if(0 != ret){ err = kReadErr; } else{ err = kNoErr; user_log("dht11_temperature=%d, dht11_humidity=%d",dht11_temperature, dht11_humidity); // create json object to format upload data send_json_object = json_object_new_object(); if(NULL == send_json_object){ user_log("create json object error!"); err = kNoMemoryErr; } else{ // add temperature/humidity data into a json oject json_object_object_add(send_json_object, "dht11_temperature", json_object_new_int(dht11_temperature)); json_object_object_add(send_json_object, "dht11_humidity", json_object_new_int(dht11_humidity)); json_object_object_add(send_json_object, "lamp_switch", json_object_new_boolean(lamp_switch)); json_object_object_add(send_json_object, "pump_switch", json_object_new_boolean(pump_switch)); // json_object_object_add(send_json_object, "rgbled_switch", json_object_new_boolean(rgbled_switch)); // json_object_object_add(send_json_object, "hasImage", json_object_new_boolean(hasImage)); upload_data = json_object_to_json_string(send_json_object); if(NULL == upload_data){ user_log("create upload data string error!"); err = kNoMemoryErr; } else{ // check fogcloud connect status if(app_context->appStatus.fogcloudStatus.isCloudConnected){ // upload data string to fogcloud, the seconde param(NULL) means send to defalut topic: '<device_id>/out' MiCOFogCloudMsgSend(app_context, NULL, (unsigned char*)upload_data, strlen(upload_data)); // user_log("upload data success! \t topic=%s/out \t dht11_temperature=%d, dht11_humidity=%d", // app_context->appConfig->fogcloudConfig.deviceId, // dht11_temperature, dht11_humidity); err = kNoErr; } } // free json object memory json_object_put(send_json_object); send_json_object = NULL; } } mico_thread_sleep(2); // data acquisition && upload every 2 seconds } exit: if(kNoErr != err){ user_log("ERROR: user_uptream exit with err=%d", err); } mico_rtos_delete_thread(NULL); // delete current thread }