void voltage_handler(void* request, void* response, uint8_t *buffer, uint16_t preferred_size, int32_t *offset) { char buf[32]; double voltage = voltage_get(); sprintf(buf, "%d", (int)(voltage * (double)1000)); REST.set_header_content_type(response, REST.type.TEXT_PLAIN); REST.set_response_payload(response, (uint8_t *)buf, strlen(buf)); }
void voltage_periodic_handler(resource_t *r) { static uint32_t obs_counter = 0; char content[32]; obs_counter++; double voltage = voltage_get(); /* 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), "%d", (int)(voltage * (double)1000))); /* Notify the registered observers with the given message type, observe option, and payload. */ REST.notify_subscribers(r, obs_counter, notification); }
float voltage_get_input(void){ return ((voltage_get() * VALUE_DIV_R1_OHM) / VALUE_DIV_R2_OHM); }