Mario Castaldo
Published © GPL3+

Monitoring Analog Input with CoAP

Monitoring the status of an Analog Input (sensor) through CoAP protocol.

IntermediateProtip2 hours1,232
Monitoring Analog Input with CoAP

Things used in this project

Hardware components

X.IP5 SmartMachine®
Telecontrolli X.IP5 SmartMachine®
×2
Raspberry Pi 3 Model B
Raspberry Pi 3 Model B
×1
Texas Instruments SmartRF06EB Evaluation Board
×1
USB-A to Micro-USB Cable
USB-A to Micro-USB Cable
×4
Jumper wires (generic)
Jumper wires (generic)
×3
Allied Electronics FLAT Cable
×1
SD Card 8gb min
×1
Telecontrolli Photoresistor VT935
×1

Software apps and online services

Texas Instruments Smart Programmer 2

Story

Read more

Schematics

Schematic X.IP5 SmartMachine

Schematico2 qsq1vlxkxn

Code

CC26xx-web-demo.c code

C/C++
This file contains the code is initialized for the Analog input with the addition of the resource on CoAP and must be replaced with the board.h inside the path: contiki/examples/cc26xx/cc26xx-web-demo
/*
 Author: Telecontrolli srl
 File: cc26xx-web-demo.c V.1.3
 */
/**
 *   An example demonstrating:
 *   * how to use a CC26XX-powered node in a deployment driven by a 6LBR
 *   * how to expose on-device sensors as CoAP resources
 *   * how to build a small web page which reports networking and sensory data
 *   * how to configure functionality through the aforementioned web page using
 *     HTTP POST requests
 *   * a network-based UART
 */
*---------------------------------------------------------------------------*/
#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;
  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 (IOID0, 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();
}
/*---------------------------------------------------------------------------*/
/**
 * @}
 */

CC26xx.web.demo.h code

C/C++
This file contains the code is initialized for the Analog input with the addition of the resource on CoAP and must be replaced with the board.h inside the path: contiki/examples/cc26xx/cc26xx-web-demo
/*
 Author: Telecontrolli srl
 File: cc26xx-web-demo.h V1.3
 */
/**
 *   An example demonstrating:
 *   * how to use a CC26XX-powered node in a deployment driven by a 6LBR
 *   * how to expose on-device sensors as CoAP resources
 *   * how to build a small web page which reports networking and sensory data
 *   * how to configure functionality through the aforementioned web page using
 *     HTTP POST requests
 *   * a network-based UART
 */
/*---------------------------------------------------------------------------*/
#ifndef CC26XX_WEB_DEMO_H_
#define CC26XX_WEB_DEMO_H_
/*---------------------------------------------------------------------------*/
#include "dev/leds.h"
#include "sys/process.h"
#include "mqtt-client.h"
#include "net-uart.h"

#include <stdint.h>
/*---------------------------------------------------------------------------*/
#ifdef CC26XX_WEB_DEMO_CONF_MQTT_CLIENT
#define CC26XX_WEB_DEMO_MQTT_CLIENT CC26XX_WEB_DEMO_CONF_MQTT_CLIENT
#else
#define CC26XX_WEB_DEMO_MQTT_CLIENT 1
#endif

#ifdef CC26XX_WEB_DEMO_CONF_6LBR_CLIENT
#define CC26XX_WEB_DEMO_6LBR_CLIENT CC26XX_WEB_DEMO_CONF_6LBR_CLIENT
#else
#define CC26XX_WEB_DEMO_6LBR_CLIENT 1
#endif

#ifdef CC26XX_WEB_DEMO_CONF_COAP_SERVER
#define CC26XX_WEB_DEMO_COAP_SERVER CC26XX_WEB_DEMO_CONF_COAP_SERVER
#else
#define CC26XX_WEB_DEMO_COAP_SERVER 1
#endif

#ifdef CC26XX_WEB_DEMO_CONF_NET_UART
#define CC26XX_WEB_DEMO_NET_UART CC26XX_WEB_DEMO_CONF_NET_UART
#else
#define CC26XX_WEB_DEMO_NET_UART 1
#endif

#ifdef CC26XX_WEB_DEMO_CONF_ADC_IOID14
#define CC26XX_WEB_DEMO_ADC_IOI14 CC26XX_WEB_DEMO_CONF_ADC_IOID14
#else
#define CC26XX_WEB_DEMO_ADC_IOID14 1
#endif
/*---------------------------------------------------------------------------*/
/* Active probing of RSSI from our preferred parent */
#if (CC26XX_WEB_DEMO_COAP_SERVER || CC26XX_WEB_DEMO_MQTT_CLIENT)
#define CC26XX_WEB_DEMO_READ_PARENT_RSSI 1
#else
#define CC26XX_WEB_DEMO_READ_PARENT_RSSI 0
#endif

#define CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MAX 86400 /* secs: 1 day */
#define CC26XX_WEB_DEMO_RSSI_MEASURE_INTERVAL_MIN     5 /* secs */
/*---------------------------------------------------------------------------*/
/* User configuration */
/* Take a sensor reading on button press */
#define CC26XX_WEB_DEMO_SENSOR_READING_TRIGGER &button_left_sensor

/* Payload length of ICMPv6 echo requests used to measure RSSI with def rt */
#define CC26XX_WEB_DEMO_ECHO_REQ_PAYLOAD_LEN   20

#if BOARD_SENSORTAG
/* Force an MQTT publish on sensor event */
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &reed_relay_sensor
#elif BOARD_LAUNCHPAD
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &button_left_sensor
#else
#define CC26XX_WEB_DEMO_MQTT_PUBLISH_TRIGGER &button_down_sensor
#endif

#define CC26XX_WEB_DEMO_STATUS_LED LEDS_GREEN
/*---------------------------------------------------------------------------*/
/* A timeout used when waiting to connect to a network */
#define CC26XX_WEB_DEMO_NET_CONNECT_PERIODIC        (CLOCK_SECOND >> 3)
/*---------------------------------------------------------------------------*/
/* Default configuration values */
#define CC26XX_WEB_DEMO_DEFAULT_ORG_ID              "quickstart"
#if CPU_FAMILY_CC13XX
#define CC26XX_WEB_DEMO_DEFAULT_TYPE_ID             "cc13xx"
#else
#define CC26XX_WEB_DEMO_DEFAULT_TYPE_ID             "cc26xx"
#endif
#define CC26XX_WEB_DEMO_DEFAULT_EVENT_TYPE_ID       "status"
#define CC26XX_WEB_DEMO_DEFAULT_SUBSCRIBE_CMD_TYPE  "+"
#define CC26XX_WEB_DEMO_DEFAULT_BROKER_PORT         1883
#define CC26XX_WEB_DEMO_DEFAULT_PUBLISH_INTERVAL    (30 * CLOCK_SECOND)
#define CC26XX_WEB_DEMO_DEFAULT_KEEP_ALIVE_TIMER    60
#define CC26XX_WEB_DEMO_DEFAULT_RSSI_MEAS_INTERVAL  (CLOCK_SECOND * 30)
/*---------------------------------------------------------------------------*/
/*
 * You normally won't have to change anything from here onwards unless you are
 * extending the example
 */
