/*---------------------------------------------------------------------------*/ PROCESS_THREAD(sensors_sample_process, ev, data) { /* Variables are declared static to ensure their values are kept between kernel calls. */ static struct etimer timer; /* Any process must start with this. */ PROCESS_BEGIN(); /* Set the etimer to generate an event in one second. */ etimer_set(&timer, CLOCK_CONF_SECOND); while(1) { /* Wait for an event. */ PROCESS_WAIT_EVENT(); /* Got the timer's event~ */ if (ev == PROCESS_EVENT_TIMER) { /* Read raw temperature value. */ int16_t temp_raw = sensor_temp_get_raw(); /* Read temperature value in specified unit (C/F). */ int16_t temp = sensor_temp_get(TEMP_UNIT_CELCIUS); /* Read raw light value. */ int16_t light_raw = sensor_light_get_raw(); printf("temp_raw:%d\ntemp:%d\nlight_raw:%d", temp_raw, temp, light_raw); /* Reset the etimer so it will generate another event after the exact same time. */ etimer_reset(&timer); } } // while (1) /* Any process must end with this, even if it is never reached. */ PROCESS_END(); }
void temperature_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { char buf[32]; int16_t temp = sensor_temp_get(TEMP_UNIT_CELCIUS); sprintf(buf, "%d", temp); REST.set_header_content_type(response, REST.type.TEXT_PLAIN); REST.set_response_payload(response, (uint8_t *)buf, strlen(buf)); }
void temperature_periodic_handler(resource_t *r) { static uint32_t obs_counter = 0; char content[32]; obs_counter++; int16_t temp = sensor_temp_get(TEMP_UNIT_CELCIUS); /* Build notification. */ coap_packet_t notification[1]; /* This way the packet can be treated as pointer as usual. */ coap_init_message(notification, COAP_TYPE_NON, CONTENT_2_05, 0 ); coap_set_payload(notification, content, snprintf(content, sizeof(content), "%u", temp)); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, obs_counter, notification); }
/*---------------------------------------------------数据采集上传模考---------------------------------------------*/ static void udp_send_data(struct udp_tx *udp_tx_info) { static char pdata_buf[20]; char data_buf[3]; #if sht11 float tc,hc; sht11_init(); unsigned int tempera = sht11_temp(); unsigned int humidity = sht11_humidity(); tc=sht11_TemperatureC(tempera); hc=sht11_Humidity(tempera,humidity); printf("sorce Tc:%d;Hc:%d\n",(int)tc,(int)hc); // tc=25.5; // hc=57.2; printf("Tc:%d;Hc:%d\n",(int)tc,(int)hc); pdata_buf[0]=0x02; sprintf(data_buf,"%d",(int)tc); memcpy(&pdata_buf[1],data_buf,3); pdata_buf[4]=0x03; sprintf(data_buf,"%d",(int)hc); memcpy(&pdata_buf[5],data_buf,3); // sprintf(temp_data_buf,"%d.%d",(int)hc,((int)(hc*10))%10); printf("temp_data:%s\n",pdata_buf); #endif #if sensor int16_t temperature; temperature = sensor_temp_get(TEMP_UNIT_CELCIUS); pdata_buf[0]=0x02; sprintf(data_buf,"%d",temperature); memcpy(&pdata_buf[1],data_buf,3); printf("temp_data:%s\n",pdata_buf); #endif #if light_sensor int16_t illumination; illumination=60; // modbus_init(); // illumination= modbus_get(); printf("illumination_data:%d\n",illumination); data_buf[0]=0x00; data_buf[1]=0xff&(illumination>>8); data_buf[2]=0xff&illumination; pdata_buf[0]=0x04; memcpy(&pdata_buf[1],data_buf,3); printf("temp_data:%s\n",pdata_buf); #endif #if bh1750_sensor int16_t illumination; illumination=60; bh1750_init(); illumination= bh1750_light(); printf("illumination_data:%d\n",illumination); data_buf[0]=0x00; data_buf[1]=0xff&(illumination>>8); data_buf[2]=0xff&illumination; pdata_buf[0]=0x04; memcpy(&pdata_buf[1],data_buf,3); printf("temp_data:%s\n",pdata_buf); #endif uint8_t sum = 0; udp_tx_info->buf[0] = udp_header[0]; udp_tx_info->buf[1] = udp_header[1]; udp_tx_info->buf[2] =version[0]; udp_tx_info->buf[3] = DATA_SEND ; //0x01 udp_tx_info->buf[4]=user.userid[3]; udp_tx_info->buf[5]=user.userid[2]; udp_tx_info->buf[6]=user.userid[1]; udp_tx_info->buf[7]=user.userid[0]; udp_tx_info->buf[8] = K-8; memcpy(&udp_tx_info->buf[9],udp_id,K-8); memcpy(&udp_tx_info->buf[K+1],device_type,4); udp_tx_info->buf[K+5] = 0x00; udp_tx_info->buf[K+6] = 0x14; memcpy(&udp_tx_info->buf[K+7],pdata_buf,20); sum = calac_checksum(udp_tx_info); udp_tx_info->buf[K+27] = sum; udp_tx_info->buf[K+28] = 0x0D; udp_tx_info->buf[K+29] = 0x0A; udp_tx_info->len = K+30; }