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
main.cpp
00001 /* =================================================================== 00002 Copyright © 2016, AVNET Inc. 00003 00004 Licensed under the Apache License, Version 2.0 (the "License"); 00005 you may not use this file except in compliance with the License. 00006 You may obtain a copy of the License at 00007 00008 http://www.apache.org/licenses/LICENSE-2.0 00009 00010 Unless required by applicable law or agreed to in writing, 00011 software distributed under the License is distributed on an 00012 "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, 00013 either express or implied. See the License for the specific 00014 language governing permissions and limitations under the License. 00015 00016 ======================================================================== */ 00017 00018 #include "mbed.h" 00019 #include <cctype> 00020 #include <string> 00021 #include "config_me.h" 00022 #include "sensors.h" 00023 #include "cell_modem.h" 00024 #include "hardware.h" 00025 00026 I2C i2c(PTC11, PTC10); //SDA, SCL -- define the I2C pins being used 00027 MODSERIAL pc(USBTX, USBRX, 256, 256); // tx, rx with default tx, rx buffer sizes 00028 MODSERIAL mdm(PTD3, PTD2, 4096, 4096); 00029 DigitalOut led_green(LED_GREEN); 00030 DigitalOut led_red(LED_RED); 00031 DigitalOut led_blue(LED_BLUE); 00032 00033 #define OK_COLOR 0x2 00034 #define ERROR_COLOR 0x1 00035 00036 00037 //******************************************************************************************************************************************** 00038 //* Create string with sensor readings that can be sent to flow as an HTTP get 00039 //******************************************************************************************************************************************** 00040 K64F_Sensors_t SENSOR_DATA = { 00041 .Temperature = "0", 00042 .Humidity = "0", 00043 .AccelX = "0", 00044 .AccelY = "0", 00045 .AccelZ = "0", 00046 .MagnetometerX = "0", 00047 .MagnetometerY = "0", 00048 .MagnetometerZ = "0", 00049 .AmbientLightVis = "0", 00050 .AmbientLightIr = "0", 00051 .UVindex = "0", 00052 .Proximity = "0", 00053 .Temperature_Si7020 = "0", 00054 .Humidity_Si7020 = "0", 00055 .Virtual_Sensor1 = "0", 00056 .Virtual_Sensor2 = "0", 00057 .Virtual_Sensor3 = "0", 00058 .Virtual_Sensor4 = "0", 00059 .Virtual_Sensor5 = "0", 00060 .Virtual_Sensor6 = "0", 00061 .Virtual_Sensor7 = "0", 00062 .Virtual_Sensor8 = "0", 00063 .GPS_Satellites = "0", 00064 .GPS_Latitude = "0", 00065 .GPS_Longitude = "0", 00066 .GPS_Altitude = "0", 00067 .GPS_Speed = "0", 00068 .GPS_Course = "0" 00069 }; 00070 00071 char hid[20] = {0}; 00072 char sid[20] = {0}; 00073 char sessionName[20] = {0}; 00074 00075 void display_app_firmware_version(void) { 00076 PUTS("\r\n\r\nHiking Pal Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n"); 00077 } 00078 00079 //Periodic timer 00080 Ticker OneMsTicker; 00081 volatile bool bTimerExpiredFlag = false; 00082 int OneMsTicks = 0; 00083 int iTimer1Interval_ms = 1000; 00084 //******************************************************************************************************************************************** 00085 //* Periodic 1ms timer tick 00086 //******************************************************************************************************************************************** 00087 void OneMsFunction() { 00088 OneMsTicks++; 00089 if ((OneMsTicks % iTimer1Interval_ms) == 0) { 00090 bTimerExpiredFlag = true; 00091 } 00092 } //OneMsFunction() 00093 00094 //******************************************************************************************************************************************** 00095 //* Set the RGB LED's Color 00096 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 00097 //******************************************************************************************************************************************** 00098 void SetLedColor(unsigned char ucColor) { 00099 //Note that when an LED is on, you write a 0 to it: 00100 led_red = !(ucColor & 0x1); //bit 0 00101 led_green = !(ucColor & 0x2); //bit 1 00102 led_blue = !(ucColor & 0x4); //bit 2 00103 } //SetLedColor() 00104 00105 void extract_longlong(const char* s, char v[]) { 00106 long long value = strtoll(s, NULL, 0); 00107 sprintf(v, "%lld", value); 00108 } 00109 00110 long long find_longlong(const char* s, const char* token, const char** end) { 00111 const char* tokenBegin = strstr(s, token); 00112 if (tokenBegin != 0) { 00113 *end = tokenBegin + strlen(token); 00114 return strtoll(tokenBegin + strlen(token), NULL, 0); 00115 } 00116 return 0; 00117 } 00118 00119 void find_longlong(const char* s, const char* token, char v[]) { 00120 const char* tokenBegin = strstr(s, token); 00121 if (tokenBegin != 0) { 00122 extract_longlong(tokenBegin + strlen(token), v); 00123 } 00124 } 00125 00126 00127 int send_receive(char* request, char* response) { 00128 int result = cell_modem_Sendreceive(request, response); 00129 SetLedColor(result ? OK_COLOR : ERROR_COLOR); 00130 return result; 00131 } 00132 00133 int send_only(char* request) { 00134 char response[512]; 00135 return send_receive(request, response); 00136 } 00137 00138 void find_latest_hiking(char hikingId[]) { 00139 char request[512]; 00140 char response[512]; 00141 sprintf(request, "GET %s/hikings HTTP/1.1\r\nHost: %s\r\n\r\n", FLOW_BASE_URL, MY_SERVER_URL); 00142 if (send_receive(&request[0], &response[0])) { 00143 long long llv; 00144 long long latestId = 0; 00145 const char* begin = response; 00146 PRINTF("%s\n\n", begin); 00147 00148 char token[] = "\"id\":"; 00149 for (;;) { 00150 llv = find_longlong(begin, token, &begin); 00151 00152 PRINTF("---> %lld\n", llv); 00153 PRINTF("%s\n", begin); 00154 00155 if (llv == 0) { 00156 break; 00157 } 00158 if (llv > latestId) { 00159 latestId = llv; 00160 } 00161 } 00162 sprintf(hikingId, "%lld", latestId); 00163 PRINTF("LATEST HIKING ID: %lld", latestId); 00164 } 00165 } 00166 00167 void join_hiking(const char* hikingId, const char* name, char sessionId[]) { 00168 char request[512]; 00169 char response[512]; 00170 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); 00171 if (send_receive(&request[0], &response[0])) { 00172 find_longlong(response, "\"id\":", sessionId); 00173 } 00174 } 00175 00176 void generate_move_request(char request[], const char* hid, const char* sid) { 00177 sprintf( 00178 request, 00179 "GET %s/hikings/%s/sessions/%s/moves?lat=%s&lng=%s HTTP/1.1\r\nHost: %s\r\n\r\n", 00180 FLOW_BASE_URL, 00181 hid, 00182 sid, 00183 SENSOR_DATA.GPS_Latitude, 00184 SENSOR_DATA.GPS_Longitude, 00185 MY_SERVER_URL); 00186 } 00187 void report_move(const char* hid, const char* sid) { 00188 char request[512]; 00189 generate_move_request(request, hid, sid); 00190 send_only(&request[0]); 00191 } 00192 00193 int main() { 00194 //delay so that the debug terminal can open after power-on reset: 00195 wait (5.0); 00196 pc.baud(115200); 00197 00198 display_app_firmware_version(); 00199 00200 PRINTF(GRN "Hiking Pal tracking device started!\r\n\r\n"); 00201 00202 //Initialize the I2C sensors that are present 00203 sensors_init(); 00204 read_sensors(); 00205 00206 // Set LED to RED until init finishes 00207 SetLedColor(0x1); //Red 00208 // Initialize the modem 00209 PRINTF("\r\n"); 00210 cell_modem_init(); 00211 display_wnc_firmware_rev(); 00212 00213 // Set LED BLUE for partial init 00214 SetLedColor(0x4); //Blue 00215 00216 //Create a 1ms timer tick function: 00217 iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS; 00218 OneMsTicker.attach(OneMsFunction, 0.001f) ; 00219 00220 sprintf(sessionName, "IoT-kit-%d", rand() % 1000); 00221 00222 find_latest_hiking(hid); 00223 PRINTF("Found Hiking ID: "); 00224 PRINTF(hid); 00225 PRINTF("\r\n"); 00226 00227 join_hiking(hid, sessionName, sid); 00228 PRINTF("Allocated Session ID: "); 00229 PRINTF(sid); 00230 PRINTF("\r\n"); 00231 00232 // Send and receive data perpetually 00233 while(1) { 00234 if (bTimerExpiredFlag) { 00235 bTimerExpiredFlag = false; 00236 read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor 00237 report_move(hid, sid); 00238 } 00239 } //forever loop 00240 }
Generated on Tue Jul 12 2022 17:36:30 by
1.7.2
