Add websocket data feed, send temperature data

PUBLISHED_FROM=e56601f99ccf80d8420b4b54b0ef7c272ec4a656
This commit is contained in:
Deomid Ryabkov 2016-03-23 19:17:11 +00:00 committed by rojer
parent c091e6910b
commit b6d700077a
6 changed files with 176 additions and 11 deletions

View File

@ -92,10 +92,11 @@ CC3200_PLATFORM_SRCS = cc3200_fs.c cc3200_fs_slfs.c cc3200_libc.c cc3200_socket.
VPATH += $(COMMON_PATH)/platforms/cc3200
FREERTOS_SRCS = timers.c list.c queue.c tasks.c port.c heap_3.c osi_freertos.c
DRIVER_SRCS = cpu.c i2c.c interrupt.c pin.c prcm.c spi.c uart.c udma.c utils.c
DRIVER_SRCS = cpu.c i2c.c i2c_if.c interrupt.c pin.c prcm.c spi.c uart.c udma.c utils.c
SL_SRCS = socket.c wlan.c driver.c device.c netapp.c netcfg.c cc_pal.c fs.c
SDK_SRCS = startup_gcc.c $(FREERTOS_SRCS) $(DRIVER_SRCS) $(SL_SRCS)
IPATH += $(SDK_PATH) $(SDK_PATH)/inc $(SDK_PATH)/driverlib $(SDK_PATH)/oslib \
IPATH += $(SDK_PATH) $(SDK_PATH)/inc $(SDK_PATH)/driverlib \
$(SDK_PATH)/example/common $(SDK_PATH)/oslib \
$(SDK_PATH)/simplelink $(SDK_PATH)/simplelink/include \
$(SDK_PATH)/third_party/FreeRTOS/source \
$(SDK_PATH)/third_party/FreeRTOS/source/include \
@ -106,7 +107,7 @@ VPATH += $(SDK_PATH)/driverlib $(SDK_PATH)/example/common $(SDK_PATH)/oslib \
$(SDK_PATH)/third_party/FreeRTOS/source/portable/GCC/ARM_CM4 \
$(SDK_PATH)/third_party/FreeRTOS/source/portable/MemMang \
APP_SRCS = main.c mongoose.c $(CC3200_PLATFORM_SRCS) $(SDK_SRCS)
APP_SRCS = main.c tmp006.c mongoose.c $(CC3200_PLATFORM_SRCS) $(SDK_SRCS)
APP_OBJS = $(addprefix $(OBJDIR)/,$(patsubst %.c,%.o,$(APP_SRCS)))
$(FW_ELF): $(APP_OBJS)

View File

@ -21,20 +21,27 @@
#include "uart.h"
#include "utils.h"
#include <stdbool.h>
#include "i2c_if.h"
#include "simplelink.h"
#include "device.h"
#include "oslib/osi.h"
#include <mongoose.h>
#include "mongoose.h"
#include "tmp006.h"
#define CONSOLE_BAUD_RATE 115200
#define CONSOLE_UART UARTA0_BASE
#define CONSOLE_UART_PERIPH PRCM_UARTA0
#define SYS_CLK 80000000
#define WIFI_SSID "YourWiFi"
#define WIFI_PASS "YourPassword"
#define TMP006_ADDR 0x41
struct sj_event {
int type;
void *data;
@ -60,14 +67,105 @@ void SimpleLinkNetAppEventHandler(SlNetAppEvent_t *e) {
}
}
static void data_ev_handler(struct mg_connection *nc, int ev, void *p);
struct temp_data {
double ts;
double volt;
double temp;
};
static struct temp_data s_temp_data;
static void ev_handler(struct mg_connection *nc, int ev, void *p) {
LOG(LL_DEBUG, ("Ev: %d", ev));
if (ev == MG_EV_HTTP_REQUEST) {
struct mg_serve_http_opts opts;
memset(&opts, 0, sizeof(opts));
opts.document_root = ".";
mg_serve_http(nc, (struct http_message *) p, opts);
switch (ev) {
case MG_EV_ACCEPT: {
char addr[32];
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
LOG(LL_INFO, ("%p conn from %s", nc, addr));
break;
}
case MG_EV_HTTP_REQUEST: {
char addr[32];
struct http_message *hm = (struct http_message *) p;
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
LOG(LL_INFO,
("HTTP request from %s: %.*s %.*s", addr, (int) hm->method.len,
hm->method.p, (int) hm->uri.len, hm->uri.p));
struct mg_serve_http_opts opts;
memset(&opts, 0, sizeof(opts));
opts.document_root = ".";
mg_serve_http(nc, (struct http_message *) p, opts);
break;
}
case MG_EV_CLOSE: {
LOG(LL_INFO, ("%p closed", nc));
break;
}
case MG_EV_WEBSOCKET_HANDSHAKE_DONE: {
LOG(LL_INFO, ("%p switching to data mode", nc));
nc->handler = data_ev_handler;
nc->ev_timer_time = mg_time(); /* Immediately */
break;
}
case MG_EV_TIMER: {
double volt = tmp006_read_sensor_voltage(TMP006_ADDR);
double temp = tmp006_read_die_temp(TMP006_ADDR);
if (volt != TMP006_INVALID_READING && temp != TMP006_INVALID_READING) {
s_temp_data.temp = temp;
s_temp_data.volt = volt;
s_temp_data.ts = mg_time();
LOG(LL_DEBUG, ("V = %lf mV, T = %lf C", volt, temp));
}
nc->ev_timer_time = mg_time() + 0.05;
}
}
}
struct conn_data {
double last_ts;
double last_temp;
double last_volt;
};
static void send_temp(struct mg_connection *nc) {
LOG(LL_DEBUG, ("%p sending temp data", nc));
mg_printf_websocket_frame(
nc, WEBSOCKET_OP_TEXT,
"{\"type\": \"temp\", \"ts\": %lf, \"v\": %lf, \"t\": %lf}",
s_temp_data.ts, s_temp_data.volt, s_temp_data.temp);
}
static void data_ev_handler(struct mg_connection *nc, int ev, void *p) {
struct conn_data *cd = (struct conn_data *) nc->user_data;
if (cd == NULL) {
cd = (struct conn_data *) calloc(1, sizeof(*cd));
nc->user_data = cd;
}
switch (ev) {
case MG_EV_POLL:
case MG_EV_TIMER: {
const double now = mg_time();
/* Send if there was a change or repeat last data every second. */
if (s_temp_data.volt != cd->last_volt ||
s_temp_data.temp != cd->last_temp ||
now > cd->last_ts + 1.0) {
send_temp(nc);
cd->last_ts = now;
cd->last_volt = s_temp_data.volt;
cd->last_temp = s_temp_data.temp;
}
nc->ev_timer_time = now + 0.05;
break;
}
case MG_EV_CLOSE: {
LOG(LL_INFO, ("%p closed", nc));
free(cd);
break;
}
}
}
@ -77,6 +175,9 @@ static void mg_task(void *arg) {
osi_MsgQCreate(&s_v7_q, "V7", sizeof(struct sj_event), 32 /* len */);
sl_Start(NULL, NULL, NULL);
if (!tmp006_set_config(TMP006_ADDR, TMP006_CONV_2, false)) {
LOG(LL_ERROR, ("Failed to init temperature sensor"));
}
if (strlen(WIFI_SSID) > 0) {
int ret;
@ -111,6 +212,7 @@ static void mg_task(void *arg) {
struct mg_connection *nc = mg_bind(&mg_mgr, "80", ev_handler);
if (nc != NULL) {
mg_set_protocol_http_websocket(nc);
nc->ev_timer_time = mg_time(); /* Start data collection */
} else {
LOG(LL_ERROR, ("Failed to create listener: %s", err));
}
@ -118,7 +220,7 @@ static void mg_task(void *arg) {
while (1) {
struct sj_event e;
mg_mgr_poll(&mg_mgr, 0);
if (osi_MsgQRead(&s_v7_q, &e, 1000) != OSI_OK) continue;
if (osi_MsgQRead(&s_v7_q, &e, 1) != OSI_OK) continue;
}
}
@ -146,6 +248,10 @@ int main() {
setvbuf(stderr, NULL, _IONBF, 0);
cs_log_set_level(LL_INFO);
MAP_PinTypeI2C(PIN_01, PIN_MODE_1); /* SDA */
MAP_PinTypeI2C(PIN_02, PIN_MODE_1); /* SCL */
I2C_IF_Open(I2C_MASTER_MODE_FST);
VStartSimpleLinkSpawnTask(8);
osi_TaskCreate(mg_task, (const signed char *) "mg", 8192, NULL, 3, NULL);
osi_start();

Binary file not shown.

After

Width:  |  Height:  |  Size: 17 KiB

38
examples/CC3200/tmp006.c Normal file
View File

@ -0,0 +1,38 @@
#include "tmp006.h"
#include "mongoose.h"
#include "i2c_if.h"
#define TMP006_REG_SENSOR_VOLTAGE 0x00
#define TMP006_REG_DIE_TEMP 0x01
#define TMP006_REG_CONFIG 0x02
bool tmp006_set_config(
uint8_t addr, enum tmp006_conversion_rate conv_rate, bool drdy_en) {
unsigned char val[3] = {TMP006_REG_CONFIG, 0x80, 0};
/* Reset first */
if (I2C_IF_Write(addr, val, 3, 1) != 0) return false;
val[1] = 0x70 | (conv_rate << 1) | drdy_en;
return (I2C_IF_Write(addr, val, 3, 1) == 0);
}
double tmp006_read_sensor_voltage(uint8_t addr) {
unsigned char reg = TMP006_REG_SENSOR_VOLTAGE;
unsigned char val[2] = {0, 0};
int status = I2C_IF_ReadFrom(addr, &reg, 1, val, 2);
if (status < 0) return -1000;
int voltage = (val[0] << 8) | val[1];
if (val[0] & 0x80) voltage = -((1 << 16) - voltage);
return voltage * 0.00015625;
}
double tmp006_read_die_temp(uint8_t addr) {
unsigned char reg = TMP006_REG_DIE_TEMP;
unsigned char val[2] = {0, 0};
int status = I2C_IF_ReadFrom(addr, &reg, 1, val, 2);
if (status < 0) return -1000;
int temp = (val[0] << 6) | (val[1] >> 2);
if (val[0] & 0x80) temp = -((1 << 14) - temp);
return temp / 32.0;
}

21
examples/CC3200/tmp006.h Normal file
View File

@ -0,0 +1,21 @@
#ifndef CS_MONGOOSE_EXAMPLES_CC3200_TMP006_H_
#define CS_MONGOOSE_EXAMPLES_CC3200_TMP006_H_
#include <inttypes.h>
#include <stdbool.h>
enum tmp006_conversion_rate {
TMP006_CONV_4 = 0,
TMP006_CONV_2,
TMP006_CONV_1,
TMP006_CONV_1_2,
TMP006_CONV_1_4,
};
bool tmp006_set_config(uint8_t addr, enum tmp006_conversion_rate conv_rate, bool drdy_en);
double tmp006_read_sensor_voltage(uint8_t addr);
double tmp006_read_die_temp(uint8_t addr);
#define TMP006_INVALID_READING (-1000.0)
#endif /* CS_MONGOOSE_EXAMPLES_CC3200_TMP006_H_ */

View File

@ -37,7 +37,6 @@ void ev_handler(struct mg_connection *nc, int ev, void *p) {
case MG_EV_HTTP_REQUEST: {
char addr[32];
struct http_message *hm = (struct http_message *) p;
(void) hm;
mg_sock_addr_to_str(&nc->sa, addr, sizeof(addr),
MG_SOCK_STRINGIFY_IP | MG_SOCK_STRINGIFY_PORT);
LOG(LL_INFO,