Commit 5ae21908 authored by Yongjia Niu's avatar Yongjia Niu
Browse files

Main: Add IoTConnect Pub, Sub(Commands) Demo

parent dc01e747
#ifndef __DEVICES_SETTINGS_H__
#define __DEVICES_SETTINGS_H__
#define ENTRY_EMBEST_CPID "617449FD62B649E7AD1050DB4E95096E"
#define ENTRY_EMBEST_ENV "Avnet"
#define ENTRY_EMBEST_COMPANY_NAME "Embest Tech"
#define EMBEST_PSOC6_KIT_NAME "PSoC 6 WiFi-BT Pioneer Kit"
#define EMBEST_PSOC6_KIT_ID "psoc6kit1"
#define EMBEST_PSOC6_KIT_DEMO_INFINEON_NAME "PSoC 6 WiFi-BT Pioneer Kit Demo for Infineon Conference"
#define EMBEST_PSOC6_KIT_DEMO_INFINEON_ID "psoc6kit-infineon-demo"
#define EMBEST_PSOC6_KIT_DEMO_TE_NAME "PSoC 6 WiFi-BT Pioneer Kit Demo for TE Conference"
#define EMBEST_PSOC6_KIT_DEMO_TE_ID "psoc6kit-te-demo"
#endif
\ No newline at end of file
#include "mbed.h"
#include "mbed_trace.h"
#include "NTPClient.h"
#include "IoTConnectCommon.h"
#include "IoTConnectClient.h"
#include "IoTConnectHttpClient.h"
#include "GyroAttr.h"
#include "ColorAttr.h"
#include "devices_settings.h"
#define TRACE_GROUP "Main"
// Blinking rate in milliseconds
#define BLINKING_RATE_MS 500
#define PUB_PERIOD_MS 10000
#define SENSOR_GRYO_UPDATE_RATE_MS 1000
I2C i2c(I2C_SDA, I2C_SCL);
RGBLed led_rgb(LED_RED, LED_GREEN, LED_BLUE);
......@@ -13,48 +24,157 @@ BMI160_I2C *imu = NULL;
GyroAttr *gyro = NULL;
ColorAttr *color = NULL;
static const char* colors[8] = {
"red", "green", "blue","magenta",
"cyan", "yellow", "white", "black"
};
NetworkInterface* network = NULL;
IoTConnectEntry embest(ENTRY_EMBEST_COMPANY_NAME, ENTRY_EMBEST_CPID, ENTRY_EMBEST_ENV);
IoTConnectDevice* psoc_dev;
IoTConnectClient* client = NULL;
IoTConnectHttpClient* http_client = NULL;
// static const char* colors[8] = {
// "red", "green", "blue", "magenta",
// "cyan", "yellow", "white", "black"
// };
void cmd_set_color(int argc, char* argv[]) {
if (argc < 2) {
return;
}
const char* cmd = argv[0];
const char* new_color_name = argv[1];
if (cmd && new_color_name && color) {
tr_info("New command[%s %s] received and handled.", cmd, new_color_name);
color->set_color(new_color_name);
}
}
NetworkInterface* connect_network() {
NetworkInterface* net = NULL;
tr_info("Opening network interface...");
net = NetworkInterface::get_default_instance();
if (!net) {
tr_error("Unable to open network interface.");
return NULL;
}
nsapi_error_t net_status = NSAPI_ERROR_NO_CONNECTION;
while ((net_status = net->connect()) != NSAPI_ERROR_OK) {
tr_error("Unable to connect to network (%d). Retrying...", net_status);
}
SocketAddress address;
net->get_ip_address(&address);
tr_info("Connected to the network successfully. IP address: %s\n", address.get_ip_address());
return net;
}
int sync_rtc_time(NetworkInterface* net, const char* ntp_server = NULL) {
const char* ntp_srv = NULL;
if (net == NULL) {
return -1;
}
if (ntp_server) {
ntp_srv = ntp_server;
} else {
ntp_srv = "ntp.aliyun.com";
}
NTPClient ntp(net);
ntp.set_server(ntp_srv, 123);
tr_info("Try to get timestamp from %s", ntp_srv);
time_t now = ntp.get_timestamp();
if (now <= 0) {
tr_error("Failed to retrieve the time from %s:123", ntp_srv);
return -1;
}
set_time(now);
tr_info("Time is now %s", ctime(&now));
return 0;
}
int main() {
int ret = 0;
cJSON *attr = NULL;
char *js = NULL;
int i = 0;
mbed_trace_init();
#ifdef MBED_MAJOR_VERSION
printf("Mbed OS version %d.%d.%d\n\n", MBED_MAJOR_VERSION, MBED_MINOR_VERSION, MBED_PATCH_VERSION);
#endif
i2c.frequency(400000);
imu = new BMI160_I2C(i2c, BMI160_I2C::I2C_ADRS_SDO_LO);
gyro = new GyroAttr(imu);
color = new ColorAttr(&led_rgb);
attr = cJSON_CreateObject();
psoc_dev = new IoTConnectDevice(EMBEST_PSOC6_KIT_DEMO_INFINEON_ID, EMBEST_PSOC6_KIT_DEMO_INFINEON_NAME, &embest);
// psoc_dev = new IoTConnectDevice(EMBEST_PSOC6_KIT_DEMO_TE_ID, EMBEST_PSOC6_KIT_DEMO_TE_NAME, &embest);
attr = psoc_dev->get_attr();
cJSON_AddItemReferenceToObject(attr, "gyro", gyro->get_gyro());
cJSON_AddItemReferenceToObject(attr, "color", color->get_color());
psoc_dev->register_cmd("set_color", cmd_set_color);
gyro->sensor_init();
gyro->start_sensor_update(500);
gyro->start_sensor_update(SENSOR_GRYO_UPDATE_RATE_MS);
while(true) {
js = cJSON_PrintUnformatted(attr);
// Connect to network
network = connect_network();
if (network == NULL) {
return -1;
}
if (js) {
printf("%s\n", js);
free(js);
}
// sync the real time clock (RTC)
ret = sync_rtc_time(network);
if (ret != 0) {
return ret;
}
color->set_color(colors[i++]);
if (i >= 8) {
i = 0;
}
http_client = new IoTConnectHttpClient(network, psoc_dev);
http_client->init_device_cfg();
ThisThread::sleep_for(2000);
client = new IoTConnectClient(network, psoc_dev);
ret = client->connect();
if (ret != 0) {
tr_error("Connect failed, exit");
return -1;
}
ret = client->subscribe(MQTT::QOS0);
if (ret != 0) {
tr_error("Failed to subscribe");
return -1;
}
return 0;
ret = client->start_main_loop();
if (ret != 0) {
tr_error("Failed to start client thread main loop");
return -1;
}
// Initialise the digital pin LED1 as an output
DigitalOut led(LED2);
}
\ No newline at end of file
while (true) {
client->pub();
for(i = 0; i < PUB_PERIOD_MS; i += BLINKING_RATE_MS) {
led = !led;
ThisThread::sleep_for(BLINKING_RATE_MS);
}
}
return 0;
}
https://github.com/ARMmbed/ntp-client/#a4ccf62992b9adc5086a9afea08fa4deb2e2fbd7
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment