Пример #1
0
int main(void)
{
	SystemInit();
	UARTx_Init();
	Printf_SystemRccClocks();
	RTC_Init();
	Hal_Init();
	McuStatusInit();
	
	while(1)
	{
		MessageHandle();
		KEY_Handle();		
		IR_Handle();
		DHT11_Read_Data(&Device_ReadStruct.Temperature, &Device_ReadStruct.Humidity);
		ReportDevStatusHandle();
	}
}
Пример #2
0
OSStatus temp_hum_sensor_read(int32_t *temperature,  uint32_t *humidity)
{
  OSStatus err = kUnknownErr;
  uint8_t ret = 0;
  uint8_t dht11_temp = 0;
  uint8_t dht11_hum = 0;
  int32_t bme280_temp = 0;
  uint32_t bme280_hum = 0;
  uint32_t bme280_press = 0;
  
  switch(temp_hum_sensor_type){
  case MICOKIT_TEMP_HUM_SENSOR_BME280:
    {
      err = bme280_data_readout(&bme280_temp, &bme280_press, &bme280_hum);
      *temperature = bme280_temp/100;
      *humidity = bme280_hum/1024;
      break;
    }
  case MICOKIT_TEMP_HUM_SENSOR_DHT11:
    {
      ret = DHT11_Read_Data(&dht11_temp, &dht11_hum);
      if(0 != ret || dht11_hum == 0 ){
        err = kReadErr;
      }
      else{
        *temperature = (int32_t)dht11_temp;
        *humidity = (uint32_t)dht11_hum;
        err = kNoErr;
      }
      break;
    }
  default:
    err = kUnsupportedErr;
    break;
  }
  
  return err;
}
void update_dht11_sensor(void)
{
  uint8_t dht11_ret = 1;
  char temp_value[10];
  char hum_value[10];
  
  dht11_ret = DHT11_Read_Data(&dht11_temp_data, &dht11_hum_data);
  if(0 == dht11_ret)
  {
    itoa(dht11_temp_data,temp_value);
    itoa(dht11_hum_data,hum_value);
    if(ARRAYENT_SUCCESS!=ArrayentSetProperty("temp",temp_value))
    {
      user_log("service have rejected!\n\r");
    }
    mico_thread_msleep(200);
    if(ARRAYENT_SUCCESS!=ArrayentSetProperty("hsv",hum_value))
    {
      user_log("service have rejected!\n\r");
    }
    
  }
}
Пример #4
0
void micokit_ext_mfg_test(mico_Context_t *inContext)
{
  OSStatus err = kUnknownErr;
  char str[64] = {'\0'};
  char mac[6];
  
  int rgb_led_hue = 0;
  
  uint8_t dht11_ret = 0;
  uint8_t dht11_temp_data = 0;
  uint8_t dht11_hum_data = 0;
  
  int light_ret = 0;
  uint16_t light_sensor_data = 0;
  
  int infrared_ret = 0;
  uint16_t infrared_reflective_data = 0;
  
  int32_t bme280_temp = 0;
  uint32_t bme280_hum = 0;
  uint32_t bme280_press = 0;
  
  UNUSED_PARAMETER(inContext);
  
  mico_rtos_init_semaphore(&mfg_test_state_change_sem, 1); 
  err = MICOAddNotification( mico_notify_WIFI_SCAN_COMPLETED, (void *)mico_notify_WifiScanCompleteHandler );
  require_noerr( err, exit );
  
  while(1){
    switch(mfg_test_module_number){
    case 0:  // mfg mode start
      {
        sprintf(str, "%s\r\nStart:\r\n%s\r\n%s", "TEST MODE", "  next: Key2", "  prev: Key1");
        mf_printf(str);
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, MICO_WAIT_FOREVER));
        break;
      }
    case 1:  // OLED
      {
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          sprintf(str, "%s OLED\r\n", OLED_MFG_TEST_PREFIX);
          mf_printf(str);
          mico_thread_msleep(300);
          
          mf_printf(mfg_test_oled_test_string);
          mico_thread_msleep(300);
        }
        OLED_Clear();
        break;
      }
    case 2:  // RGB_LED
      {
        sprintf(str, "%s RGB LED\r\nBlink: \r\n      R=>G=>B", OLED_MFG_TEST_PREFIX);
        mf_printf(str);
        
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          hsb2rgb_led_open(rgb_led_hue, 100, 50);
          rgb_led_hue += 120;
          if(rgb_led_hue >= 360){
            rgb_led_hue = 0;
          }
          mico_thread_msleep(300);
        }
        hsb2rgb_led_open(0, 0, 0);
        break;
      }
    case 3: // infrared sensor
      {
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          infrared_ret = infrared_reflective_read(&infrared_reflective_data);
          if(0 == infrared_ret){ 
            sprintf(str, "%s Infrared\r\nInfrared: %d", OLED_MFG_TEST_PREFIX,
                    infrared_reflective_data);
            mf_printf(str);
          }
          mico_thread_msleep(300);
        }
        break;
      }
    case 4: // DC Motor
      {
        sprintf(str, "%s DC Motor\r\nRun:\r\n     on : 500ms\r\n     off: 500ms", OLED_MFG_TEST_PREFIX);
        mf_printf(str);
        
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          dc_motor_set(1);
          mico_thread_msleep(500);
          dc_motor_set(0);
          mico_thread_msleep(500);
        }
        dc_motor_set(0);
        break;
      }
    case 5: // BME280
      {
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          err = bme280_sensor_init();
          if(kNoErr != err){
            sprintf(str, "%s BME280\r\nMoule not found!", OLED_MFG_TEST_PREFIX);
            mf_printf(str);
            // goto next mdoule
            mico_thread_msleep(500);
            mfg_test_module_number = (mfg_test_module_number+1)%(MFG_TEST_MAX_MODULE_NUM+1);
            break;
          }
          else{
            err = bme280_data_readout(&bme280_temp, &bme280_press, &bme280_hum);
            if(kNoErr == err){
              sprintf(str, "%s BME280\r\nT: %3.1fC\r\nH: %3.1f%%\r\nP: %5.2fkPa", OLED_MFG_TEST_PREFIX,
                      (float)bme280_temp/100, (float)bme280_hum/1024, (float)bme280_press/1000);
              mf_printf(str);
            }
            else{
              sprintf(str, "%s BME280\r\nRead error!", OLED_MFG_TEST_PREFIX);
              mf_printf(str);
            }
          }
          mico_thread_msleep(500);
        }
        break;
      }
    case 6: // DHT11
      {
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          dht11_ret = DHT11_Read_Data(&dht11_temp_data, &dht11_hum_data);
          if(0 == dht11_ret){
            sprintf(str, "%s DHT11\r\nT: %3.1fC\r\nH: %3.1f%%", OLED_MFG_TEST_PREFIX,
                    (float)dht11_temp_data, (float)dht11_hum_data);
            mf_printf(str);
          }
          mico_thread_sleep(1);   // DHT11 must >= 1s
        }
        break;
      }
    case 7:   // Light sensor
      {
        while(kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, 0))
        {
          light_ret = light_sensor_read(&light_sensor_data);
          if(0 == light_ret){
            sprintf(str, "%s Light\r\nLight: %d", OLED_MFG_TEST_PREFIX,
                    light_sensor_data);
            mf_printf(str);
          }
          mico_thread_msleep(300);
        }
        break;
      }
    case 8: // wifi
      {
        wlan_get_mac_address(mac);
        sprintf(str, "%s Wi-Fi\r\nMAC:\r\n    %02X%02X%02X%02X%02X%02X", OLED_MFG_TEST_PREFIX,
                mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
        mf_printf(str);
        //mico_thread_msleep(500);
        
        scanap_done = false;
        micoWlanStartScan();
        while((!scanap_done) || (kNoErr != mico_rtos_get_semaphore(&mfg_test_state_change_sem, MICO_WAIT_FOREVER)));
        break;
      }
    default:
      goto exit;  // error
      break;
    }
  }
  
exit:
  mico_thread_sleep(MICO_NEVER_TIMEOUT);
}
Пример #5
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;
}
Пример #6
0
/* 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
}