Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: FXOS8700CQ MODSERIAL mbed
Fork of Avnet_ATT_Cellular_IOT by
Diff: main.cpp
- Revision:
- 84:0bf8168b690a
- Parent:
- 82:a6c68da2b94a
- Child:
- 85:43d791cd5967
--- a/main.cpp Wed Dec 21 08:15:23 2016 +0000
+++ b/main.cpp Thu Dec 22 06:31:32 2016 +0000
@@ -34,8 +34,7 @@
//********************************************************************************************************************************************
//* Create string with sensor readings that can be sent to flow as an HTTP get
//********************************************************************************************************************************************
-K64F_Sensors_t SENSOR_DATA =
-{
+K64F_Sensors_t SENSOR_DATA = {
.Temperature = "0",
.Humidity = "0",
.AccelX = "0",
@@ -66,13 +65,11 @@
.GPS_Course = "0"
};
-void display_app_firmware_version(void)
-{
+void display_app_firmware_version(void) {
PUTS("\r\n\r\nApp Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n");
}
-void GenerateModemString(char * modem_string, long long hid, long long sid)
-{
+void GenerateModemString(char * modem_string, long long hid, long long sid) {
sprintf(
modem_string,
"GET %s/hikings/%lld/sessions/%lld/moves?lat=%s&lng=%s HTTP/1.1\r\nHost: %s\r\n\r\n",
@@ -82,7 +79,6 @@
SENSOR_DATA.GPS_Latitude,
SENSOR_DATA.GPS_Longitude,
MY_SERVER_URL);
-
} //GenerateModemString
@@ -94,11 +90,9 @@
//********************************************************************************************************************************************
//* Periodic 1ms timer tick
//********************************************************************************************************************************************
-void OneMsFunction()
-{
+void OneMsFunction() {
OneMsTicks++;
- if ((OneMsTicks % iTimer1Interval_ms) == 0)
- {
+ if ((OneMsTicks % iTimer1Interval_ms) == 0) {
bTimerExpiredFlag = true;
}
} //OneMsFunction()
@@ -107,8 +101,7 @@
//* Set the RGB LED's Color
//* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue)
//********************************************************************************************************************************************
-void SetLedColor(unsigned char ucColor)
-{
+void SetLedColor(unsigned char ucColor) {
//Note that when an LED is on, you write a 0 to it:
led_red = !(ucColor & 0x1); //bit 0
led_green = !(ucColor & 0x2); //bit 1
@@ -118,8 +111,7 @@
//********************************************************************************************************************************************
//* Process the JSON response. In this example we are only extracting a LED color.
//********************************************************************************************************************************************
-bool parse_JSON(char* json_string)
-{
+bool parse_JSON(char* json_string) {
char* beginquote;
char token[] = "\"LED\":\"";
beginquote = strstr(json_string, token );
@@ -182,6 +174,36 @@
}
} //parse_JSON
+void extract_longlong(const char* s, char v[]) {
+ long long value = strtoll(s, NULL, 0);
+ sprintf(v, "%lld", value);
+}
+
+void find_longlong(const char* s, const char* token, char v[]) {
+ const char* tokenBegin = strstr(s, token);
+ if (tokenBegin != 0) {
+ extract_longlong(tokenBegin + strlen(token), v);
+ }
+}
+
+void find_first_hiking(char hikingId[]) {
+ char request[512];
+ char response[512];
+ sprintf(request, "GET %s/hikings HTTP/1.1\r\nHost: %s\r\n\r\n", FLOW_BASE_URL, MY_SERVER_URL);
+ if (cell_modem_Sendreceive(&request[0], &response[0])) {
+ find_longlong(response, "\"id\":", hikingId);
+ }
+}
+
+void join_hiking(const char* hikingId, const char* name, char sessionId[]) {
+ char request[512];
+ char response[512];
+ sprintf(request, "GET %s/hikings/%s/sessions?name=%s HTTP/1.1\r\nHost: %s\r\n\r\n", FLOW_BASE_URL, hikingId, name, MY_SERVER_URL);
+ if (cell_modem_Sendreceive(&request[0], &response[0])) {
+ find_longlong(response, "\"id\":", sessionId);
+ }
+}
+
int main() {
static unsigned ledOnce = 0;
//delay so that the debug terminal can open after power-on reset:
@@ -209,14 +231,26 @@
//Create a 1ms timer tick function:
iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS;
OneMsTicker.attach(OneMsFunction, 0.001f) ;
+
+ char hid[20] = {0};
+ char sid[20] = {0};
+ char sessionName[20] = {0};
+
+ sprintf(sessionName, "IoT-kit-%d", rand() % 1000);
+
+ find_first_hiking(hid);
+ PRINTF("Found Hiking ID: ");
+ PRINTF(hid);
+ PRINTF("\r\n");
+
+ join_hiking(hid, sessionName, sid);
+ PRINTF("Allocated Session ID: ");
+ PRINTF(sid);
+ PRINTF("\r\n");
// Send and receive data perpetually
while(1) {
- #ifdef USE_VIRTUAL_SENSORS
- ProcessUsbInterface();
- #endif
- if (bTimerExpiredFlag)
- {
+ if (bTimerExpiredFlag) {
bTimerExpiredFlag = false;
read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor
char modem_string[512];
