void ds1820_state_machine() { switch (ds1820_state) { case DS1820_IDLE: if (ds1820_reset()) return; ds1820_write(DS1820_CMD_SKIP_ROM); ds1820_write(DS1820_CMD_CONVERT_T); ds1820_state = DS1820_CONVERT; break; case DS1820_CONVERT: if (ds1820_read() == 0xFF) ds1820_state = DS1820_READ; break; case DS1820_READ: ds1820_read_scratchpad(); ds1820_state = DS1820_IDLE; break; } }
int main() { if(wiringPiSetup() == -1) return 1; float temp = 0.0f; while(1) { temp = ds1820_read(); printf("%.1f\n", temp); delay(1000); } }
static void ds1820_read_scratchpad() { uint8_t i, crc; ds1820_reset(); ds1820_write(DS1820_CMD_SKIP_ROM); ds1820_write(DS1820_CMD_READ); crc = 0; for (i = 0; i < 9; i++) { ds1820[i] = ds1820_read(); crc = _crc_ibutton_update(crc, ds1820[i]); } /* only update temperature if CRC ok */ if (!crc) { temperature[0] = ds1820[0]; temperature[1] = ds1820[1]; } }
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(); }