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
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 00034 //******************************************************************************************************************************************** 00035 //* Create string with sensor readings that can be sent to flow as an HTTP get 00036 //******************************************************************************************************************************************** 00037 K64F_Sensors_t SENSOR_DATA = 00038 { 00039 .Temperature = "0", 00040 .Humidity = "0", 00041 .AccelX = "0", 00042 .AccelY = "0", 00043 .AccelZ = "0", 00044 .MagnetometerX = "0", 00045 .MagnetometerY = "0", 00046 .MagnetometerZ = "0", 00047 .AmbientLightVis = "0", 00048 .AmbientLightIr = "0", 00049 .UVindex = "0", 00050 .Proximity = "0", 00051 .Temperature_Si7020 = "0", 00052 .Humidity_Si7020 = "0", 00053 .Virtual_Sensor1 = "0", 00054 .Virtual_Sensor2 = "0", 00055 .Virtual_Sensor3 = "0", 00056 .Virtual_Sensor4 = "0", 00057 .Virtual_Sensor5 = "0", 00058 .Virtual_Sensor6 = "0", 00059 .Virtual_Sensor7 = "0", 00060 .Virtual_Sensor8 = "0", 00061 .GPS_Satellites = "0", 00062 .GPS_Latitude = "0", 00063 .GPS_Longitude = "0", 00064 .GPS_Altitude = "0", 00065 .GPS_Speed = "0", 00066 .GPS_Course = "0" 00067 }; 00068 00069 void display_app_firmware_version(void) 00070 { 00071 PUTS("\r\n\r\nApp Firmware: Release 1.0 - built: "__DATE__" "__TIME__"\r\n\r\n"); 00072 } 00073 00074 void GenerateModemString(char * modem_string) 00075 { 00076 switch(iSensorsToReport) 00077 { 00078 case TEMP_HUMIDITY_ONLY: 00079 { 00080 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, FLOW_URL_TYPE, MY_SERVER_URL); 00081 break; 00082 } 00083 case TEMP_HUMIDITY_ACCELEROMETER: 00084 { 00085 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, FLOW_URL_TYPE, MY_SERVER_URL); 00086 break; 00087 } 00088 case TEMP_HUMIDITY_ACCELEROMETER_GPS: 00089 { 00090 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&gps_satellites=%s&latitude=%s&longitude=%s&altitude=%s&speed=%s&course=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ,SENSOR_DATA.GPS_Satellites,SENSOR_DATA.GPS_Latitude,SENSOR_DATA.GPS_Longitude,SENSOR_DATA.GPS_Altitude,SENSOR_DATA.GPS_Speed,SENSOR_DATA.GPS_Course, FLOW_URL_TYPE, MY_SERVER_URL); 00091 break; 00092 } 00093 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS: 00094 { 00095 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, FLOW_URL_TYPE, MY_SERVER_URL); 00096 break; 00097 } 00098 case TEMP_HUMIDITY_ACCELEROMETER_PMODSENSORS_VIRTUALSENSORS: 00099 { 00100 sprintf(modem_string, "GET %s%s?serial=%s&temp=%s&humidity=%s&accelX=%s&accelY=%s&accelZ=%s&proximity=%s&light_uv=%s&light_vis=%s&light_ir=%s&virt_sens1=%s&virt_sens2=%s&virt_sens3=%s&virt_sens4=%s&virt_sens5=%s&virt_sens6=%s&virt_sens7=%s&virt_sens8=%s %s%s\r\n\r\n", FLOW_BASE_URL, FLOW_INPUT_NAME, FLOW_DEVICE_NAME, SENSOR_DATA.Temperature, SENSOR_DATA.Humidity, SENSOR_DATA.AccelX,SENSOR_DATA.AccelY,SENSOR_DATA.AccelZ, SENSOR_DATA.Proximity, SENSOR_DATA.UVindex, SENSOR_DATA.AmbientLightVis, SENSOR_DATA.AmbientLightIr, SENSOR_DATA.Virtual_Sensor1, SENSOR_DATA.Virtual_Sensor2, SENSOR_DATA.Virtual_Sensor3, SENSOR_DATA.Virtual_Sensor4, SENSOR_DATA.Virtual_Sensor5, SENSOR_DATA.Virtual_Sensor6, SENSOR_DATA.Virtual_Sensor7, SENSOR_DATA.Virtual_Sensor8, FLOW_URL_TYPE, MY_SERVER_URL); 00101 break; 00102 } 00103 default: 00104 { 00105 sprintf(modem_string, "Invalid sensor selected\r\n\r\n"); 00106 break; 00107 } 00108 } //switch(iSensorsToReport) 00109 } //GenerateModemString 00110 00111 00112 //Periodic timer 00113 Ticker OneMsTicker; 00114 volatile bool bTimerExpiredFlag = false; 00115 int OneMsTicks = 0; 00116 int iTimer1Interval_ms = 1000; 00117 //******************************************************************************************************************************************** 00118 //* Periodic 1ms timer tick 00119 //******************************************************************************************************************************************** 00120 void OneMsFunction() 00121 { 00122 OneMsTicks++; 00123 if ((OneMsTicks % iTimer1Interval_ms) == 0) 00124 { 00125 bTimerExpiredFlag = true; 00126 } 00127 } //OneMsFunction() 00128 00129 //******************************************************************************************************************************************** 00130 //* Set the RGB LED's Color 00131 //* LED Color 0=Off to 7=White. 3 bits represent BGR (bit0=Red, bit1=Green, bit2=Blue) 00132 //******************************************************************************************************************************************** 00133 void SetLedColor(unsigned char ucColor) 00134 { 00135 //Note that when an LED is on, you write a 0 to it: 00136 led_red = !(ucColor & 0x1); //bit 0 00137 led_green = !(ucColor & 0x2); //bit 1 00138 led_blue = !(ucColor & 0x4); //bit 2 00139 } //SetLedColor() 00140 00141 //******************************************************************************************************************************************** 00142 //* Process the JSON response. In this example we are only extracting a LED color. 00143 //******************************************************************************************************************************************** 00144 bool parse_JSON(char* json_string) 00145 { 00146 char* beginquote; 00147 char token[] = "\"LED\":\""; 00148 beginquote = strstr(json_string, token ); 00149 if ((beginquote != 0)) 00150 { 00151 char cLedColor = beginquote[strlen(token)]; 00152 PRINTF(GRN "LED Found : %c" DEF "\r\n", cLedColor); 00153 switch(cLedColor) 00154 { 00155 case 'O': 00156 { //Off 00157 SetLedColor(0); 00158 break; 00159 } 00160 case 'R': 00161 { //Red 00162 SetLedColor(1); 00163 break; 00164 } 00165 case 'G': 00166 { //Green 00167 SetLedColor(2); 00168 break; 00169 } 00170 case 'Y': 00171 { //Yellow 00172 SetLedColor(3); 00173 break; 00174 } 00175 case 'B': 00176 { //Blue 00177 SetLedColor(4); 00178 break; 00179 } 00180 case 'M': 00181 { //Magenta 00182 SetLedColor(5); 00183 break; 00184 } 00185 case 'T': 00186 { //Turquoise 00187 SetLedColor(6); 00188 break; 00189 } 00190 case 'W': 00191 { //White 00192 SetLedColor(7); 00193 break; 00194 } 00195 default: 00196 { 00197 break; 00198 } 00199 } //switch(cLedColor) 00200 return true; 00201 } 00202 else 00203 { 00204 return false; 00205 } 00206 } //parse_JSON 00207 00208 int main() { 00209 static unsigned ledOnce = 0; 00210 //delay so that the debug terminal can open after power-on reset: 00211 wait (5.0); 00212 pc.baud(115200); 00213 00214 display_app_firmware_version(); 00215 00216 PRINTF(GRN "Hello World from the Cellular IoT Kit!\r\n\r\n"); 00217 00218 //Initialize the I2C sensors that are present 00219 sensors_init(); 00220 read_sensors(); 00221 00222 // Set LED to RED until init finishes 00223 SetLedColor(0x1); //Red 00224 // Initialize the modem 00225 PRINTF("\r\n"); 00226 cell_modem_init(); 00227 display_wnc_firmware_rev(); 00228 00229 // Set LED BLUE for partial init 00230 SetLedColor(0x4); //Blue 00231 00232 //Create a 1ms timer tick function: 00233 iTimer1Interval_ms = SENSOR_UPDATE_INTERVAL_MS; 00234 OneMsTicker.attach(OneMsFunction, 0.001f) ; 00235 00236 // Send and receive data perpetually 00237 while(1) { 00238 #ifdef USE_VIRTUAL_SENSORS 00239 ProcessUsbInterface(); 00240 #endif 00241 if (bTimerExpiredFlag) 00242 { 00243 bTimerExpiredFlag = false; 00244 read_sensors(); //read available external sensors from a PMOD and the on-board motion sensor 00245 char modem_string[512]; 00246 GenerateModemString(&modem_string[0]); 00247 char myJsonResponse[512]; 00248 if (cell_modem_Sendreceive(&modem_string[0], &myJsonResponse[0])) 00249 { 00250 if (!ledOnce) 00251 { 00252 ledOnce = 1; 00253 SetLedColor(0x2); //Green 00254 } 00255 parse_JSON(&myJsonResponse[0]); 00256 } 00257 } //bTimerExpiredFlag 00258 } //forever loop 00259 }
Generated on Tue Jul 12 2022 19:03:09 by
1.7.2
Avnet ATT WNC 14A2A Cellular IoT Kit