void ds1820_refresh_all(){ for(int i = 0; i < 3; i++){ //Read temperatures from sensors whose conversion is ready if((temp[i] != DS1820_TEMP_NONE) && (ds1820_convert_ready(DS1820_PIN1 + i))){ temp[i] = ds1820_get_temp(DS1820_PIN1 + i); //Check for first values if(temp_min[i] == DS1820_TEMP_NONE){ temp_min[i] = temp[i]; } if(temp_max[i] == DS1820_TEMP_NONE){ temp_max[i] = temp[i]; } //Update min/max values if(temp[i] < temp_min[i]){ temp_min[i] = temp[i]; }else if(temp[i] > temp_max[i]){ temp_max[i] = temp[i]; } } //Start conversion for the next call if(ds1820_convert(DS1820_PIN1 + i)){ temp[i] = DS1820_TEMP_NONE; //Indication for ready check that there is no sensor //temp_min[DS1820_PIN1 + i] = DS1820_TEMP_NONE; //Reset min/max values //temp_max[DS1820_PIN1 + i] = DS1820_TEMP_NONE; }else if(temp[i] == DS1820_TEMP_NONE){ temp[i] = DS1820_TEMP_INIT; //Indication for ready check that there is a sensor } } }
//////////////////////////////////////////////////////////////////////////// // Interfacing Function //////////////////////////////////////////////////////////////////////////// void vTaskDS1820Convert( void *pvParameters ){ char buf[30]; static char print_buf[80]; int ii = 0; static float fTemp[NUM_SENSORS] = {0.0, 0.0}, fLastTemp[NUM_SENSORS] = {0.0, 0.0}; // initialise the bus ds1820_init(); if (ds1820_reset() ==PRESENCE_ERROR) { ds1820_error(PRESENCE_ERROR); vConsolePrint("Presence Error, Deleting DS1820 Task\r\n\0"); vTaskDelete(NULL); // if this task fails... delete it } // Allocate memory for sensors b[HLT] = (char *) pvPortMalloc (sizeof(rom)+1); b[MASH] = (char *) pvPortMalloc (sizeof(rom)+1); // b[CABINET] = (char *) malloc (sizeof(rom)+1); // Copy default values memcpy(b[HLT], HLT_TEMP_SENSOR, sizeof(HLT_TEMP_SENSOR)+1); memcpy(b[MASH], MASH_TEMP_SENSOR, sizeof(MASH_TEMP_SENSOR)+1); // memcpy(b[CABINET], CABINET_TEMP_SENSOR, sizeof(CABINET_TEMP_SENSOR)+1); ds1820_search(); //if (rom[0] == 0x10) if (rom[0] != 0) { sprintf(print_buf, "%02x-%02x-%02x-%02x-%02x-%02x-%02x-%02x\r\n\0",rom[0], rom[1], rom[2], rom[3], rom[4], rom[5], rom[6], rom[7]); vConsolePrint(print_buf); } else { vConsolePrint("NO SENSOR\r\n\0"); } for (;;) { ds1820_convert(); vTaskDelay(850/portTICK_RATE_MS); // wait for conversion // save values in array for use by application for (ii = 0 ; ii < NUM_SENSORS; ii++) { fTemp[ii] = ds1820_read_device(b[ii]); if (fTemp[ii] < 105.0 && fTemp[ii] > 0.0) { if ((fTemp[ii] < (temps[ii] + 5.0)) || (fTemp[ii] > (temps[ii] - 5.0)) || (fTemp[ii] <= 85.0 && fTemp[ii] >= 86.0)) { portENTER_CRITICAL(); // so that other task cannot access temps[] while we are saving. temps[ii] = fTemp [ii]; portEXIT_CRITICAL(); } } if (fTemp[ii] == 0.0) { vConsolePrint("zero values. Temp Bus Resetting\r\n\0"); ds1820_power_reset(); ds1820_reset(); } } taskYIELD(); } }
PROCESS_THREAD(rest_server_example, ev, data) { static struct etimer ds_periodic_timer; #if REST_RES_DS1820 static struct etimer ds_read_timer; #endif PROCESS_BEGIN(); PRINTF("Starting Erbium Example Server\n"); #ifdef RF_CHANNEL PRINTF("RF channel: %u\n", RF_CHANNEL); #endif #ifdef IEEE802154_PANID PRINTF("PAN ID: 0x%04X\n", IEEE802154_PANID); #endif PRINTF("uIP buffer: %u\n", UIP_BUFSIZE); PRINTF("LL header: %u\n", UIP_LLH_LEN); PRINTF("IP+UDP header: %u\n", UIP_IPUDPH_LEN); PRINTF("REST max chunk: %u\n", REST_MAX_CHUNK_SIZE); /* if static routes are used rather than RPL */ #if !UIP_CONF_IPV6_RPL && !defined (CONTIKI_TARGET_MINIMAL_NET) && !defined (CONTIKI_TARGET_NATIVE) set_global_address(); configure_routing(); #endif /* Initialize the OSD Hardware. */ hw_init(); /* Initialize the REST engine. */ rest_init_engine(); /* Activate the application-specific resources. */ #if REST_RES_DS1820 rest_activate_resource(&resource_ds1820); #endif #if REST_RES_DHT11 rest_activate_resource(&resource_dht11); #endif #if REST_RES_DHT11TEMP rest_activate_resource(&resource_dht11temp); #endif #if REST_RES_INFO rest_activate_resource(&resource_info); #endif #if defined (PLATFORM_HAS_LEDS) #if REST_RES_LEDS rest_activate_resource(&resource_leds); #endif #if REST_RES_TOGGLE rest_activate_resource(&resource_toggle); #endif #endif /* PLATFORM_HAS_LEDS */ #if defined (PLATFORM_HAS_TEMPERATURE) && REST_RES_TEMPERATURE SENSORS_ACTIVATE(temperature_sensor); rest_activate_resource(&resource_temperature); #endif #if defined (PLATFORM_HAS_BATTERY) && REST_RES_BATTERY SENSORS_ACTIVATE(battery_sensor); rest_activate_resource(&resource_battery); #endif /* Define application-specific events here. */ etimer_set(&ds_periodic_timer, MESURE_INTERVAL); while(1) { PROCESS_WAIT_EVENT(); #if defined (PLATFORM_HAS_BUTTON) if (ev == sensors_event && data == &button_sensor) { PRINTF("BUTTON\n"); } #endif /* PLATFORM_HAS_BUTTON */ if(etimer_expired(&ds_periodic_timer)) { PRINTF("Periodic\n"); etimer_reset(&ds_periodic_timer); #if REST_RES_DHT11 // DHT_Read_Data(&dht11_temp, &dht11_hum); DHT_Read_Data(&dht11_temp, &dht11_hum); #endif #if REST_RES_DS1820 if(ds1820_convert()){ etimer_set(&ds_read_timer, READ_TIME); } #endif } #if REST_RES_DS1820 if(etimer_expired(&ds_read_timer)) { PRINTF("DS1820_Read\n"); ds1820_read(); } #endif } /* while (1) */ PROCESS_END(); }