Other Parts Discussed in Thread: LAUNCHXL-CC2650
I would like to implement in the CoAP the ADC value input, can someone tell me how to move around?
This thread has been locked.
If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.
I would like to implement in the CoAP the ADC value input, can someone tell me how to move around?
Nothing yet, I can not see my values of the ADC input. I used this guide: sunmaysky.blogspot.it/.../contiki-cc2650-adc-demo-using-dio23-as.html
/*
Author: Telecontrolli srl
File: cc26xx-web-demo.c V1.0
Description: Main module for the CC26XX web demo. Activates on-device resources,
* takes sensor readings periodically and caches them for all other modules
* to use. Now with this we can cotroll 5 IOID,such as Input (Analogic/Digital)/Output.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "board-peripherals.h"
#include "lib/sensors.h"
#include "lib/list.h"
#include "sys/process.h"
#include "net/ipv6/sicslowpan.h"
#include "button-sensor.h"
#include "batmon-sensor.h"
#include "httpd-simple.h"
#include "cc26xx-web-demo.h"
#include "mqtt-client.h"
#include "coap-server.h"
#include "dev/leds.h"
#include "ti-lib.h"
#include "driverlib/aux_adc.h"
#include "driverlib/aux_wuc.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
PROCESS_NAME(cetic_6lbr_client_process);
PROCESS(cc26xx_web_demo_process, "CC26XX Web Demo");
PROCESS(adc_process, "ADC_process");
/*---------------------------------------------------------------------------*/
#define SENSOR_READING_PERIOD (CLOCK_SECOND * 20)
#define SENSOR_READING_RANDOM (CLOCK_SECOND << 4)
struct ctimer batmon_timer;
struct ctimer bmp_timer, hdc_timer, tmp_timer, opt_timer, mpu_timer;
/*---------------------------------------------------------------------------*/
/* Provide visible feedback via LEDS while searching for a network */
#define NO_NET_LED_DURATION (CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC >> 1)
static struct etimer et;
static struct ctimer ct;
/*---------------------------------------------------------------------------*/
/* Parent RSSI functionality */
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
static struct uip_icmp6_echo_reply_notification echo_reply_notification;
static struct etimer echo_request_timer;
int def_rt_rssi = 0;
#endif
uint16_t singleSample;
/*---------------------------------------------------------------------------*/
process_event_t cc26xx_web_demo_publish_event;
process_event_t cc26xx_web_demo_config_loaded_event;
process_event_t cc26xx_web_demo_load_config_defaults;
/*---------------------------------------------------------------------------*/
/* Saved settings on flash: store, offset, magic */
#define CONFIG_FLASH_OFFSET 0
#define CONFIG_MAGIC 0xCC265002
cc26xx_web_demo_config_t cc26xx_web_demo_config;
/*---------------------------------------------------------------------------*/
/* A cache of sensor values. Updated periodically or upon key press */
LIST(sensor_list);
/*---------------------------------------------------------------------------*/
/* The objects representing sensors used in this demo */
#define DEMO_SENSOR(name, type, descr, xml_element, form_field, units) \
cc26xx_web_demo_sensor_reading_t name##_reading = \
{ NULL, 0, 0, descr, xml_element, form_field, units, type, 1, 1 }
/* CC26xx sensors */
DEMO_SENSOR(batmon_temp, CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP,
"Battery Temp", "battery-temp", "batmon_temp",
CC26XX_WEB_DEMO_UNIT_TEMP);
DEMO_SENSOR(batmon_volt, CC26XX_WEB_DEMO_SENSOR_BATMON_VOLT,
"Battery Volt", "battery-volt", "batmon_volt",
CC26XX_WEB_DEMO_UNIT_VOLT);
DEMO_SENSOR(adc_ioid14, CC26XX_WEB_DEMO_SENSOR_ADC_IOID14,
"ADC IOID14", "adc-ioid14", "adc_ioid14",
CC26XX_WEB_DEMO_UNIT_VOLT);
/*---------------------------------------------------------------------------*/
static void
publish_led_off(void *d)
{
leds_off(CC26XX_WEB_DEMO_STATUS_LED);
}
/*---------------------------------------------------------------------------*/
static void
save_config()
{
/* Dump current running config to flash */
#if BOARD_SENSORTAG || BOARD_LAUNCHPAD
int rv;
cc26xx_web_demo_sensor_reading_t *reading = NULL;
rv = ext_flash_open();
if(!rv) {
printf("Could not open flash to save config\n");
ext_flash_close();
return;
}
rv = ext_flash_erase(CONFIG_FLASH_OFFSET, sizeof(cc26xx_web_demo_config_t));
if(!rv) {
printf("Error erasing flash\n");
} else {
cc26xx_web_demo_config.magic = CONFIG_MAGIC;
cc26xx_web_demo_config.len = sizeof(cc26xx_web_demo_config_t);
cc26xx_web_demo_config.sensors_bitmap = 0;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(reading->publish) {
cc26xx_web_demo_config.sensors_bitmap |= (1 << reading->type);
}
}
rv = ext_flash_write(CONFIG_FLASH_OFFSET, sizeof(cc26xx_web_demo_config_t),
(uint8_t *)&cc26xx_web_demo_config);
if(!rv) {
printf("Error saving config\n");
}
}
ext_flash_close();
#endif
}
/*---------------------------------------------------------------------------*/
static void
load_config()
{
#if BOARD_SENSORTAG || BOARD_LAUNCHPAD
/* Read from flash into a temp buffer */
cc26xx_web_demo_config_t tmp_cfg;
cc26xx_web_demo_sensor_reading_t *reading = NULL;
int rv = ext_flash_open();
if(!rv) {
printf("Could not open flash to load config\n");
ext_flash_close();
return;
}
rv = ext_flash_read(CONFIG_FLASH_OFFSET, sizeof(tmp_cfg),
(uint8_t *)&tmp_cfg);
ext_flash_close();
if(!rv) {
printf("Error loading config\n");
return;
}
if(tmp_cfg.magic == CONFIG_MAGIC && tmp_cfg.len == sizeof(tmp_cfg)) {
memcpy(&cc26xx_web_demo_config, &tmp_cfg, sizeof(cc26xx_web_demo_config));
}
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(cc26xx_web_demo_config.sensors_bitmap & (1 << reading->type)) {
reading->publish = 1;
} else {
reading->publish = 0;
snprintf(reading->converted, CC26XX_WEB_DEMO_CONVERTED_LEN, "\"N/A\"");
}
}
#endif
}
/*---------------------------------------------------------------------------*/
/* Don't start everything here, we need to dictate order of initialisation */
AUTOSTART_PROCESSES(&cc26xx_web_demo_process);
/*---------------------------------------------------------------------------*/
int
cc26xx_web_demo_ipaddr_sprintf(char *buf, uint8_t buf_len,
const uip_ipaddr_t *addr)
{
uint16_t a;
uint8_t len = 0;
int i, f;
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
len += snprintf(&buf[len], buf_len - len, "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
len += snprintf(&buf[len], buf_len - len, ":");
}
len += snprintf(&buf[len], buf_len - len, "%x", a);
}
}
return len;
}
/*---------------------------------------------------------------------------*/
const cc26xx_web_demo_sensor_reading_t *
cc26xx_web_demo_sensor_lookup(int sens_type)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(reading->type == sens_type) {
return reading;
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
const cc26xx_web_demo_sensor_reading_t *
cc26xx_web_demo_sensor_first()
{
return list_head(sensor_list);
}
/*---------------------------------------------------------------------------*/
void
cc26xx_web_demo_restore_defaults(void)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
leds_on(LEDS_ALL);
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
reading->publish = 1;
}
#if CC26XX_WEB_DEMO_MQTT_CLIENT
process_post_synch(&mqtt_client_process,
cc26xx_web_demo_load_config_defaults, NULL);
#endif
#if CC26XX_WEB_DEMO_NET_UART
process_post_synch(&net_uart_process, cc26xx_web_demo_load_config_defaults,
NULL);
#endif
save_config();
leds_off(LEDS_ALL);
}
/*---------------------------------------------------------------------------*/
static int
defaults_post_handler(char *key, int key_len, char *val, int val_len)
{
if(key_len != strlen("defaults") ||
strncasecmp(key, "defaults", strlen("defaults")) != 0) {
/* Not ours */
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
cc26xx_web_demo_restore_defaults();
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
/*---------------------------------------------------------------------------*/
static int
sensor_readings_handler(char *key, int key_len, char *val, int val_len)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
int rv;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(key_len == strlen(reading->form_field) &&
strncmp(reading->form_field, key, strlen(key)) == 0) {
rv = atoi(val);
/* Be pedantic: only accept 0 and 1, not just any non-zero value */
if(rv == 0) {
reading->publish = 0;
snprintf(reading->converted, CC26XX_WEB_DEMO_CONVERTED_LEN, "\"N/A\"");
} else if(rv == 1) {
reading->publish = 1;
} else {
return HTTPD_SIMPLE_POST_HANDLER_ERROR;
}
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
}
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
/*---------------------------------------------------------------------------*/
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
static int
ping_interval_post_handler(char *key, int key_len, char *val, int val_len)
{
int rv = 0;
if(key_len != strlen("ping_interval") ||
strncasecmp(key, "ping_interval", strlen("ping_interval")) != 0) {
/* Not ours */
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
rv = atoi(val);
if(rv < CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN ||
rv > CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX) {
return HTTPD_SIMPLE_POST_HANDLER_ERROR;
}
cc26xx_web_demo_config.def_rt_ping_interval = rv * CLOCK_SECOND;
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
#endif
/*---------------------------------------------------------------------------*/
HTTPD_SIMPLE_POST_HANDLER(sensor, sensor_readings_handler);
HTTPD_SIMPLE_POST_HANDLER(defaults, defaults_post_handler);
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
HTTPD_SIMPLE_POST_HANDLER(ping_interval, ping_interval_post_handler);
/*---------------------------------------------------------------------------*/
static void
echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data,
uint16_t datalen)
{
if(uip_ip6addr_cmp(source, uip_ds6_defrt_choose())) {
def_rt_rssi = sicslowpan_get_last_rssi();
}
}
/*---------------------------------------------------------------------------*/
static void
ping_parent(void)
{
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
return;
}
uip_icmp6_send(uip_ds6_defrt_choose(), ICMP6_ECHO_REQUEST, 0,
CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN);
}
#endif
/*---------------------------------------------------------------------------*/
static void
get_batmon_reading(void *data)
{
int value;
int adc_reading;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(batmon_temp_reading.publish) {
value = batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
batmon_temp_reading.raw = value;
buf = batmon_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d", value);
}
}
if(batmon_volt_reading.publish) {
value = batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT);
if(value != CC26XX_SENSOR_READING_ERROR) {
batmon_volt_reading.raw = value;
buf = batmon_volt_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d", (value * 125) >> 5);
}
}
if(adc_ioid14_reading.publish) {
if(1) {
buf = adc_ioid14_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d",singleSample);
}}
ctimer_set(&batmon_timer, next, get_batmon_reading, NULL);
}
/*---------------------------------------------------------------------------*/
#if BOARD_SENSORTAG
/*---------------------------------------------------------------------------*/
static void
compare_and_update(cc26xx_web_demo_sensor_reading_t *reading)
{
if(reading->last == reading->raw) {
reading->changed = 0;
} else {
reading->last = reading->raw;
reading->changed = 1;
}
}
/*---------------------------------------------------------------------------*/
static void
print_mpu_reading(int reading, char *buf)
{
char *loc_buf = buf;
if(reading < 0) {
sprintf(loc_buf, "-");
reading = -reading;
loc_buf++;
}
sprintf(loc_buf, "%d.%02d", reading / 100, reading % 100);
}
/*---------------------------------------------------------------------------*/
static void
get_bmp_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(bmp_pres_reading.publish) {
value = bmp_280_sensor.value(BMP_280_SENSOR_TYPE_PRESS);
if(value != CC26XX_SENSOR_READING_ERROR) {
bmp_pres_reading.raw = value;
compare_and_update(&bmp_pres_reading);
buf = bmp_pres_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
if(bmp_temp_reading.publish) {
value = bmp_280_sensor.value(BMP_280_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
bmp_temp_reading.raw = value;
compare_and_update(&bmp_temp_reading);
buf = bmp_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
SENSORS_DEACTIVATE(bmp_280_sensor);
ctimer_set(&bmp_timer, next, init_bmp_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_tmp_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(tmp_amb_reading.publish || tmp_obj_reading.publish) {
if(tmp_007_sensor.value(TMP_007_SENSOR_TYPE_ALL) ==
CC26XX_SENSOR_READING_ERROR) {
SENSORS_DEACTIVATE(tmp_007_sensor);
ctimer_set(&tmp_timer, next, init_tmp_reading, NULL);
}
}
if(tmp_amb_reading.publish) {
value = tmp_007_sensor.value(TMP_007_SENSOR_TYPE_AMBIENT);
tmp_amb_reading.raw = value;
compare_and_update(&tmp_amb_reading);
buf = tmp_amb_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%03d", value / 1000,
value % 1000);
}
if(tmp_obj_reading.publish) {
value = tmp_007_sensor.value(TMP_007_SENSOR_TYPE_OBJECT);
tmp_obj_reading.raw = value;
compare_and_update(&tmp_obj_reading);
buf = tmp_obj_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%03d", value / 1000,
value % 1000);
}
SENSORS_DEACTIVATE(tmp_007_sensor);
ctimer_set(&tmp_timer, next, init_tmp_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_hdc_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(hdc_temp_reading.publish) {
value = hdc_1000_sensor.value(HDC_1000_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
hdc_temp_reading.raw = value;
compare_and_update(&hdc_temp_reading);
buf = hdc_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
if(hdc_hum_reading.publish) {
value = hdc_1000_sensor.value(HDC_1000_SENSOR_TYPE_HUMIDITY);
if(value != CC26XX_SENSOR_READING_ERROR) {
hdc_hum_reading.raw = value;
compare_and_update(&hdc_hum_reading);
buf = hdc_hum_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
ctimer_set(&hdc_timer, next, init_hdc_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_light_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
value = opt_3001_sensor.value(0);
if(value != CC26XX_SENSOR_READING_ERROR) {
opt_reading.raw = value;
compare_and_update(&opt_reading);
buf = opt_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
/* The OPT will turn itself off, so we don't need to call its DEACTIVATE */
ctimer_set(&opt_timer, next, init_light_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_mpu_reading()
{
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
int raw;
if(mpu_gyro_x_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_X);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_x_reading.raw = raw;
}
}
if(mpu_gyro_y_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_Y);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_y_reading.raw = raw;
}
}
if(mpu_gyro_z_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_Z);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_z_reading.raw = raw;
}
}
if(mpu_acc_x_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_X);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_x_reading.raw = raw;
}
}
if(mpu_acc_y_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_Y);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_y_reading.raw = raw;
}
}
if(mpu_acc_z_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_Z);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_z_reading.raw = raw;
}
}
SENSORS_DEACTIVATE(mpu_9250_sensor);
if(mpu_gyro_x_reading.publish) {
compare_and_update(&mpu_gyro_x_reading);
memset(mpu_gyro_x_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_x_reading.raw, mpu_gyro_x_reading.converted);
}
if(mpu_gyro_y_reading.publish) {
compare_and_update(&mpu_gyro_y_reading);
memset(mpu_gyro_y_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_y_reading.raw, mpu_gyro_y_reading.converted);
}
if(mpu_gyro_z_reading.publish) {
compare_and_update(&mpu_gyro_z_reading);
memset(mpu_gyro_z_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_z_reading.raw, mpu_gyro_z_reading.converted);
}
if(mpu_acc_x_reading.publish) {
compare_and_update(&mpu_acc_x_reading);
memset(mpu_acc_x_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_x_reading.raw, mpu_acc_x_reading.converted);
}
if(mpu_acc_y_reading.publish) {
compare_and_update(&mpu_acc_y_reading);
memset(mpu_acc_y_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_y_reading.raw, mpu_acc_y_reading.converted);
}
if(mpu_acc_z_reading.publish) {
compare_and_update(&mpu_acc_z_reading);
memset(mpu_acc_z_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_z_reading.raw, mpu_acc_z_reading.converted);
}
/* We only use the single timer */
ctimer_set(&mpu_timer, next, init_mpu_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
init_tmp_reading(void *data)
{
if(tmp_amb_reading.publish || tmp_obj_reading.publish) {
SENSORS_ACTIVATE(tmp_007_sensor);
} else {
ctimer_set(&tmp_timer, CLOCK_SECOND, init_tmp_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_bmp_reading(void *data)
{
if(bmp_pres_reading.publish || bmp_temp_reading.publish) {
SENSORS_ACTIVATE(bmp_280_sensor);
} else {
ctimer_set(&bmp_timer, CLOCK_SECOND, init_bmp_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_hdc_reading(void *data)
{
if(hdc_hum_reading.publish || hdc_temp_reading.publish) {
SENSORS_ACTIVATE(hdc_1000_sensor);
} else {
ctimer_set(&hdc_timer, CLOCK_SECOND, init_hdc_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_light_reading(void *data)
{
if(opt_reading.publish) {
SENSORS_ACTIVATE(opt_3001_sensor);
} else {
ctimer_set(&opt_timer, CLOCK_SECOND, init_light_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_mpu_reading(void *data)
{
int readings_bitmap = 0;
if(mpu_acc_x_reading.publish || mpu_acc_y_reading.publish ||
mpu_acc_z_reading.publish) {
readings_bitmap |= MPU_9250_SENSOR_TYPE_ACC;
}
if(mpu_gyro_x_reading.publish || mpu_gyro_y_reading.publish ||
mpu_gyro_z_reading.publish) {
readings_bitmap |= MPU_9250_SENSOR_TYPE_GYRO;
}
if(readings_bitmap) {
mpu_9250_sensor.configure(SENSORS_ACTIVE, readings_bitmap);
} else {
ctimer_set(&mpu_timer, CLOCK_SECOND, init_mpu_reading, NULL);
}
}
#endif
/*---------------------------------------------------------------------------*/
static void
init_sensor_readings(void)
{
/*
* Make a first pass and get all initial sensor readings. This will also
* trigger periodic value updates
*/
get_batmon_reading(NULL);
#if BOARD_SENSORTAG
init_bmp_reading(NULL);
init_light_reading(NULL);
init_hdc_reading(NULL);
init_tmp_reading(NULL);
init_mpu_reading(NULL);
#endif /* BOARD_SENSORTAG */
return;
}
/*---------------------------------------------------------------------------*/
static void
init_sensors(void)
{
list_add(sensor_list, &batmon_temp_reading);
list_add(sensor_list, &batmon_volt_reading);
list_add(sensor_list, &adc_ioid14_reading);
SENSORS_ACTIVATE(batmon_sensor);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cc26xx_web_demo_process, ev, data)
{
PROCESS_BEGIN();
printf("CC26XX Web Demo Process\n");
init_sensors();
cc26xx_web_demo_publish_event = process_alloc_event();
cc26xx_web_demo_config_loaded_event = process_alloc_event();
cc26xx_web_demo_load_config_defaults = process_alloc_event();
/* Start all other (enabled) processes first */
process_start(&httpd_simple_process, NULL);
#if CC26XX_WEB_DEMO_COAP_SERVER
process_start(&coap_server_process, NULL);
#endif
#if CC26XX_WEB_DEMO_6LBR_CLIENT
process_start(&cetic_6lbr_client_process, NULL);
#endif
#if CC26XX_WEB_DEMO_MQTT_CLIENT
process_start(&mqtt_client_process, NULL);
#endif
#if CC26XX_WEB_DEMO_NET_UART
process_start(&net_uart_process, NULL);
#endif
#if CC26XX_WEB_DEMO_ADC_IOID14
process_start(&adc_process, NULL);
#endif
cc26xx_web_demo_config.sensors_bitmap = 0xFFFFFFFF; /* all on by default */
cc26xx_web_demo_config.def_rt_ping_interval =
CC26XX_WEB_DEMO_DEFAULT_RSSI_MEAS_INTERVAL;
load_config();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_config_loaded_event, NULL);
init_sensor_readings();
httpd_simple_register_post_handler(&sensor_handler);
httpd_simple_register_post_handler(&defaults_handler);
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
httpd_simple_register_post_handler(&ping_interval_handler);
def_rt_rssi = 0x8000000;
uip_icmp6_echo_reply_callback_add(&echo_reply_notification,
echo_reply_handler);
etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
#endif
etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
/*
* Update all sensor readings on a configurable sensors_event
* (e.g a button press / or reed trigger)
*/
while(1) {
if(ev == PROCESS_EVENT_TIMER && etimer_expired(&et)) {
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
leds_on(CC26XX_WEB_DEMO_STATUS_LED);
ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL);
etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
}
}
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
if(ev == PROCESS_EVENT_TIMER && etimer_expired(&echo_request_timer)) {
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
} else {
ping_parent();
etimer_set(&echo_request_timer, cc26xx_web_demo_config.def_rt_ping_interval);
}
}
#endif
if(ev == sensors_event && data == CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) {
if((CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER)->value(
BUTTON_SENSOR_VALUE_DURATION) > CLOCK_SECOND * 5) {
printf("Restoring defaults!\n");
cc26xx_web_demo_restore_defaults();
} else {
init_sensor_readings();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL);
}
} else if(ev == httpd_simple_event_new_config) {
save_config();
#if BOARD_SENSORTAG
} else if(ev == sensors_event && data == &bmp_280_sensor) {
get_bmp_reading();
} else if(ev == sensors_event && data == &opt_3001_sensor) {
get_light_reading();
} else if(ev == sensors_event && data == &hdc_1000_sensor) {
get_hdc_reading();
} else if(ev == sensors_event && data == &tmp_007_sensor) {
get_tmp_reading();
} else if(ev == sensors_event && data == &mpu_9250_sensor) {
get_mpu_reading();
#endif
}
PROCESS_YIELD();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(adc_process, ev, data)
{
PROCESS_BEGIN();
static struct etimer et_adc;
while(1)
{
etimer_set(&et_adc,CLOCK_SECOND*5);
PROCESS_WAIT_EVENT();
if(etimer_expired(&et_adc)) {
//intialisation of ADC
ti_lib_aon_wuc_aux_wakeup_event(AONWUC_AUX_WAKEUP);
while(!(ti_lib_aon_wuc_power_status_get() & AONWUC_AUX_POWER_ON))
{ }
// Enable clock for ADC digital and analog interface (not currently enabled in driver)
// Enable clocks
ti_lib_aux_wuc_clock_enable(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK);
while(ti_lib_aux_wuc_clock_status(AUX_WUC_ADI_CLOCK | AUX_WUC_ANAIF_CLOCK | AUX_WUC_SMPH_CLOCK) != AUX_WUC_CLOCK_READY)
{ }
printf("clock selected\r\n");
// Connect AUX IO0 (IOID14, but also DP2 on XDS110) as analog input.
AUXADCSelectInput(ADC_COMPB_IN_AUXIO0);
printf("input selected\r\n");
// Set up ADC range
// AUXADC_REF_FIXED = nominally 4.3 V
AUXADCEnableSync(AUXADC_REF_FIXED, AUXADC_SAMPLE_TIME_2P7_US, AUXADC_TRIGGER_MANUAL);
printf("init adc --- OK\r\n");
//Trigger ADC converting
AUXADCGenManualTrigger();
printf("trigger --- OK\r\n");
//reading adc value
singleSample = AUXADCReadFifo();
printf("%d mv on ADC\r\n",singleSample);
//shut the adc down
AUXADCDisable();
//printf("disable --- OK\r\n");
get_batmon_reading(NULL);
etimer_reset(&et_adc);
}
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
*/
/*
* Author: Telecontrolli srl
* File: coap-server.c V1.0
* Description: The file coap-server.c built the list of resources.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "board-peripherals.h"
#include "rf-core/rf-ble.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
/* Common resources */
extern resource_t res_leds;
extern resource_t res_batmon_temp;
extern resource_t res_batmon_volt;
extern resource_t res_adc_ioid14;
extern resource_t res_device_sw;
extern resource_t res_device_hw;
extern resource_t res_device_uptime;
extern resource_t res_device_cfg_reset;
extern resource_t res_parent_rssi;
extern resource_t res_parent_ip;
#if RF_BLE_ENABLED
extern resource_t res_ble_advd;
#endif
extern resource_t res_toggle_rosso;
extern resource_t res_toggle_verde;
extern resource_t res_toggle_giallo;
extern resource_t res_toggle_arancio;
/* Board-specific resources */
#if BOARD_SENSORTAG
extern resource_t res_bmp280_temp;
extern resource_t res_bmp280_press;
extern resource_t res_tmp007_amb;
extern resource_t res_tmp007_obj;
extern resource_t res_hdc1000_temp;
extern resource_t res_hdc1000_hum;
extern resource_t res_opt3001_light;
extern resource_t res_mpu_acc_x;
extern resource_t res_mpu_acc_y;
extern resource_t res_mpu_acc_z;
extern resource_t res_mpu_gyro_x;
extern resource_t res_mpu_gyro_y;
extern resource_t res_mpu_gyro_z;
#else
// extern resource_t res_toggle_orange;
// extern resource_t res_toggle_yellow;
#endif
/*---------------------------------------------------------------------------*/
const char *coap_server_not_found_msg = "Resource not found";
const char *coap_server_supported_msg = "Supported:"
"text/plain,"
"application/json,"
"application/xml";
/*---------------------------------------------------------------------------*/
static void
start_board_resources(void)
{
rest_activate_resource(&res_toggle_verde, "Led/Verde");
rest_activate_resource(&res_toggle_rosso, "Led/Rosso");
rest_activate_resource(&res_toggle_giallo, "Led/Giallo");
rest_activate_resource(&res_toggle_arancio, "Led/Arancio");
rest_activate_resource(&res_leds, "lt");
#if BOARD_SMARTRF06EB
// rest_activate_resource(&res_toggle_yellow, "lt/y");
// rest_activate_resource(&res_toggle_orange, "lt/o");
#endif
}
/*---------------------------------------------------------------------------*/
PROCESS(coap_server_process, "CC26XX CoAP Server");
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(coap_server_process, ev, data)
{
PROCESS_BEGIN();
printf("CC26XX CoAP Server\n");
/* Initialize the REST engine. */
rest_init_engine();
rest_activate_resource(&res_batmon_temp, "Sensori/batmon/Livello Temperatura");
rest_activate_resource(&res_batmon_volt, "Sensori/batmon/Livello Batteria");
rest_activate_resource(&res_adc_ioid14, "adc/ioid14");
rest_activate_resource(&res_device_hw, "Dispositivo/Middleware/Hardware");
rest_activate_resource(&res_device_sw, "Dispositivo/Middleware/Software");
rest_activate_resource(&res_device_uptime, "Dispositivo/Uptime");
rest_activate_resource(&res_device_cfg_reset, "Dispositivo/Reset");
rest_activate_resource(&res_parent_rssi, "Rete/Nodo/RSSI");
rest_activate_resource(&res_parent_ip, "Rete/Nodo/IPv6");
#if RF_BLE_ENABLED
rest_activate_resource(&res_ble_advd, "Dispositivo/ble_advd");
#endif
start_board_resources();
/* Define application-specific events here. */
while(1) {
PROCESS_WAIT_EVENT();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
* @}
*/
/*
* Author: Telecontrolli srl
* File: res-sensors.c V1.0
* Description: CoAP resource handler for the Sensortag-CC26xx sensors
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "rest-engine.h"
#include "er-coap.h"
#include "cc26xx-web-demo.h"
#include "coap-server.h"
#include <stdio.h>
#include <stdint.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
/*
* Generic resource handler for any sensor in this example. Ultimately gets
* called by all handlers and populates the CoAP response
*/
static void
res_get_handler_all(int sens_type, void *request, void *response,
uint8_t *buffer, uint16_t preferred_size, int32_t *offset)
{
unsigned int accept = -1;
const cc26xx_web_demo_sensor_reading_t *reading;
reading = cc26xx_web_demo_sensor_lookup(sens_type);
if(reading == NULL) {
REST.set_response_status(response, REST.status.NOT_FOUND);
REST.set_response_payload(response, coap_server_not_found_msg,
strlen(coap_server_not_found_msg));
return;
}
REST.get_header_accept(request, &accept);
if(accept == -1 || accept == REST.type.TEXT_PLAIN) {
REST.set_header_content_type(response, REST.type.TEXT_PLAIN);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "%s", reading->converted);
REST.set_response_payload(response, (uint8_t *)buffer,
strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_JSON) {
REST.set_header_content_type(response, REST.type.APPLICATION_JSON);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE, "{\"%s\":%s}",
reading->descr, reading->converted);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else if(accept == REST.type.APPLICATION_XML) {
REST.set_header_content_type(response, REST.type.APPLICATION_XML);
snprintf((char *)buffer, REST_MAX_CHUNK_SIZE,
"<%s val=\"%s\" unit=\"%s\"/>", reading->xml_element,
reading->converted, reading->units);
REST.set_response_payload(response, buffer, strlen((char *)buffer));
} else {
REST.set_response_status(response, REST.status.NOT_ACCEPTABLE);
REST.set_response_payload(response, coap_server_supported_msg,
strlen(coap_server_supported_msg));
}
}
/*---------------------------------------------------------------------------*/
/* BatMon resources and handler: Temperature, Voltage */
static void
res_get_handler_batmon_temp(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_batmon_volt(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BATMON_VOLT, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_adc_ioid14(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_ADC_IOID14, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
RESOURCE(res_batmon_temp, "Titolo=\"Livello Temperatura\";rt=\"C\"",
res_get_handler_batmon_temp, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
RESOURCE(res_batmon_volt, "Titolo=\"Tensione di batteria\";rt=\"mV\"",
res_get_handler_batmon_volt, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
RESOURCE(res_adc_ioid14, "Titolo=\"ADC IOID14\";rt\"mv\"",
res_get_handler_adc_ioid14, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
#if BOARD_SENSORTAG
/*---------------------------------------------------------------------------*/
/* MPU resources and handler: Accelerometer and Gyro */
static void
res_get_handler_mpu_acc_x(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_ACC_X, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_mpu_acc_y(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_ACC_Y, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_mpu_acc_z(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_ACC_Z, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_mpu_gyro_x(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_X, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_mpu_gyro_y(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_Y, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_mpu_gyro_z(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_Z, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
RESOURCE(res_mpu_acc_x, "title=\"Acc X\";rt=\"G\"", res_get_handler_mpu_acc_x,
NULL, NULL, NULL);
RESOURCE(res_mpu_acc_y, "title=\"Acc Y\";rt=\"G\"", res_get_handler_mpu_acc_y,
NULL, NULL, NULL);
RESOURCE(res_mpu_acc_z, "title=\"Acc Z\";rt=\"G\"", res_get_handler_mpu_acc_z,
NULL, NULL, NULL);
RESOURCE(res_mpu_gyro_x, "title=\"Gyro X\";rt=\"deg/sec\"",
res_get_handler_mpu_gyro_x, NULL, NULL, NULL);
RESOURCE(res_mpu_gyro_y, "title=\"Gyro Y\";rt=\"deg/sec\"",
res_get_handler_mpu_gyro_y, NULL, NULL, NULL);
RESOURCE(res_mpu_gyro_z, "title=\"Gyro Z\";rt=\"deg/sec\"",
res_get_handler_mpu_gyro_z, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
/* TMP sensor resources and handlers: Object, Ambient */
static void
res_get_handler_obj_temp(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_TMP_OBJECT, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_amb_temp(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_TMP_AMBIENT, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
RESOURCE(res_tmp007_obj, "title=\"Temperature (Object)\";rt=\"C\"",
res_get_handler_obj_temp, NULL, NULL, NULL);
RESOURCE(res_tmp007_amb, "title=\"Temperature (Ambient)\";rt=\"C\"",
res_get_handler_amb_temp, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
/* BMP sensor resources: Temperature, Pressure */
static void
res_get_handler_bmp_temp(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BMP_TEMP, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_bmp_press(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_BMP_PRES, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
RESOURCE(res_bmp280_temp, "title=\"Barometer (Temperature)\";rt=\"C\"",
res_get_handler_bmp_temp, NULL, NULL, NULL);
RESOURCE(res_bmp280_press,
"title=\"Barometer (Pressure)\";rt=\"hPa (hectopascal / millibar)\"",
res_get_handler_bmp_press, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
/* HDC1000 sensor resources and handler: Temperature, Pressure */
static void
res_get_handler_hdc_temp(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_HDC_TEMP, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
static void
res_get_handler_hdc_humidity(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_HDC_HUMIDITY, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
RESOURCE(res_hdc1000_temp, "title=\"Temperature\";rt=\"C\"",
res_get_handler_hdc_temp, NULL, NULL, NULL);
RESOURCE(res_hdc1000_hum, "title=\"Humidity\";rt=\"%RH\"",
res_get_handler_hdc_humidity, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
/* Illuminance resources and handler */
static void
res_get_handler_opt(void *request, void *response, uint8_t *buffer,
uint16_t preferred_size, int32_t *offset)
{
res_get_handler_all(CC26XX_WEB_DEMO_SENSOR_OPT_LIGHT, request, response,
buffer, preferred_size, offset);
}
/*---------------------------------------------------------------------------*/
RESOURCE(res_opt3001_light, "title=\"Illuminance\";rt=\"Lux\"",
res_get_handler_opt, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
#endif /* BOARD_SENSORTAG */
/*---------------------------------------------------------------------------*/
/** @} */
Hello friend, today I am by implementing the value digital input in CoAP. I followed the guide that thou hast done and I just have to grow the variable that tells me if the LED is on/off. Can you help me?
/*
Author: Telecontrolli srl
File: cc26xx-web-demo.c V1.0
Description: Main module for the CC26XX web demo. Activates on-device resources,
* takes sensor readings periodically and caches them for all other modules
* to use. Now with this we can cotroll 5 IOID,such as Input (Analogic/Digital)/Output.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "board-peripherals.h"
#include "lib/sensors.h"
#include "lib/list.h"
#include "sys/process.h"
#include "net/ipv6/sicslowpan.h"
#include "button-sensor.h"
#include "batmon-sensor.h"
#include "httpd-simple.h"
#include "cc26xx-web-demo.h"
#include "mqtt-client.h"
#include "coap-server.h"
#include "dev/leds.h"
#include "dev/button-sensor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
PROCESS_NAME(cetic_6lbr_client_process);
PROCESS(cc26xx_web_demo_process, "CC26XX Web Demo");
PROCESS(blink_process, "Blink_process");
/*---------------------------------------------------------------------------*/
#define SENSOR_READING_PERIOD (CLOCK_SECOND * 20)
#define SENSOR_READING_RANDOM (CLOCK_SECOND << 4)
struct ctimer batmon_timer;
struct ctimer bmp_timer, hdc_timer, tmp_timer, opt_timer, mpu_timer;
/*---------------------------------------------------------------------------*/
/* Provide visible feedback via LEDS while searching for a network */
#define NO_NET_LED_DURATION (CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC >> 1)
static struct etimer et;
static struct ctimer ct;
/*---------------------------------------------------------------------------*/
/* Parent RSSI functionality */
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
static struct uip_icmp6_echo_reply_notification echo_reply_notification;
static struct etimer echo_request_timer;
int def_rt_rssi = 0;
#endif
//static uint16_t push = 0;
/*---------------------------------------------------------------------------*/
process_event_t cc26xx_web_demo_publish_event;
process_event_t cc26xx_web_demo_config_loaded_event;
process_event_t cc26xx_web_demo_load_config_defaults;
/*---------------------------------------------------------------------------*/
/* Saved settings on flash: store, offset, magic */
#define CONFIG_FLASH_OFFSET 0
#define CONFIG_MAGIC 0xCC265002
cc26xx_web_demo_config_t cc26xx_web_demo_config;
/*---------------------------------------------------------------------------*/
/* A cache of sensor values. Updated periodically or upon key press */
LIST(sensor_list);
/*---------------------------------------------------------------------------*/
/* The objects representing sensors used in this demo */
#define DEMO_SENSOR(name, type, descr, xml_element, form_field, units) \
cc26xx_web_demo_sensor_reading_t name##_reading = \
{ NULL, 0, 0, descr, xml_element, form_field, units, type, 1, 1 }
/* CC26xx sensors */
DEMO_SENSOR(batmon_temp, CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP,
"Battery Temp", "battery-temp", "batmon_temp",
CC26XX_WEB_DEMO_UNIT_TEMP);
DEMO_SENSOR(batmon_volt, CC26XX_WEB_DEMO_SENSOR_BATMON_VOLT,
"Battery Volt", "battery-volt", "batmon_volt",
CC26XX_WEB_DEMO_UNIT_VOLT);
DEMO_SENSOR(digital_ioid0, CC26XX_WEB_DEMO_SENSOR_DIGITAL_IOID0,
"Digital IOID0", "digital-ioid0", "digital_ioid0",
CC26XX_WEB_DEMO_UNIT_DIGITAL);
/*---------------------------------------------------------------------------*/
static void
publish_led_off(void *d)
{
leds_off(CC26XX_WEB_DEMO_STATUS_LED);
}
/*---------------------------------------------------------------------------*/
static void
save_config()
{
/* Dump current running config to flash */
#if BOARD_SENSORTAG || BOARD_LAUNCHPAD
int rv;
cc26xx_web_demo_sensor_reading_t *reading = NULL;
rv = ext_flash_open();
if(!rv) {
printf("Could not open flash to save config\n");
ext_flash_close();
return;
}
rv = ext_flash_erase(CONFIG_FLASH_OFFSET, sizeof(cc26xx_web_demo_config_t));
if(!rv) {
printf("Error erasing flash\n");
} else {
cc26xx_web_demo_config.magic = CONFIG_MAGIC;
cc26xx_web_demo_config.len = sizeof(cc26xx_web_demo_config_t);
cc26xx_web_demo_config.sensors_bitmap = 0;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(reading->publish) {
cc26xx_web_demo_config.sensors_bitmap |= (1 << reading->type);
}
}
rv = ext_flash_write(CONFIG_FLASH_OFFSET, sizeof(cc26xx_web_demo_config_t),
(uint8_t *)&cc26xx_web_demo_config);
if(!rv) {
printf("Error saving config\n");
}
}
ext_flash_close();
#endif
}
/*---------------------------------------------------------------------------*/
static void
load_config()
{
#if BOARD_SENSORTAG || BOARD_LAUNCHPAD
/* Read from flash into a temp buffer */
cc26xx_web_demo_config_t tmp_cfg;
cc26xx_web_demo_sensor_reading_t *reading = NULL;
int rv = ext_flash_open();
if(!rv) {
printf("Could not open flash to load config\n");
ext_flash_close();
return;
}
rv = ext_flash_read(CONFIG_FLASH_OFFSET, sizeof(tmp_cfg),
(uint8_t *)&tmp_cfg);
ext_flash_close();
if(!rv) {
printf("Error loading config\n");
return;
}
if(tmp_cfg.magic == CONFIG_MAGIC && tmp_cfg.len == sizeof(tmp_cfg)) {
memcpy(&cc26xx_web_demo_config, &tmp_cfg, sizeof(cc26xx_web_demo_config));
}
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(cc26xx_web_demo_config.sensors_bitmap & (1 << reading->type)) {
reading->publish = 1;
} else {
reading->publish = 0;
snprintf(reading->converted, CC26XX_WEB_DEMO_CONVERTED_LEN, "\"N/A\"");
}
}
#endif
}
/*---------------------------------------------------------------------------*/
/* Don't start everything here, we need to dictate order of initialisation */
AUTOSTART_PROCESSES(&cc26xx_web_demo_process);
/*---------------------------------------------------------------------------*/
int
cc26xx_web_demo_ipaddr_sprintf(char *buf, uint8_t buf_len,
const uip_ipaddr_t *addr)
{
uint16_t a;
uint8_t len = 0;
int i, f;
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
len += snprintf(&buf[len], buf_len - len, "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
len += snprintf(&buf[len], buf_len - len, ":");
}
len += snprintf(&buf[len], buf_len - len, "%x", a);
}
}
return len;
}
/*---------------------------------------------------------------------------*/
const cc26xx_web_demo_sensor_reading_t *
cc26xx_web_demo_sensor_lookup(int sens_type)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(reading->type == sens_type) {
return reading;
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
const cc26xx_web_demo_sensor_reading_t *
cc26xx_web_demo_sensor_first()
{
return list_head(sensor_list);
}
/*---------------------------------------------------------------------------*/
void
cc26xx_web_demo_restore_defaults(void)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
leds_on(LEDS_ALL);
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
reading->publish = 1;
}
#if CC26XX_WEB_DEMO_MQTT_CLIENT
process_post_synch(&mqtt_client_process,
cc26xx_web_demo_load_config_defaults, NULL);
#endif
#if CC26XX_WEB_DEMO_NET_UART
process_post_synch(&net_uart_process, cc26xx_web_demo_load_config_defaults,
NULL);
#endif
save_config();
leds_off(LEDS_ALL);
}
/*---------------------------------------------------------------------------*/
static int
defaults_post_handler(char *key, int key_len, char *val, int val_len)
{
if(key_len != strlen("defaults") ||
strncasecmp(key, "defaults", strlen("defaults")) != 0) {
/* Not ours */
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
cc26xx_web_demo_restore_defaults();
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
/*---------------------------------------------------------------------------*/
static int
sensor_readings_handler(char *key, int key_len, char *val, int val_len)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
int rv;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(key_len == strlen(reading->form_field) &&
strncmp(reading->form_field, key, strlen(key)) == 0) {
rv = atoi(val);
/* Be pedantic: only accept 0 and 1, not just any non-zero value */
if(rv == 0) {
reading->publish = 0;
snprintf(reading->converted, CC26XX_WEB_DEMO_CONVERTED_LEN, "\"N/A\"");
} else if(rv == 1) {
reading->publish = 1;
} else {
return HTTPD_SIMPLE_POST_HANDLER_ERROR;
}
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
}
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
/*---------------------------------------------------------------------------*/
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
static int
ping_interval_post_handler(char *key, int key_len, char *val, int val_len)
{
int rv = 0;
if(key_len != strlen("ping_interval") ||
strncasecmp(key, "ping_interval", strlen("ping_interval")) != 0) {
/* Not ours */
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
rv = atoi(val);
if(rv < CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN ||
rv > CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX) {
return HTTPD_SIMPLE_POST_HANDLER_ERROR;
}
cc26xx_web_demo_config.def_rt_ping_interval = rv * CLOCK_SECOND;
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
#endif
/*---------------------------------------------------------------------------*/
HTTPD_SIMPLE_POST_HANDLER(sensor, sensor_readings_handler);
HTTPD_SIMPLE_POST_HANDLER(defaults, defaults_post_handler);
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
HTTPD_SIMPLE_POST_HANDLER(ping_interval, ping_interval_post_handler);
/*---------------------------------------------------------------------------*/
static void
echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data,
uint16_t datalen)
{
if(uip_ip6addr_cmp(source, uip_ds6_defrt_choose())) {
def_rt_rssi = sicslowpan_get_last_rssi();
}
}
/*---------------------------------------------------------------------------*/
static void
ping_parent(void)
{
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
return;
}
uip_icmp6_send(uip_ds6_defrt_choose(), ICMP6_ECHO_REQUEST, 0,
CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN);
}
#endif
/*---------------------------------------------------------------------------*/
static void
get_batmon_reading(void *data)
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(batmon_temp_reading.publish) {
value = batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
batmon_temp_reading.raw = value;
buf = batmon_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d", value);
}
}
if(batmon_volt_reading.publish) {
value = batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT);
if(value != CC26XX_SENSOR_READING_ERROR) {
batmon_volt_reading.raw = value;
buf = batmon_volt_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d", (value * 125) >> 5);
}
}
if(digital_ioid0_reading.publish) {
if(1) {
buf = digital_ioid0_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d",push);
}}
ctimer_set(&batmon_timer, next, get_batmon_reading, NULL);
ctimer_set(&batmon_timer, next, get_batmon_reading, NULL);
}
/*---------------------------------------------------------------------------*/
#if BOARD_SENSORTAG
/*---------------------------------------------------------------------------*/
static void
compare_and_update(cc26xx_web_demo_sensor_reading_t *reading)
{
if(reading->last == reading->raw) {
reading->changed = 0;
} else {
reading->last = reading->raw;
reading->changed = 1;
}
}
/*---------------------------------------------------------------------------*/
static void
print_mpu_reading(int reading, char *buf)
{
char *loc_buf = buf;
if(reading < 0) {
sprintf(loc_buf, "-");
reading = -reading;
loc_buf++;
}
sprintf(loc_buf, "%d.%02d", reading / 100, reading % 100);
}
/*---------------------------------------------------------------------------*/
static void
get_bmp_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(bmp_pres_reading.publish) {
value = bmp_280_sensor.value(BMP_280_SENSOR_TYPE_PRESS);
if(value != CC26XX_SENSOR_READING_ERROR) {
bmp_pres_reading.raw = value;
compare_and_update(&bmp_pres_reading);
buf = bmp_pres_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
if(bmp_temp_reading.publish) {
value = bmp_280_sensor.value(BMP_280_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
bmp_temp_reading.raw = value;
compare_and_update(&bmp_temp_reading);
buf = bmp_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
SENSORS_DEACTIVATE(bmp_280_sensor);
ctimer_set(&bmp_timer, next, init_bmp_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_tmp_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(tmp_amb_reading.publish || tmp_obj_reading.publish) {
if(tmp_007_sensor.value(TMP_007_SENSOR_TYPE_ALL) ==
CC26XX_SENSOR_READING_ERROR) {
SENSORS_DEACTIVATE(tmp_007_sensor);
ctimer_set(&tmp_timer, next, init_tmp_reading, NULL);
}
}
if(tmp_amb_reading.publish) {
value = tmp_007_sensor.value(TMP_007_SENSOR_TYPE_AMBIENT);
tmp_amb_reading.raw = value;
compare_and_update(&tmp_amb_reading);
buf = tmp_amb_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%03d", value / 1000,
value % 1000);
}
if(tmp_obj_reading.publish) {
value = tmp_007_sensor.value(TMP_007_SENSOR_TYPE_OBJECT);
tmp_obj_reading.raw = value;
compare_and_update(&tmp_obj_reading);
buf = tmp_obj_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%03d", value / 1000,
value % 1000);
}
SENSORS_DEACTIVATE(tmp_007_sensor);
ctimer_set(&tmp_timer, next, init_tmp_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_hdc_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(hdc_temp_reading.publish) {
value = hdc_1000_sensor.value(HDC_1000_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
hdc_temp_reading.raw = value;
compare_and_update(&hdc_temp_reading);
buf = hdc_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
if(hdc_hum_reading.publish) {
value = hdc_1000_sensor.value(HDC_1000_SENSOR_TYPE_HUMIDITY);
if(value != CC26XX_SENSOR_READING_ERROR) {
hdc_hum_reading.raw = value;
compare_and_update(&hdc_hum_reading);
buf = hdc_hum_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
ctimer_set(&hdc_timer, next, init_hdc_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_light_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
value = opt_3001_sensor.value(0);
if(value != CC26XX_SENSOR_READING_ERROR) {
opt_reading.raw = value;
compare_and_update(&opt_reading);
buf = opt_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
/* The OPT will turn itself off, so we don't need to call its DEACTIVATE */
ctimer_set(&opt_timer, next, init_light_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_mpu_reading()
{
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
int raw;
if(mpu_gyro_x_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_X);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_x_reading.raw = raw;
}
}
if(mpu_gyro_y_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_Y);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_y_reading.raw = raw;
}
}
if(mpu_gyro_z_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_Z);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_z_reading.raw = raw;
}
}
if(mpu_acc_x_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_X);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_x_reading.raw = raw;
}
}
if(mpu_acc_y_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_Y);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_y_reading.raw = raw;
}
}
if(mpu_acc_z_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_Z);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_z_reading.raw = raw;
}
}
SENSORS_DEACTIVATE(mpu_9250_sensor);
if(mpu_gyro_x_reading.publish) {
compare_and_update(&mpu_gyro_x_reading);
memset(mpu_gyro_x_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_x_reading.raw, mpu_gyro_x_reading.converted);
}
if(mpu_gyro_y_reading.publish) {
compare_and_update(&mpu_gyro_y_reading);
memset(mpu_gyro_y_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_y_reading.raw, mpu_gyro_y_reading.converted);
}
if(mpu_gyro_z_reading.publish) {
compare_and_update(&mpu_gyro_z_reading);
memset(mpu_gyro_z_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_z_reading.raw, mpu_gyro_z_reading.converted);
}
if(mpu_acc_x_reading.publish) {
compare_and_update(&mpu_acc_x_reading);
memset(mpu_acc_x_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_x_reading.raw, mpu_acc_x_reading.converted);
}
if(mpu_acc_y_reading.publish) {
compare_and_update(&mpu_acc_y_reading);
memset(mpu_acc_y_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_y_reading.raw, mpu_acc_y_reading.converted);
}
if(mpu_acc_z_reading.publish) {
compare_and_update(&mpu_acc_z_reading);
memset(mpu_acc_z_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_z_reading.raw, mpu_acc_z_reading.converted);
}
/* We only use the single timer */
ctimer_set(&mpu_timer, next, init_mpu_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
init_tmp_reading(void *data)
{
if(tmp_amb_reading.publish || tmp_obj_reading.publish) {
SENSORS_ACTIVATE(tmp_007_sensor);
} else {
ctimer_set(&tmp_timer, CLOCK_SECOND, init_tmp_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_bmp_reading(void *data)
{
if(bmp_pres_reading.publish || bmp_temp_reading.publish) {
SENSORS_ACTIVATE(bmp_280_sensor);
} else {
ctimer_set(&bmp_timer, CLOCK_SECOND, init_bmp_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_hdc_reading(void *data)
{
if(hdc_hum_reading.publish || hdc_temp_reading.publish) {
SENSORS_ACTIVATE(hdc_1000_sensor);
} else {
ctimer_set(&hdc_timer, CLOCK_SECOND, init_hdc_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_light_reading(void *data)
{
if(opt_reading.publish) {
SENSORS_ACTIVATE(opt_3001_sensor);
} else {
ctimer_set(&opt_timer, CLOCK_SECOND, init_light_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_mpu_reading(void *data)
{
int readings_bitmap = 0;
if(mpu_acc_x_reading.publish || mpu_acc_y_reading.publish ||
mpu_acc_z_reading.publish) {
readings_bitmap |= MPU_9250_SENSOR_TYPE_ACC;
}
if(mpu_gyro_x_reading.publish || mpu_gyro_y_reading.publish ||
mpu_gyro_z_reading.publish) {
readings_bitmap |= MPU_9250_SENSOR_TYPE_GYRO;
}
if(readings_bitmap) {
mpu_9250_sensor.configure(SENSORS_ACTIVE, readings_bitmap);
} else {
ctimer_set(&mpu_timer, CLOCK_SECOND, init_mpu_reading, NULL);
}
}
#endif
/*---------------------------------------------------------------------------*/
static void
init_sensor_readings(void)
{
/*
* Make a first pass and get all initial sensor readings. This will also
* trigger periodic value updates
*/
get_batmon_reading(NULL);
#if BOARD_SENSORTAG
init_bmp_reading(NULL);
init_light_reading(NULL);
init_hdc_reading(NULL);
init_tmp_reading(NULL);
init_mpu_reading(NULL);
#endif /* BOARD_SENSORTAG */
return;
}
/*---------------------------------------------------------------------------*/
static void
init_sensors(void)
{
list_add(sensor_list, &batmon_temp_reading);
list_add(sensor_list, &batmon_volt_reading);
list_add(sensor_list, &digital_ioid0_reading);
SENSORS_ACTIVATE(batmon_sensor);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cc26xx_web_demo_process, ev, data)
{
PROCESS_BEGIN();
printf("CC26XX Web Demo Process\n");
init_sensors();
cc26xx_web_demo_publish_event = process_alloc_event();
cc26xx_web_demo_config_loaded_event = process_alloc_event();
cc26xx_web_demo_load_config_defaults = process_alloc_event();
/* Start all other (enabled) processes first */
process_start(&httpd_simple_process, NULL);
#if CC26XX_WEB_DEMO_COAP_SERVER
process_start(&coap_server_process, NULL);
#endif
#if CC26XX_WEB_DEMO_6LBR_CLIENT
process_start(&cetic_6lbr_client_process, NULL);
#endif
#if CC26XX_WEB_DEMO_MQTT_CLIENT
process_start(&mqtt_client_process, NULL);
#endif
#if CC26XX_WEB_DEMO_NET_UART
process_start(&net_uart_process, NULL);
#endif
#if CC26XX_WEB_DEMO_DIGITAL_IOID0
process_start(&blink_process,NULL);
#endif
cc26xx_web_demo_config.sensors_bitmap = 0xFFFFFFFF; /* all on by default */
cc26xx_web_demo_config.def_rt_ping_interval =
CC26XX_WEB_DEMO_DEFAULT_RSSI_MEAS_INTERVAL;
load_config();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_config_loaded_event, NULL);
init_sensor_readings();
httpd_simple_register_post_handler(&sensor_handler);
httpd_simple_register_post_handler(&defaults_handler);
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
httpd_simple_register_post_handler(&ping_interval_handler);
def_rt_rssi = 0x8000000;
uip_icmp6_echo_reply_callback_add(&echo_reply_notification,
echo_reply_handler);
etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
#endif
etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
/*
* Update all sensor readings on a configurable sensors_event
* (e.g a button press / or reed trigger)
*/
while(1) {
if(ev == PROCESS_EVENT_TIMER && etimer_expired(&et)) {
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
leds_on(CC26XX_WEB_DEMO_STATUS_LED);
ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL);
etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
}
}
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
if(ev == PROCESS_EVENT_TIMER && etimer_expired(&echo_request_timer)) {
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
} else {
ping_parent();
etimer_set(&echo_request_timer, cc26xx_web_demo_config.def_rt_ping_interval);
}
}
#endif
if(ev == sensors_event && data == CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) {
if((CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER)->value(
BUTTON_SENSOR_VALUE_DURATION) > CLOCK_SECOND * 5) {
printf("Restoring defaults!\n");
cc26xx_web_demo_restore_defaults();
} else {
init_sensor_readings();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL);
}
} else if(ev == httpd_simple_event_new_config) {
save_config();
#if BOARD_SENSORTAG
} else if(ev == sensors_event && data == &bmp_280_sensor) {
get_bmp_reading();
} else if(ev == sensors_event && data == &opt_3001_sensor) {
get_light_reading();
} else if(ev == sensors_event && data == &hdc_1000_sensor) {
get_hdc_reading();
} else if(ev == sensors_event && data == &tmp_007_sensor) {
get_tmp_reading();
} else if(ev == sensors_event && data == &mpu_9250_sensor) {
get_mpu_reading();
#endif
}
PROCESS_YIELD();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------------*/
PROCESS_THREAD (blink_process, ev, data) {
PROCESS_EXITHANDLER(goto exit);
PROCESS_BEGIN ();
SENSORS_ACTIVATE(button_sensor);
printf ("Clicca il bottone \n");
leds_on(LEDS_ALL);
while(1) {
static uint8_t push = 0;
PROCESS_WAIT_EVENT_UNTIL((ev==sensors_event) && (data ==&button_left_sensor));
if (push % 2 == 0) {
leds_toggle(LEDS_ALL);
push++;
} else {
leds_toggle(LEDS_ALL);
push++;
}
if (push == 255) {
push = 0;
} }
exit:
leds_off(LEDS_ALL);
PROCESS_END();
}
/**
* @}
*/
This is a file with the implementation of the digital input on the coap, but in coap does not leave the variable that tells me that the LED is turned on or off. Can you help me?
/*
Author: Telecontrolli srl
File: cc26xx-web-demo.c V1.0
Description: Main module for the CC26XX web demo. Activates on-device resources,
* takes sensor readings periodically and caches them for all other modules
* to use. Now with this we can cotroll 5 IOID,such as Input (Analogic/Digital)/Output.
*/
/*---------------------------------------------------------------------------*/
#include "contiki.h"
#include "contiki-net.h"
#include "rest-engine.h"
#include "board-peripherals.h"
#include "lib/sensors.h"
#include "lib/list.h"
#include "sys/process.h"
#include "net/ipv6/sicslowpan.h"
#include "button-sensor.h"
#include "batmon-sensor.h"
#include "httpd-simple.h"
#include "cc26xx-web-demo.h"
#include "mqtt-client.h"
#include "coap-server.h"
#include "dev/leds.h"
#include "dev/button-sensor.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
/*---------------------------------------------------------------------------*/
PROCESS_NAME(cetic_6lbr_client_process);
PROCESS(cc26xx_web_demo_process, "CC26XX Web Demo");
PROCESS(button_process, "Button_process");
/*---------------------------------------------------------------------------*/
#define SENSOR_READING_PERIOD (CLOCK_SECOND * 20)
#define SENSOR_READING_RANDOM (CLOCK_SECOND << 4)
struct ctimer batmon_timer;
struct ctimer bmp_timer, hdc_timer, tmp_timer, opt_timer, mpu_timer;
/*---------------------------------------------------------------------------*/
/* Provide visible feedback via LEDS while searching for a network */
#define NO_NET_LED_DURATION (CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC >> 1)
static struct etimer et;
static struct ctimer ct;
/*---------------------------------------------------------------------------*/
/* Parent RSSI functionality */
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
static struct uip_icmp6_echo_reply_notification echo_reply_notification;
static struct etimer echo_request_timer;
int def_rt_rssi = 0;
#endif
uint16_t mode = 0;
/*---------------------------------------------------------------------------*/
process_event_t cc26xx_web_demo_publish_event;
process_event_t cc26xx_web_demo_config_loaded_event;
process_event_t cc26xx_web_demo_load_config_defaults;
/*---------------------------------------------------------------------------*/
/* Saved settings on flash: store, offset, magic */
#define CONFIG_FLASH_OFFSET 0
#define CONFIG_MAGIC 0xCC265002
cc26xx_web_demo_config_t cc26xx_web_demo_config;
/*---------------------------------------------------------------------------*/
/* A cache of sensor values. Updated periodically or upon key press */
LIST(sensor_list);
/*---------------------------------------------------------------------------*/
/* The objects representing sensors used in this demo */
#define DEMO_SENSOR(name, type, descr, xml_element, form_field, units) \
cc26xx_web_demo_sensor_reading_t name##_reading = \
{ NULL, 0, 0, descr, xml_element, form_field, units, type, 1, 1 }
/* CC26xx sensors */
DEMO_SENSOR(batmon_temp, CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP,
"Battery Temp", "battery-temp", "batmon_temp",
CC26XX_WEB_DEMO_UNIT_TEMP);
DEMO_SENSOR(batmon_volt, CC26XX_WEB_DEMO_SENSOR_BATMON_VOLT,
"Battery Volt", "battery-volt", "batmon_volt",
CC26XX_WEB_DEMO_UNIT_VOLT);
DEMO_SENSOR(digital_ioid0, CC26XX_WEB_DEMO_SENSOR_DIGITAL_IOID0,
"Digital IOID0", "digital-ioid0", "digital_ioid0",
CC26XX_WEB_DEMO_UNIT_DIGITAL);
/*---------------------------------------------------------------------------*/
static void
publish_led_off(void *d)
{
leds_off(CC26XX_WEB_DEMO_STATUS_LED);
}
/*---------------------------------------------------------------------------*/
static void
save_config()
{
/* Dump current running config to flash */
#if BOARD_SENSORTAG || BOARD_LAUNCHPAD
int rv;
cc26xx_web_demo_sensor_reading_t *reading = NULL;
rv = ext_flash_open();
if(!rv) {
printf("Could not open flash to save config\n");
ext_flash_close();
return;
}
rv = ext_flash_erase(CONFIG_FLASH_OFFSET, sizeof(cc26xx_web_demo_config_t));
if(!rv) {
printf("Error erasing flash\n");
} else {
cc26xx_web_demo_config.magic = CONFIG_MAGIC;
cc26xx_web_demo_config.len = sizeof(cc26xx_web_demo_config_t);
cc26xx_web_demo_config.sensors_bitmap = 0;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(reading->publish) {
cc26xx_web_demo_config.sensors_bitmap |= (1 << reading->type);
}
}
rv = ext_flash_write(CONFIG_FLASH_OFFSET, sizeof(cc26xx_web_demo_config_t),
(uint8_t *)&cc26xx_web_demo_config);
if(!rv) {
printf("Error saving config\n");
}
}
ext_flash_close();
#endif
}
/*---------------------------------------------------------------------------*/
static void
load_config()
{
#if BOARD_SENSORTAG || BOARD_LAUNCHPAD
/* Read from flash into a temp buffer */
cc26xx_web_demo_config_t tmp_cfg;
cc26xx_web_demo_sensor_reading_t *reading = NULL;
int rv = ext_flash_open();
if(!rv) {
printf("Could not open flash to load config\n");
ext_flash_close();
return;
}
rv = ext_flash_read(CONFIG_FLASH_OFFSET, sizeof(tmp_cfg),
(uint8_t *)&tmp_cfg);
ext_flash_close();
if(!rv) {
printf("Error loading config\n");
return;
}
if(tmp_cfg.magic == CONFIG_MAGIC && tmp_cfg.len == sizeof(tmp_cfg)) {
memcpy(&cc26xx_web_demo_config, &tmp_cfg, sizeof(cc26xx_web_demo_config));
}
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(cc26xx_web_demo_config.sensors_bitmap & (1 << reading->type)) {
reading->publish = 1;
} else {
reading->publish = 0;
snprintf(reading->converted, CC26XX_WEB_DEMO_CONVERTED_LEN, "\"N/A\"");
}
}
#endif
}
/*---------------------------------------------------------------------------*/
/* Don't start everything here, we need to dictate order of initialisation */
AUTOSTART_PROCESSES(&cc26xx_web_demo_process);
/*---------------------------------------------------------------------------*/
int
cc26xx_web_demo_ipaddr_sprintf(char *buf, uint8_t buf_len,
const uip_ipaddr_t *addr)
{
uint16_t a;
uint8_t len = 0;
int i, f;
for(i = 0, f = 0; i < sizeof(uip_ipaddr_t); i += 2) {
a = (addr->u8[i] << 8) + addr->u8[i + 1];
if(a == 0 && f >= 0) {
if(f++ == 0) {
len += snprintf(&buf[len], buf_len - len, "::");
}
} else {
if(f > 0) {
f = -1;
} else if(i > 0) {
len += snprintf(&buf[len], buf_len - len, ":");
}
len += snprintf(&buf[len], buf_len - len, "%x", a);
}
}
return len;
}
/*---------------------------------------------------------------------------*/
const cc26xx_web_demo_sensor_reading_t *
cc26xx_web_demo_sensor_lookup(int sens_type)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(reading->type == sens_type) {
return reading;
}
}
return NULL;
}
/*---------------------------------------------------------------------------*/
const cc26xx_web_demo_sensor_reading_t *
cc26xx_web_demo_sensor_first()
{
return list_head(sensor_list);
}
/*---------------------------------------------------------------------------*/
void
cc26xx_web_demo_restore_defaults(void)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
leds_on(LEDS_ALL);
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
reading->publish = 1;
}
#if CC26XX_WEB_DEMO_MQTT_CLIENT
process_post_synch(&mqtt_client_process,
cc26xx_web_demo_load_config_defaults, NULL);
#endif
#if CC26XX_WEB_DEMO_NET_UART
process_post_synch(&net_uart_process, cc26xx_web_demo_load_config_defaults,
NULL);
#endif
save_config();
leds_off(LEDS_ALL);
}
/*---------------------------------------------------------------------------*/
static int
defaults_post_handler(char *key, int key_len, char *val, int val_len)
{
if(key_len != strlen("defaults") ||
strncasecmp(key, "defaults", strlen("defaults")) != 0) {
/* Not ours */
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
cc26xx_web_demo_restore_defaults();
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
/*---------------------------------------------------------------------------*/
static int
sensor_readings_handler(char *key, int key_len, char *val, int val_len)
{
cc26xx_web_demo_sensor_reading_t *reading = NULL;
int rv;
for(reading = list_head(sensor_list);
reading != NULL;
reading = list_item_next(reading)) {
if(key_len == strlen(reading->form_field) &&
strncmp(reading->form_field, key, strlen(key)) == 0) {
rv = atoi(val);
/* Be pedantic: only accept 0 and 1, not just any non-zero value */
if(rv == 0) {
reading->publish = 0;
snprintf(reading->converted, CC26XX_WEB_DEMO_CONVERTED_LEN, "\"N/A\"");
} else if(rv == 1) {
reading->publish = 1;
} else {
return HTTPD_SIMPLE_POST_HANDLER_ERROR;
}
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
}
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
/*---------------------------------------------------------------------------*/
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
static int
ping_interval_post_handler(char *key, int key_len, char *val, int val_len)
{
int rv = 0;
if(key_len != strlen("ping_interval") ||
strncasecmp(key, "ping_interval", strlen("ping_interval")) != 0) {
/* Not ours */
return HTTPD_SIMPLE_POST_HANDLER_UNKNOWN;
}
rv = atoi(val);
if(rv < CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN ||
rv > CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX) {
return HTTPD_SIMPLE_POST_HANDLER_ERROR;
}
cc26xx_web_demo_config.def_rt_ping_interval = rv * CLOCK_SECOND;
return HTTPD_SIMPLE_POST_HANDLER_OK;
}
#endif
/*---------------------------------------------------------------------------*/
HTTPD_SIMPLE_POST_HANDLER(sensor, sensor_readings_handler);
HTTPD_SIMPLE_POST_HANDLER(defaults, defaults_post_handler);
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
HTTPD_SIMPLE_POST_HANDLER(ping_interval, ping_interval_post_handler);
/*---------------------------------------------------------------------------*/
static void
echo_reply_handler(uip_ipaddr_t *source, uint8_t ttl, uint8_t *data,
uint16_t datalen)
{
if(uip_ip6addr_cmp(source, uip_ds6_defrt_choose())) {
def_rt_rssi = sicslowpan_get_last_rssi();
}
}
/*---------------------------------------------------------------------------*/
static void
ping_parent(void)
{
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
return;
}
uip_icmp6_send(uip_ds6_defrt_choose(), ICMP6_ECHO_REQUEST, 0,
CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN);
}
#endif
/*---------------------------------------------------------------------------*/
static void
get_batmon_reading(void *data)
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(batmon_temp_reading.publish) {
value = batmon_sensor.value(BATMON_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
batmon_temp_reading.raw = value;
buf = batmon_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d", value);
}
}
if(batmon_volt_reading.publish) {
value = batmon_sensor.value(BATMON_SENSOR_TYPE_VOLT);
if(value != CC26XX_SENSOR_READING_ERROR) {
batmon_volt_reading.raw = value;
buf = batmon_volt_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d", (value * 125) >> 5);
}
}
if(digital_ioid0_reading.publish) {
if(1) {
buf = digital_ioid0_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d",mode);
}}
ctimer_set(&batmon_timer, next, get_batmon_reading, NULL);
ctimer_set(&batmon_timer, next, get_batmon_reading, NULL);
}
/*---------------------------------------------------------------------------*/
#if BOARD_SENSORTAG
/*---------------------------------------------------------------------------*/
static void
compare_and_update(cc26xx_web_demo_sensor_reading_t *reading)
{
if(reading->last == reading->raw) {
reading->changed = 0;
} else {
reading->last = reading->raw;
reading->changed = 1;
}
}
/*---------------------------------------------------------------------------*/
static void
print_mpu_reading(int reading, char *buf)
{
char *loc_buf = buf;
if(reading < 0) {
sprintf(loc_buf, "-");
reading = -reading;
loc_buf++;
}
sprintf(loc_buf, "%d.%02d", reading / 100, reading % 100);
}
/*---------------------------------------------------------------------------*/
static void
get_bmp_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(bmp_pres_reading.publish) {
value = bmp_280_sensor.value(BMP_280_SENSOR_TYPE_PRESS);
if(value != CC26XX_SENSOR_READING_ERROR) {
bmp_pres_reading.raw = value;
compare_and_update(&bmp_pres_reading);
buf = bmp_pres_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
if(bmp_temp_reading.publish) {
value = bmp_280_sensor.value(BMP_280_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
bmp_temp_reading.raw = value;
compare_and_update(&bmp_temp_reading);
buf = bmp_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
SENSORS_DEACTIVATE(bmp_280_sensor);
ctimer_set(&bmp_timer, next, init_bmp_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_tmp_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(tmp_amb_reading.publish || tmp_obj_reading.publish) {
if(tmp_007_sensor.value(TMP_007_SENSOR_TYPE_ALL) ==
CC26XX_SENSOR_READING_ERROR) {
SENSORS_DEACTIVATE(tmp_007_sensor);
ctimer_set(&tmp_timer, next, init_tmp_reading, NULL);
}
}
if(tmp_amb_reading.publish) {
value = tmp_007_sensor.value(TMP_007_SENSOR_TYPE_AMBIENT);
tmp_amb_reading.raw = value;
compare_and_update(&tmp_amb_reading);
buf = tmp_amb_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%03d", value / 1000,
value % 1000);
}
if(tmp_obj_reading.publish) {
value = tmp_007_sensor.value(TMP_007_SENSOR_TYPE_OBJECT);
tmp_obj_reading.raw = value;
compare_and_update(&tmp_obj_reading);
buf = tmp_obj_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%03d", value / 1000,
value % 1000);
}
SENSORS_DEACTIVATE(tmp_007_sensor);
ctimer_set(&tmp_timer, next, init_tmp_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_hdc_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
if(hdc_temp_reading.publish) {
value = hdc_1000_sensor.value(HDC_1000_SENSOR_TYPE_TEMP);
if(value != CC26XX_SENSOR_READING_ERROR) {
hdc_temp_reading.raw = value;
compare_and_update(&hdc_temp_reading);
buf = hdc_temp_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
if(hdc_hum_reading.publish) {
value = hdc_1000_sensor.value(HDC_1000_SENSOR_TYPE_HUMIDITY);
if(value != CC26XX_SENSOR_READING_ERROR) {
hdc_hum_reading.raw = value;
compare_and_update(&hdc_hum_reading);
buf = hdc_hum_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
}
ctimer_set(&hdc_timer, next, init_hdc_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_light_reading()
{
int value;
char *buf;
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
value = opt_3001_sensor.value(0);
if(value != CC26XX_SENSOR_READING_ERROR) {
opt_reading.raw = value;
compare_and_update(&opt_reading);
buf = opt_reading.converted;
memset(buf, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
snprintf(buf, CC26XX_WEB_DEMO_CONVERTED_LEN, "%d.%02d", value / 100,
value % 100);
}
/* The OPT will turn itself off, so we don't need to call its DEACTIVATE */
ctimer_set(&opt_timer, next, init_light_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
get_mpu_reading()
{
clock_time_t next = SENSOR_READING_PERIOD +
(random_rand() % SENSOR_READING_RANDOM);
int raw;
if(mpu_gyro_x_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_X);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_x_reading.raw = raw;
}
}
if(mpu_gyro_y_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_Y);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_y_reading.raw = raw;
}
}
if(mpu_gyro_z_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_GYRO_Z);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_gyro_z_reading.raw = raw;
}
}
if(mpu_acc_x_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_X);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_x_reading.raw = raw;
}
}
if(mpu_acc_y_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_Y);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_y_reading.raw = raw;
}
}
if(mpu_acc_z_reading.publish) {
raw = mpu_9250_sensor.value(MPU_9250_SENSOR_TYPE_ACC_Z);
if(raw != CC26XX_SENSOR_READING_ERROR) {
mpu_acc_z_reading.raw = raw;
}
}
SENSORS_DEACTIVATE(mpu_9250_sensor);
if(mpu_gyro_x_reading.publish) {
compare_and_update(&mpu_gyro_x_reading);
memset(mpu_gyro_x_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_x_reading.raw, mpu_gyro_x_reading.converted);
}
if(mpu_gyro_y_reading.publish) {
compare_and_update(&mpu_gyro_y_reading);
memset(mpu_gyro_y_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_y_reading.raw, mpu_gyro_y_reading.converted);
}
if(mpu_gyro_z_reading.publish) {
compare_and_update(&mpu_gyro_z_reading);
memset(mpu_gyro_z_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_gyro_z_reading.raw, mpu_gyro_z_reading.converted);
}
if(mpu_acc_x_reading.publish) {
compare_and_update(&mpu_acc_x_reading);
memset(mpu_acc_x_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_x_reading.raw, mpu_acc_x_reading.converted);
}
if(mpu_acc_y_reading.publish) {
compare_and_update(&mpu_acc_y_reading);
memset(mpu_acc_y_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_y_reading.raw, mpu_acc_y_reading.converted);
}
if(mpu_acc_z_reading.publish) {
compare_and_update(&mpu_acc_z_reading);
memset(mpu_acc_z_reading.converted, 0, CC26XX_WEB_DEMO_CONVERTED_LEN);
print_mpu_reading(mpu_acc_z_reading.raw, mpu_acc_z_reading.converted);
}
/* We only use the single timer */
ctimer_set(&mpu_timer, next, init_mpu_reading, NULL);
}
/*---------------------------------------------------------------------------*/
static void
init_tmp_reading(void *data)
{
if(tmp_amb_reading.publish || tmp_obj_reading.publish) {
SENSORS_ACTIVATE(tmp_007_sensor);
} else {
ctimer_set(&tmp_timer, CLOCK_SECOND, init_tmp_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_bmp_reading(void *data)
{
if(bmp_pres_reading.publish || bmp_temp_reading.publish) {
SENSORS_ACTIVATE(bmp_280_sensor);
} else {
ctimer_set(&bmp_timer, CLOCK_SECOND, init_bmp_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_hdc_reading(void *data)
{
if(hdc_hum_reading.publish || hdc_temp_reading.publish) {
SENSORS_ACTIVATE(hdc_1000_sensor);
} else {
ctimer_set(&hdc_timer, CLOCK_SECOND, init_hdc_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_light_reading(void *data)
{
if(opt_reading.publish) {
SENSORS_ACTIVATE(opt_3001_sensor);
} else {
ctimer_set(&opt_timer, CLOCK_SECOND, init_light_reading, NULL);
}
}
/*---------------------------------------------------------------------------*/
static void
init_mpu_reading(void *data)
{
int readings_bitmap = 0;
if(mpu_acc_x_reading.publish || mpu_acc_y_reading.publish ||
mpu_acc_z_reading.publish) {
readings_bitmap |= MPU_9250_SENSOR_TYPE_ACC;
}
if(mpu_gyro_x_reading.publish || mpu_gyro_y_reading.publish ||
mpu_gyro_z_reading.publish) {
readings_bitmap |= MPU_9250_SENSOR_TYPE_GYRO;
}
if(readings_bitmap) {
mpu_9250_sensor.configure(SENSORS_ACTIVE, readings_bitmap);
} else {
ctimer_set(&mpu_timer, CLOCK_SECOND, init_mpu_reading, NULL);
}
}
#endif
/*---------------------------------------------------------------------------*/
static void
init_sensor_readings(void)
{
/*
* Make a first pass and get all initial sensor readings. This will also
* trigger periodic value updates
*/
get_batmon_reading(NULL);
#if BOARD_SENSORTAG
init_bmp_reading(NULL);
init_light_reading(NULL);
init_hdc_reading(NULL);
init_tmp_reading(NULL);
init_mpu_reading(NULL);
#endif /* BOARD_SENSORTAG */
return;
}
/*---------------------------------------------------------------------------*/
static void
init_sensors(void)
{
list_add(sensor_list, &batmon_temp_reading);
list_add(sensor_list, &batmon_volt_reading);
list_add(sensor_list, &digital_ioid0_reading);
SENSORS_ACTIVATE(batmon_sensor);
}
/*---------------------------------------------------------------------------*/
PROCESS_THREAD(cc26xx_web_demo_process, ev, data)
{
PROCESS_BEGIN();
printf("CC26XX Web Demo Process\n");
init_sensors();
cc26xx_web_demo_publish_event = process_alloc_event();
cc26xx_web_demo_config_loaded_event = process_alloc_event();
cc26xx_web_demo_load_config_defaults = process_alloc_event();
/* Start all other (enabled) processes first */
process_start(&httpd_simple_process, NULL);
#if CC26XX_WEB_DEMO_COAP_SERVER
process_start(&coap_server_process, NULL);
#endif
#if CC26XX_WEB_DEMO_6LBR_CLIENT
process_start(&cetic_6lbr_client_process, NULL);
#endif
#if CC26XX_WEB_DEMO_MQTT_CLIENT
process_start(&mqtt_client_process, NULL);
#endif
#if CC26XX_WEB_DEMO_NET_UART
process_start(&net_uart_process, NULL);
#endif
#if CC26XX_WEB_DEMO_DIGITAL_IOID0
process_start(&button_process,NULL);
#endif
cc26xx_web_demo_config.sensors_bitmap = 0xFFFFFFFF; /* all on by default */
cc26xx_web_demo_config.def_rt_ping_interval =
CC26XX_WEB_DEMO_DEFAULT_RSSI_MEAS_INTERVAL;
load_config();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_config_loaded_event, NULL);
init_sensor_readings();
httpd_simple_register_post_handler(&sensor_handler);
httpd_simple_register_post_handler(&defaults_handler);
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
httpd_simple_register_post_handler(&ping_interval_handler);
def_rt_rssi = 0x8000000;
uip_icmp6_echo_reply_callback_add(&echo_reply_notification,
echo_reply_handler);
etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
#endif
etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
/*
* Update all sensor readings on a configurable sensors_event
* (e.g a button press / or reed trigger)
*/
while(1) {
if(ev == PROCESS_EVENT_TIMER && etimer_expired(&et)) {
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
leds_on(CC26XX_WEB_DEMO_STATUS_LED);
ctimer_set(&ct, NO_NET_LED_DURATION, publish_led_off, NULL);
etimer_set(&et, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
}
}
#if CC26XX_WEB_DEMO_READ_PARENT_RSSI
if(ev == PROCESS_EVENT_TIMER && etimer_expired(&echo_request_timer)) {
if(uip_ds6_get_global(ADDR_PREFERRED) == NULL) {
etimer_set(&echo_request_timer, CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC);
} else {
ping_parent();
etimer_set(&echo_request_timer, cc26xx_web_demo_config.def_rt_ping_interval);
}
}
#endif
if(ev == sensors_event && data == CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER) {
if((CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER)->value(
BUTTON_SENSOR_VALUE_DURATION) > CLOCK_SECOND * 5) {
printf("Restoring defaults!\n");
cc26xx_web_demo_restore_defaults();
} else {
init_sensor_readings();
process_post(PROCESS_BROADCAST, cc26xx_web_demo_publish_event, NULL);
}
} else if(ev == httpd_simple_event_new_config) {
save_config();
#if BOARD_SENSORTAG
} else if(ev == sensors_event && data == &bmp_280_sensor) {
get_bmp_reading();
} else if(ev == sensors_event && data == &opt_3001_sensor) {
get_light_reading();
} else if(ev == sensors_event && data == &hdc_1000_sensor) {
get_hdc_reading();
} else if(ev == sensors_event && data == &tmp_007_sensor) {
get_tmp_reading();
} else if(ev == sensors_event && data == &mpu_9250_sensor) {
get_mpu_reading();
#endif
}
PROCESS_YIELD();
}
PROCESS_END();
}
/*---------------------------------------------------------------------------------*/
PROCESS_THREAD(button_process, ev, data) {
PROCESS_EXITHANDLER(goto exit);
PROCESS_BEGIN();
SENSORS_ACTIVATE(button_sensor);
printf("Clicca il bottone! \n");
leds_on(LEDS_ALL);
while(1) {
PROCESS_WAIT_EVENT_UNTIL((ev==sensors_event) && (data == &button_left_sensor));
printf("Premi il tasto! \n");
if(mode == 0) {
leds_toggle(LEDS_ALL);
} else if(mode == 1) {
leds_toggle(LEDS_ALL);
}
}
exit:
leds_off(LEDS_ALL);
PROCESS_END();
}
/**
* @}
*/
Can you give us a look?