Axeda Corp
/
AxedaGo-ubloxC027
Axeda demo software for u-blox C027 (GSM)
main.cpp@0:a725e8eab383, 2014-08-11 (annotated)
- Committer:
- AxedaCorp
- Date:
- Mon Aug 11 19:02:42 2014 +0000
- Revision:
- 0:a725e8eab383
- Child:
- 1:ff6d8adaf6b9
1st commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
AxedaCorp | 0:a725e8eab383 | 1 | #include "axTransport.h" |
AxedaCorp | 0:a725e8eab383 | 2 | #include "mbed.h" |
AxedaCorp | 0:a725e8eab383 | 3 | #include "GPS.h" |
AxedaCorp | 0:a725e8eab383 | 4 | #include "MDM.h" |
AxedaCorp | 0:a725e8eab383 | 5 | #include "axToolkit.h" |
AxedaCorp | 0:a725e8eab383 | 6 | #include <stdlib.h> |
AxedaCorp | 0:a725e8eab383 | 7 | #include <stdio.h> |
AxedaCorp | 0:a725e8eab383 | 8 | #include <ctype.h> |
AxedaCorp | 0:a725e8eab383 | 9 | |
AxedaCorp | 0:a725e8eab383 | 10 | #define SIMPIN NULL |
AxedaCorp | 0:a725e8eab383 | 11 | #define APN "wap.cingular" |
AxedaCorp | 0:a725e8eab383 | 12 | #define USERNAME "wap@cingulargprs.com" |
AxedaCorp | 0:a725e8eab383 | 13 | #define PASSWORD "cingular1" |
AxedaCorp | 0:a725e8eab383 | 14 | |
AxedaCorp | 0:a725e8eab383 | 15 | //------------------------------------------------------------------------------------ |
AxedaCorp | 0:a725e8eab383 | 16 | |
AxedaCorp | 0:a725e8eab383 | 17 | #define MODEL "nlr_ublox_c028" |
AxedaCorp | 0:a725e8eab383 | 18 | #define SERIAL_NUM "lymes" |
AxedaCorp | 0:a725e8eab383 | 19 | #define HOST "toolbox-stage-connect.axeda.com" |
AxedaCorp | 0:a725e8eab383 | 20 | #define PORT (80) |
AxedaCorp | 0:a725e8eab383 | 21 | #define PING (700) |
AxedaCorp | 0:a725e8eab383 | 22 | |
AxedaCorp | 0:a725e8eab383 | 23 | axToolkitConfig cfg; |
AxedaCorp | 0:a725e8eab383 | 24 | //Initial AMMP configuration |
AxedaCorp | 0:a725e8eab383 | 25 | axToolkitConfig *initConfig(axToolkitConfig *cfg) |
AxedaCorp | 0:a725e8eab383 | 26 | |
AxedaCorp | 0:a725e8eab383 | 27 | { |
AxedaCorp | 0:a725e8eab383 | 28 | char *model = MODEL; |
AxedaCorp | 0:a725e8eab383 | 29 | char *serial = SERIAL_NUM; |
AxedaCorp | 0:a725e8eab383 | 30 | const char *host = HOST; |
AxedaCorp | 0:a725e8eab383 | 31 | int port = PORT; |
AxedaCorp | 0:a725e8eab383 | 32 | |
AxedaCorp | 0:a725e8eab383 | 33 | printf("STATUS: Initializing AMMP configuration\r\n"); |
AxedaCorp | 0:a725e8eab383 | 34 | ax_createPlatform(&cfg->platform, host,NULL,port); |
AxedaCorp | 0:a725e8eab383 | 35 | cfg->platform.secure=AX_FALSE; |
AxedaCorp | 0:a725e8eab383 | 36 | cfg->platform.verify=AX_FALSE; |
AxedaCorp | 0:a725e8eab383 | 37 | cfg->platform.ping_rate=PING; |
AxedaCorp | 0:a725e8eab383 | 38 | |
AxedaCorp | 0:a725e8eab383 | 39 | printf("STATUS: Initializing device ID\r\n"); |
AxedaCorp | 0:a725e8eab383 | 40 | ax_data_createModelSerialDeviceId(&cfg->thisDevice, model, serial, NULL); |
AxedaCorp | 0:a725e8eab383 | 41 | cfg->conf_recv_timeout = 5000; |
AxedaCorp | 0:a725e8eab383 | 42 | cfg->conf_queue_size=10; |
AxedaCorp | 0:a725e8eab383 | 43 | cfg->first_run=AX_FALSE; |
AxedaCorp | 0:a725e8eab383 | 44 | cfg->debug_mode=AX_TRUE; |
AxedaCorp | 0:a725e8eab383 | 45 | cfg->print_mem=AX_FALSE; |
AxedaCorp | 0:a725e8eab383 | 46 | |
AxedaCorp | 0:a725e8eab383 | 47 | return cfg; |
AxedaCorp | 0:a725e8eab383 | 48 | } |
AxedaCorp | 0:a725e8eab383 | 49 | //Sends event item to platform |
AxedaCorp | 0:a725e8eab383 | 50 | int sendEvent(char* name, char* desc) |
AxedaCorp | 0:a725e8eab383 | 51 | { |
AxedaCorp | 0:a725e8eab383 | 52 | int res=0; |
AxedaCorp | 0:a725e8eab383 | 53 | |
AxedaCorp | 0:a725e8eab383 | 54 | printf("STATUS: Beginning event send\r\n"); |
AxedaCorp | 0:a725e8eab383 | 55 | ax_event *events[1]; |
AxedaCorp | 0:a725e8eab383 | 56 | events[0] = NULL; |
AxedaCorp | 0:a725e8eab383 | 57 | events[0]=(ax_event *)calloc(1,sizeof(ax_event)); |
AxedaCorp | 0:a725e8eab383 | 58 | res = ax_data_createEvent(events[0],name,desc,0,AX_NO_PRIORITY); |
AxedaCorp | 0:a725e8eab383 | 59 | printf("STATUS: Event item creation result: %d\r\n",res); |
AxedaCorp | 0:a725e8eab383 | 60 | |
AxedaCorp | 0:a725e8eab383 | 61 | ax_platform_sendEvents(&cfg.platform, &cfg.thisDevice,events,1); |
AxedaCorp | 0:a725e8eab383 | 62 | free(events[0]); |
AxedaCorp | 0:a725e8eab383 | 63 | |
AxedaCorp | 0:a725e8eab383 | 64 | return AX_OK; |
AxedaCorp | 0:a725e8eab383 | 65 | } |
AxedaCorp | 0:a725e8eab383 | 66 | //Sends location item to platform |
AxedaCorp | 0:a725e8eab383 | 67 | int sendLoc(double lat, double lon) |
AxedaCorp | 0:a725e8eab383 | 68 | { |
AxedaCorp | 0:a725e8eab383 | 69 | int res=0; |
AxedaCorp | 0:a725e8eab383 | 70 | |
AxedaCorp | 0:a725e8eab383 | 71 | printf("STATUS: Beginning location send\r\n"); |
AxedaCorp | 0:a725e8eab383 | 72 | ax_location *location[1]; |
AxedaCorp | 0:a725e8eab383 | 73 | location[0] = NULL; |
AxedaCorp | 0:a725e8eab383 | 74 | location[0]=(ax_location *)calloc(1,sizeof(ax_location)); |
AxedaCorp | 0:a725e8eab383 | 75 | res = ax_data_createLocation(location[0],lat,lon,0,0,AX_NO_PRIORITY); |
AxedaCorp | 0:a725e8eab383 | 76 | printf("STATUS: Location item creation result: %d\r\n",res); |
AxedaCorp | 0:a725e8eab383 | 77 | |
AxedaCorp | 0:a725e8eab383 | 78 | printf("STATUS: Sending data items to platform\r\n"); |
AxedaCorp | 0:a725e8eab383 | 79 | ax_platform_sendLocations(&cfg.platform, &cfg.thisDevice,location,1); |
AxedaCorp | 0:a725e8eab383 | 80 | free(location[0]); |
AxedaCorp | 0:a725e8eab383 | 81 | |
AxedaCorp | 0:a725e8eab383 | 82 | return AX_OK; |
AxedaCorp | 0:a725e8eab383 | 83 | } |
AxedaCorp | 0:a725e8eab383 | 84 | |
AxedaCorp | 0:a725e8eab383 | 85 | //Sends data item to platform |
AxedaCorp | 0:a725e8eab383 | 86 | int sendData(char *name, double value) |
AxedaCorp | 0:a725e8eab383 | 87 | { |
AxedaCorp | 0:a725e8eab383 | 88 | int res=0; |
AxedaCorp | 0:a725e8eab383 | 89 | |
AxedaCorp | 0:a725e8eab383 | 90 | printf("STATUS: Beginning data send\r\n"); |
AxedaCorp | 0:a725e8eab383 | 91 | ax_dataSet *data[1]; |
AxedaCorp | 0:a725e8eab383 | 92 | data[0] = NULL; |
AxedaCorp | 0:a725e8eab383 | 93 | data[0]=(ax_dataSet *)calloc(1,sizeof(ax_dataSet)); |
AxedaCorp | 0:a725e8eab383 | 94 | res = ax_data_createDataItem(data[0],name,AX_ANALOG,NULL,value,0,AX_NO_PRIORITY); |
AxedaCorp | 0:a725e8eab383 | 95 | printf("STATUS: Data item creation result: %d\r\n",res); |
AxedaCorp | 0:a725e8eab383 | 96 | |
AxedaCorp | 0:a725e8eab383 | 97 | printf("STATUS: Sending data items to platform\r\n"); |
AxedaCorp | 0:a725e8eab383 | 98 | ax_platform_sendData(&cfg.platform, &cfg.thisDevice,data,1); |
AxedaCorp | 0:a725e8eab383 | 99 | free(data[0]); |
AxedaCorp | 0:a725e8eab383 | 100 | |
AxedaCorp | 0:a725e8eab383 | 101 | return AX_OK; |
AxedaCorp | 0:a725e8eab383 | 102 | } |
AxedaCorp | 0:a725e8eab383 | 103 | |
AxedaCorp | 0:a725e8eab383 | 104 | //global variables for data items (server egress) |
AxedaCorp | 0:a725e8eab383 | 105 | int tank1=0; |
AxedaCorp | 0:a725e8eab383 | 106 | int tank2=0; |
AxedaCorp | 0:a725e8eab383 | 107 | |
AxedaCorp | 0:a725e8eab383 | 108 | int main(void) |
AxedaCorp | 0:a725e8eab383 | 109 | { |
AxedaCorp | 0:a725e8eab383 | 110 | ////////////////////////////////////INIT/////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 111 | bool abort=false; |
AxedaCorp | 0:a725e8eab383 | 112 | int ret; |
AxedaCorp | 0:a725e8eab383 | 113 | char buf[2048] = ""; |
AxedaCorp | 0:a725e8eab383 | 114 | |
AxedaCorp | 0:a725e8eab383 | 115 | // Create the GPS object |
AxedaCorp | 0:a725e8eab383 | 116 | #if 0 // use GPSI2C class |
AxedaCorp | 0:a725e8eab383 | 117 | GPSI2C gps; |
AxedaCorp | 0:a725e8eab383 | 118 | #else // or GPSSerial class |
AxedaCorp | 0:a725e8eab383 | 119 | GPSSerial gps; |
AxedaCorp | 0:a725e8eab383 | 120 | #endif |
AxedaCorp | 0:a725e8eab383 | 121 | |
AxedaCorp | 0:a725e8eab383 | 122 | //Create modem object |
AxedaCorp | 0:a725e8eab383 | 123 | MDMSerial mdm; |
AxedaCorp | 0:a725e8eab383 | 124 | // mdm.setDebug(4); //Uncomment this line to get modem debug messages |
AxedaCorp | 0:a725e8eab383 | 125 | |
AxedaCorp | 0:a725e8eab383 | 126 | //initialize the modem |
AxedaCorp | 0:a725e8eab383 | 127 | MDMParser::DevStatus devStatus = {}; |
AxedaCorp | 0:a725e8eab383 | 128 | MDMParser::NetStatus netStatus = {}; |
AxedaCorp | 0:a725e8eab383 | 129 | bool mdmOk = mdm.init(SIMPIN, &devStatus); |
AxedaCorp | 0:a725e8eab383 | 130 | mdm.dumpDevStatus(&devStatus); |
AxedaCorp | 0:a725e8eab383 | 131 | if (mdmOk) { |
AxedaCorp | 0:a725e8eab383 | 132 | //wait until we are connected |
AxedaCorp | 0:a725e8eab383 | 133 | mdmOk = mdm.registerNet(&netStatus); |
AxedaCorp | 0:a725e8eab383 | 134 | mdm.dumpNetStatus(&netStatus); |
AxedaCorp | 0:a725e8eab383 | 135 | } |
AxedaCorp | 0:a725e8eab383 | 136 | initConfig(&cfg); |
AxedaCorp | 0:a725e8eab383 | 137 | MDMParser::IP ip = mdm.join(APN,USERNAME,PASSWORD); |
AxedaCorp | 0:a725e8eab383 | 138 | if (ip == NOIP) { |
AxedaCorp | 0:a725e8eab383 | 139 | printf("ERROR - Modem unable to join internet connection\r\n"); |
AxedaCorp | 0:a725e8eab383 | 140 | abort=true; |
AxedaCorp | 0:a725e8eab383 | 141 | } |
AxedaCorp | 0:a725e8eab383 | 142 | if ((ret = ax_platform_register(&cfg.platform, &cfg.thisDevice))!=AX_OK) { |
AxedaCorp | 0:a725e8eab383 | 143 | printf("ERROR - Could not register with Axeda Platform\r\n"); |
AxedaCorp | 0:a725e8eab383 | 144 | } |
AxedaCorp | 0:a725e8eab383 | 145 | ////////////////////////////////////////////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 146 | printf("SMS and GPS Loop\r\n"); |
AxedaCorp | 0:a725e8eab383 | 147 | char link[128] = ""; |
AxedaCorp | 0:a725e8eab383 | 148 | char data[128] = ""; |
AxedaCorp | 0:a725e8eab383 | 149 | unsigned int i = 0xFFFFFFFF; |
AxedaCorp | 0:a725e8eab383 | 150 | const int wait = 2000; |
AxedaCorp | 0:a725e8eab383 | 151 | //DigitalOut led(LED1); |
AxedaCorp | 0:a725e8eab383 | 152 | while (!abort) { |
AxedaCorp | 0:a725e8eab383 | 153 | |
AxedaCorp | 0:a725e8eab383 | 154 | /////////////////////////////////POLL///////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 155 | /* if ((ret = ax_platform_ping(&cfg.platform, &cfg.thisDevice))!=AX_OK) { |
AxedaCorp | 0:a725e8eab383 | 156 | printf("ERROR - Lost connectivity with Axeda Platform\r\n"); |
AxedaCorp | 0:a725e8eab383 | 157 | }*/ |
AxedaCorp | 0:a725e8eab383 | 158 | sprintf(data, "Current data values: \r\nTank level 1: %d\r\nTank level 2: %d\r\n", tank1, tank2); |
AxedaCorp | 0:a725e8eab383 | 159 | //////////////////////////////////GPS///////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 160 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) { |
AxedaCorp | 0:a725e8eab383 | 161 | int len = LENGTH(ret); |
AxedaCorp | 0:a725e8eab383 | 162 | //printf("NMEA: %.*s\r\n", len-2, msg); |
AxedaCorp | 0:a725e8eab383 | 163 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { |
AxedaCorp | 0:a725e8eab383 | 164 | if (!strncmp("$GPGLL", buf, 6)) { |
AxedaCorp | 0:a725e8eab383 | 165 | double la = 0, lo = 0; |
AxedaCorp | 0:a725e8eab383 | 166 | char ch; |
AxedaCorp | 0:a725e8eab383 | 167 | if (gps.getNmeaAngle(1,buf,len,la) && |
AxedaCorp | 0:a725e8eab383 | 168 | gps.getNmeaAngle(3,buf,len,lo) && |
AxedaCorp | 0:a725e8eab383 | 169 | gps.getNmeaItem(6,buf,len,ch) && ch == 'A') { |
AxedaCorp | 0:a725e8eab383 | 170 | printf("STATUS: GPS Location: %.5f %.5f\r\n", la, lo); |
AxedaCorp | 0:a725e8eab383 | 171 | sprintf(link, "I am here!\n" |
AxedaCorp | 0:a725e8eab383 | 172 | "https://maps.google.com/?q=%.5f,%.5f", la, lo); |
AxedaCorp | 0:a725e8eab383 | 173 | sendLoc(la, lo); |
AxedaCorp | 0:a725e8eab383 | 174 | } |
AxedaCorp | 0:a725e8eab383 | 175 | } else if (!strncmp("$GPGGA", buf, 6)) { |
AxedaCorp | 0:a725e8eab383 | 176 | double alt = 0; |
AxedaCorp | 0:a725e8eab383 | 177 | if (gps.getNmeaItem(9,buf,len,alt)) // altitude msl [m] |
AxedaCorp | 0:a725e8eab383 | 178 | printf("STATUS: GPS Altitude: %.1f\r\n", alt); |
AxedaCorp | 0:a725e8eab383 | 179 | |
AxedaCorp | 0:a725e8eab383 | 180 | } else if (!strncmp("$GPVTG", buf, 6)) { |
AxedaCorp | 0:a725e8eab383 | 181 | double s = 0; |
AxedaCorp | 0:a725e8eab383 | 182 | if (gps.getNmeaItem(7,buf,len,s)) // speed [km/h] |
AxedaCorp | 0:a725e8eab383 | 183 | printf("STATUS: GPS Speed: %.1f\r\n", s); |
AxedaCorp | 0:a725e8eab383 | 184 | } |
AxedaCorp | 0:a725e8eab383 | 185 | } |
AxedaCorp | 0:a725e8eab383 | 186 | } |
AxedaCorp | 0:a725e8eab383 | 187 | |
AxedaCorp | 0:a725e8eab383 | 188 | /////////////////////////////SMS////////////////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 189 | int ix[8]; |
AxedaCorp | 0:a725e8eab383 | 190 | int n = mdm.smsList("REC UNREAD", ix, 8); |
AxedaCorp | 0:a725e8eab383 | 191 | if (8 < n) n = 8; |
AxedaCorp | 0:a725e8eab383 | 192 | while (0 < n--) { |
AxedaCorp | 0:a725e8eab383 | 193 | char num[32]; |
AxedaCorp | 0:a725e8eab383 | 194 | char msg[140]; |
AxedaCorp | 0:a725e8eab383 | 195 | printf("STATUS: Unread SMS at index %d\r\n", ix[n]); |
AxedaCorp | 0:a725e8eab383 | 196 | if (mdm.smsRead(ix[n], num, msg, sizeof(msg))) { |
AxedaCorp | 0:a725e8eab383 | 197 | printf("STATUS: Got SMS from \"%s\" with text \"%s\"\r\n", num, msg); |
AxedaCorp | 0:a725e8eab383 | 198 | sendEvent(num, msg); |
AxedaCorp | 0:a725e8eab383 | 199 | printf("STATUS: Delete SMS at index %d\r\n", ix[n]); |
AxedaCorp | 0:a725e8eab383 | 200 | mdm.smsDelete(ix[n]); |
AxedaCorp | 0:a725e8eab383 | 201 | // provide a reply |
AxedaCorp | 0:a725e8eab383 | 202 | const char* reply = "Howdy from u-blox and Axeda!"; |
AxedaCorp | 0:a725e8eab383 | 203 | if (strstr(msg, /*w*/"here are you")) |
AxedaCorp | 0:a725e8eab383 | 204 | reply = *link ? link : "Don't know - no GPS data yet"; // reply wil location link |
AxedaCorp | 0:a725e8eab383 | 205 | else if (strstr(msg, /*s*/"hutdown")) |
AxedaCorp | 0:a725e8eab383 | 206 | abort = true, reply = "Good night, and good luck"; |
AxedaCorp | 0:a725e8eab383 | 207 | else if (strstr(msg, /*f*/"ill1")) { |
AxedaCorp | 0:a725e8eab383 | 208 | reply = ("Increased fill - sending data to platform!"); |
AxedaCorp | 0:a725e8eab383 | 209 | if (tank1<9) { |
AxedaCorp | 0:a725e8eab383 | 210 | tank1+=2; |
AxedaCorp | 0:a725e8eab383 | 211 | } |
AxedaCorp | 0:a725e8eab383 | 212 | sendData("oil_level", tank1); |
AxedaCorp | 0:a725e8eab383 | 213 | } else if (strstr(msg, /*f*/"ill2")) { |
AxedaCorp | 0:a725e8eab383 | 214 | reply = ("Increased fill - sending data to platform!"); |
AxedaCorp | 0:a725e8eab383 | 215 | if (tank2<9) { |
AxedaCorp | 0:a725e8eab383 | 216 | tank2+=2; |
AxedaCorp | 0:a725e8eab383 | 217 | } |
AxedaCorp | 0:a725e8eab383 | 218 | sendData("oil_level2", tank2); |
AxedaCorp | 0:a725e8eab383 | 219 | } else if (strstr(msg, /*d*/"ipense1")) { |
AxedaCorp | 0:a725e8eab383 | 220 | reply = ("Dispensed from tank 1 - sending data to platform!"); |
AxedaCorp | 0:a725e8eab383 | 221 | if (tank1>=2) { |
AxedaCorp | 0:a725e8eab383 | 222 | tank1-=2; |
AxedaCorp | 0:a725e8eab383 | 223 | } |
AxedaCorp | 0:a725e8eab383 | 224 | sendData("oil_level", tank1); |
AxedaCorp | 0:a725e8eab383 | 225 | } else if (strstr(msg, /*d*/"ispense2")) { |
AxedaCorp | 0:a725e8eab383 | 226 | reply = ("Dipensed from tank 2 - sending data to platform!"); |
AxedaCorp | 0:a725e8eab383 | 227 | if (tank2>=2) { |
AxedaCorp | 0:a725e8eab383 | 228 | tank2-=2; |
AxedaCorp | 0:a725e8eab383 | 229 | } |
AxedaCorp | 0:a725e8eab383 | 230 | sendData("oil_level2", tank2); |
AxedaCorp | 0:a725e8eab383 | 231 | } |
AxedaCorp | 0:a725e8eab383 | 232 | |
AxedaCorp | 0:a725e8eab383 | 233 | printf("Send SMS reply \"%s\" to \"%s\"\r\n", reply, num); |
AxedaCorp | 0:a725e8eab383 | 234 | mdm.smsSend(num, reply); |
AxedaCorp | 0:a725e8eab383 | 235 | } |
AxedaCorp | 0:a725e8eab383 | 236 | } |
AxedaCorp | 0:a725e8eab383 | 237 | ///////////////////////////////NETCHECK//////////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 238 | if (mdmOk && (i++ == 5000/wait)) { |
AxedaCorp | 0:a725e8eab383 | 239 | i = 0; |
AxedaCorp | 0:a725e8eab383 | 240 | // check the network status |
AxedaCorp | 0:a725e8eab383 | 241 | if (mdm.checkNetStatus(&netStatus)) { |
AxedaCorp | 0:a725e8eab383 | 242 | mdm.dumpNetStatus(&netStatus, fprintf, stdout); |
AxedaCorp | 0:a725e8eab383 | 243 | } |
AxedaCorp | 0:a725e8eab383 | 244 | } |
AxedaCorp | 0:a725e8eab383 | 245 | wait_ms(wait); |
AxedaCorp | 0:a725e8eab383 | 246 | } |
AxedaCorp | 0:a725e8eab383 | 247 | //////////////////////////////CLOSE/////////////////////////////////////////////////////////////// |
AxedaCorp | 0:a725e8eab383 | 248 | gps.powerOff(); |
AxedaCorp | 0:a725e8eab383 | 249 | mdm.powerOff(); |
AxedaCorp | 0:a725e8eab383 | 250 | return 0; |
AxedaCorp | 0:a725e8eab383 | 251 | } |
AxedaCorp | 0:a725e8eab383 | 252 |