/*---------------------------------------------------------------------------*/
/* Sensor types */
#define CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP     0
#define CC26XX_WEB_DEMO_SENSOR_BATMON_VOLT     1
#define CC26XX_WEB_DEMO_SENSOR_BMP_PRES        2
#define CC26XX_WEB_DEMO_SENSOR_BMP_TEMP        3
#define CC26XX_WEB_DEMO_SENSOR_TMP_AMBIENT     4
#define CC26XX_WEB_DEMO_SENSOR_TMP_OBJECT      5
#define CC26XX_WEB_DEMO_SENSOR_HDC_TEMP        6
#define CC26XX_WEB_DEMO_SENSOR_HDC_HUMIDITY    7
#define CC26XX_WEB_DEMO_SENSOR_OPT_LIGHT       8
#define CC26XX_WEB_DEMO_SENSOR_MPU_ACC_X       9
#define CC26XX_WEB_DEMO_SENSOR_MPU_ACC_Y      10
#define CC26XX_WEB_DEMO_SENSOR_MPU_ACC_Z      11
#define CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_X     12
#define CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_Y     13
#define CC26XX_WEB_DEMO_SENSOR_MPU_GYRO_Z     14
#define CC26XX_WEB_DEMO_SENSOR_ADC_IOID14     15 
/*---------------------------------------------------------------------------*/
extern process_event_t cc26xx_web_demo_publish_event;
extern process_event_t cc26xx_web_demo_config_loaded_event;
extern process_event_t cc26xx_web_demo_load_config_defaults;
/*---------------------------------------------------------------------------*/
#define CC26XX_WEB_DEMO_UNIT_TEMP     "C"
#define CC26XX_WEB_DEMO_UNIT_VOLT     "mV"
#define CC26XX_WEB_DEMO_UNIT_PRES     "hPa"
#define CC26XX_WEB_DEMO_UNIT_HUMIDITY "%RH"
#define CC26XX_WEB_DEMO_UNIT_LIGHT    "lux"
#define CC26XX_WEB_DEMO_UNIT_ACC      "G"
#define CC26XX_WEB_DEMO_UNIT_GYRO     "deg per sec"
/*---------------------------------------------------------------------------*/
/* A data type for sensor readings, internally stored in a linked list */
#define CC26XX_WEB_DEMO_CONVERTED_LEN        12

typedef struct cc26xx_web_demo_sensor_reading {
  struct cc26xx_web_demo_sensor_reading *next;
  int raw;
  int last;
  const char *descr;
  const char *xml_element;
  const char *form_field;
  char *units;
  uint8_t type;
  uint8_t publish;
  uint8_t changed;
  char converted[CC26XX_WEB_DEMO_CONVERTED_LEN];
} cc26xx_web_demo_sensor_reading_t;
/*---------------------------------------------------------------------------*/
/* Global configuration */
typedef struct cc26xx_web_demo_config_s {
  uint32_t magic;
  int len;
  uint32_t sensors_bitmap;
  int def_rt_ping_interval;
  mqtt_client_config_t mqtt_config;
  net_uart_config_t net_uart;
} cc26xx_web_demo_config_t;

extern cc26xx_web_demo_config_t cc26xx_web_demo_config;
/*---------------------------------------------------------------------------*/
/**
 * \brief Performs a lookup for a reading of a specific type of sensor
 * \param sens_type CC26XX_WEB_DEMO_SENSOR_BATMON_TEMP...
 * \return A pointer to the reading data structure or NULL
 */
const cc26xx_web_demo_sensor_reading_t *cc26xx_web_demo_sensor_lookup(int sens_type);

/**
 * \brief Returns the first available sensor reading
 * \return A pointer to the reading data structure or NULL
 */
const cc26xx_web_demo_sensor_reading_t *cc26xx_web_demo_sensor_first(void);

/**
 * \brief Print an IPv6 address into a buffer
 * \param buf A pointer to the buffer where this function will print the IPv6
 *        address
 * \param buf_len the length of the buffer
 * \param addr A pointer to the IPv6 address
 * \return The number of bytes written to the buffer
 *
 * It is the caller's responsibility to allocate enough space for buf
 */
int cc26xx_web_demo_ipaddr_sprintf(char *buf, uint8_t buf_len,
                                   const uip_ipaddr_t *addr);

/**
 * \brief Resets the example to a default configuration
 */
void cc26xx_web_demo_restore_defaults(void);
/*---------------------------------------------------------------------------*/
#endif /* CC26XX_WEB_DEMO_H_ */
/*---------------------------------------------------------------------------*/
/**
 * @}
 * @}
 */

res-sensors.c cose

C/C++
This file contains the code is initialized for the Analog input with the addition of the resource on CoAP and must be replaced with the board.h inside the path: contiki/examples/cc26xx/cc26xx-web-demo/resources
/*
 * Author: Telecontrolli srl
 * File: res-sensors.c V1.3
 * 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=\"Temperature\";rt=\"C\"",
         res_get_handler_batmon_temp, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
RESOURCE(res_batmon_volt, "Titolo=\"Battery Level\";rt=\"mV\"",
         res_get_handler_batmon_volt, NULL, NULL, NULL);
/*---------------------------------------------------------------------------*/
RESOURCE(res_adc_ioid14, "Titolo=\"ADC analogic\";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 */
/*---------------------------------------------------------------------------*/
/** @} */

Board.h code

C/C++
This board.h code describes the PIN enumeration device and must be replaced with the board.h inside the path: contiki/platform/srf06-cc26xx/srf06/cc13xx
/*
 * Author: Telecontrolli srl
 * File: board.h V1.3
 * Description:  Defines related to the SmartRF06 Evaluation Board with a CC1310EM
 *
 * 	 	 This file provides connectivity information on LEDs, Buttons, UART and other peripherals
 *
 * 	         This file can be used as the basis to configure other boards using the
 *               CC13xx/CC26xx code as their basis.
 *
 *               This file is not meant to be modified by the user.
 * Header file with definitions related to the I/O connections on the TI
 * SmartRF06 Evaluation Board with a CC1310EM
 */
/*---------------------------------------------------------------------------*/
#ifndef BOARD_H_
#define BOARD_H_
/*---------------------------------------------------------------------------*/
#include "ioc.h"
/*---------------------------------------------------------------------------*/
/**
 * \name LED configurations
 *
 * Those values are not meant to be modified by the user
 * @{
 */
/* Some files include leds.h before us, so we need to get rid of defaults in
 * leds.h before we provide correct definitions */
#undef LEDS_GREEN
#undef LEDS_YELLOW
#undef LEDS_RED
#undef LEDS_CONF_ALL

#define LEDS_RED       1 /**< LED1 (Red)    */
#define LEDS_YELLOW    2 /**< LED2 (Yellow) */
#define LEDS_GREEN     4 /**< LED3 (Green)  */
#define LEDS_ORANGE    8 /**< LED4 (Orange) */
#define button	         /**< IOID_0 (Input)*/

#define LEDS_CONF_ALL 15

/* Notify various examples that we have LEDs */
#define PLATFORM_HAS_LEDS        1
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name LED IOID mappings
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_LED_1          IOID_13
#define BOARD_IOID_LED_2          IOID_12
#define BOARD_IOID_LED_3          IOID_UNUSED
#define BOARD_IOID_LED_4          IOID_UNUSED	
#define BOARD_LED_1               (1 << BOARD_IOID_LED_1)
#define BOARD_LED_2               (1 << BOARD_IOID_LED_2)
#define BOARD_LED_3               (1 << BOARD_IOID_LED_3)
#define BOARD_LED_4               (1 << BOARD_IOID_LED_4)
#define BOARD_LED_ALL             (BOARD_LED_1 | BOARD_LED_2 | BOARD_LED_3 | \
                                   BOARD_LED_4)
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name UART IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_UART_RX        IOID_8
#define BOARD_IOID_UART_TX        IOID_7
#define BOARD_IOID_UART_CTS       IOID_UNUSED
#define BOARD_IOID_UART_RTS       IOID_UNUSED
#define BOARD_UART_RX             (1 << BOARD_IOID_UART_RX)
#define BOARD_UART_TX             (1 << BOARD_IOID_UART_TX)
#define BOARD_UART_CTS            (1 << BOARD_IOID_UART_CTS)
#define BOARD_UART_RTS            (1 << BOARD_IOID_UART_RTS)
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name Button IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_KEY_LEFT       IOID_14
#define BOARD_IOID_KEY_RIGHT      IOID_UNUSED
#define BOARD_IOID_KEY_UP         IOID_UNUSED
#define BOARD_IOID_KEY_DOWN       IOID_UNUSED
#define BOARD_IOID_KEY_SELECT     IOID_UNUSED
#define BOARD_KEY_LEFT            (1 << BOARD_IOID_KEY_LEFT)
#define BOARD_KEY_RIGHT           (1 << BOARD_IOID_KEY_RIGHT)
#define BOARD_KEY_UP              (1 << BOARD_IOID_KEY_UP)
#define BOARD_KEY_DOWN            (1 << BOARD_IOID_KEY_DOWN)
#define BOARD_KEY_SELECT          (1 << BOARD_IOID_KEY_SELECT)
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name 3.3V domain IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_3V3_EN         IOID_UNUSED
#define BOARD_3V3_EN              (1 << BOARD_IOID_3V3_EN)
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name SPI IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_SPI_SCK        IOID_4
#define BOARD_IOID_SPI_MOSI       IOID_3
#define BOARD_IOID_SPI_MISO       IOID_2
#define BOARD_SPI_SCK             (1 << BOARD_IOID_SPI_SCK)
#define BOARD_SPI_MOSI            (1 << BOARD_IOID_SPI_MOSI)
#define BOARD_SPI_MISO            (1 << BOARD_IOID_SPI_MISO)
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name LCD IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_LCD_MODE       IOID_UNUSED
#define BOARD_IOID_LCD_RST        IOID_UNUSED
#define BOARD_IOID_LCD_CS         IOID_1
#define BOARD_IOID_LCD_SCK        BOARD_IOID_SPI_SCK
#define BOARD_IOID_LCD_MOSI       BOARD_IOID_SPI_MOSI
#define BOARD_LCD_MODE            (1 << BOARD_IOID_LCD_MODE)
#define BOARD_LCD_RST             (1 << BOARD_IOID_LCD_RST)
#define BOARD_LCD_CS              (1 << BOARD_IOID_LCD_CS)
#define BOARD_LCD_SCK             BOARD_SPI_SCK
#define BOARD_LCD_MOSI            BOARD_SPI_MOSI
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name SD Card IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_SDCARD_CS      IOID_UNUSED
#define BOARD_SDCARD_CS           (1 << BOARD_IOID_SDCARD_CS)
#define BOARD_IOID_SDCARD_SCK     BOARD_IOID_SPI_SCK
#define BOARD_SDCARD_SCK          BOARD_SPI_SCK
#define BOARD_IOID_SDCARD_MOSI    BOARD_IOID_SPI_MOSI
#define BOARD_SDCARD_MOSI         BOARD_SPI_MOSI
#define BOARD_IOID_SDCARD_MISO    BOARD_IOID_SPI_MISO
#define BOARD_SDCARD_MISO         BOARD_SPI_MISO
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name ALS IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_ALS_PWR        IOID_5
#define BOARD_IOID_ALS_OUT        IOID_6
#define BOARD_ALS_PWR             (1 << BOARD_IOID_ALS_PWR)
#define BOARD_ALS_OUT             (1 << BOARD_IOID_ALS_OUT)
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name ACC IOID mapping
 *
 * Those values are not meant to be modified by the user
 * @{
 */
#define BOARD_IOID_ACC_PWR        IOID_9
#define BOARD_IOID_ACC_INT        IOID_10
#define BOARD_IOID_ACC_INT1       IOID_11
#define BOARD_IOID_ACC_INT2       IOID_UNUSED
#define BOARD_IOID_ACC_CS         IOID_UNUSED
#define BOARD_ACC_PWR             (1 << BOARD_IOID_ACC_PWR)
#define BOARD_ACC_INT             (1 << BOARD_IOID_ACC_INT)
#define BOARD_ACC_INT1            (1 << BOARD_IOID_ACC_INT1)
#define BOARD_ACC_INT2            (1 << BOARD_IOID_ACC_INT2)
#define BOARD_ACC_CS              (1 << BOARD_IOID_ACC_CS)
#define BOARD_IOID_ACC_SCK        BOARD_IOID_SPI_SCK
#define BOARD_ACC_SCK             BOARD_SPI_SCK
#define BOARD_IOID_ACC_MOSI       BOARD_IOID_SPI_MOSI
#define BOARD_ACC_MOSI            BOARD_SPI_MOSI
#define BOARD_IOID_ACC_MISO       BOARD_IOID_SPI_MISO
#define BOARD_ACC_MISO            BOARD_SPI_MISO
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \brief ROM bootloader configuration
 *
 * Change SET_CCFG_BL_CONFIG_BL_PIN_NUMBER to BOARD_IOID_KEY_xyz to select
 * which button triggers the bootloader on reset.
 *
 * The remaining values are not meant to be modified by the user
 * @{
 */
#if ROM_BOOTLOADER_ENABLE
#define SET_CCFG_BL_CONFIG_BOOTLOADER_ENABLE            0xC5
#define SET_CCFG_BL_CONFIG_BL_LEVEL                     0x00
#define SET_CCFG_BL_CONFIG_BL_PIN_NUMBER                BOARD_IOID_KEY_SELECT
#define SET_CCFG_BL_CONFIG_BL_ENABLE                    0xC5
#else
#define SET_CCFG_BL_CONFIG_BOOTLOADER_ENABLE            0x00
#define SET_CCFG_BL_CONFIG_BL_LEVEL                     0x01
#define SET_CCFG_BL_CONFIG_BL_PIN_NUMBER                0xFF
#define SET_CCFG_BL_CONFIG_BL_ENABLE                    0xFF
#endif
/** @} */
/*---------------------------------------------------------------------------*/
/**
 * \name Device string used on startup
 * @{
 */
#define BOARD_STRING "X.IP4T+EB or XIP5"
/** @} */
/*---------------------------------------------------------------------------*/
#endif /* BOARD_H_ */
/*---------------------------------------------------------------------------*/
/**
 * @}
 * @}
 */
CREDITS

22538388	
Mario Castaldo
FollowContact

coap-server.c code

C/C++
This file contains the code is initialized for the Analog input with the addition of the resource on CoAP and must be replaced with the board.h inside the path: contiki/examples/cc26xx/cc26xx-web-demo/resources
/*
 * Author: Telecontrolli srl
 * File: coap-server.c V1.3
 * 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_digital_ioid0;
//extern resource_t res_digital;

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_red;
extern resource_t res_toggle_green;
extern resource_t res_toggle_yellow;
//extern resource_t res_toggle_orange;

/* 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_green, "Output/Digital/IOID11");
  rest_activate_resource(&res_toggle_red, "Output/Led on Board/Red");
  rest_activate_resource(&res_toggle_yellow, "Output/Led on Board/Yellow");
  //rest_activate_resource(&res_toggle_orange, "Output/Led on Board/Orange");
  //rest_activate_resource(&res_digital, "Output/Digital");
  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, "Sensor/batmon/Temperature");
  rest_activate_resource(&res_batmon_volt, "Sensor/batmon/Battery Level");
  rest_activate_resource(&res_adc_ioid14, "Input/Analog/IOID14");
  //rest_activate_resource(&res_digital_ioid0, "Input/Digital/IOID0");
  
  rest_activate_resource(&res_device_hw, "Device/Middleware/Hardware");
  rest_activate_resource(&res_device_sw, "Device/Middleware/Software");
  //rest_activate_resource(&res_device_uptime, "Device/Uptime");
  //rest_activate_resource(&res_device_cfg_reset, "Device/Reset");

  //rest_activate_resource(&res_parent_rssi, "Rete/Nodo/RSSI");
  rest_activate_resource(&res_parent_ip, "Network/Address/IPv6");

#if RF_BLE_ENABLED
  //rest_activate_resource(&res_ble_advd, "Device/ble_advd");
  #endif

  start_board_resources();

  /* Define application-specific events here. */
  while(1) {
    PROCESS_WAIT_EVENT();
  }

  PROCESS_END();
}
/*---------------------------------------------------------------------------*/
/**
 * @}
 */

Credits

Mario Castaldo

Mario Castaldo

4 projects • 1 follower

Comments