port to cellular
Dependencies: C027_Support M2XStreamClient jsonlite mbed-rtos mbed
Fork of Cellular_m2x-demo-all by
main.cpp@19:277dd9f95c9d, 2015-01-04 (annotated)
- Committer:
- mazgch
- Date:
- Sun Jan 04 02:17:42 2015 +0000
- Revision:
- 19:277dd9f95c9d
- Parent:
- 18:416ddef3e86f
make gps it threaded
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
jb8414 | 0:38a7a8cae773 | 1 | #include <jsonlite.h> |
jb8414 | 0:38a7a8cae773 | 2 | #include "M2XStreamClient.h" |
jb8414 | 0:38a7a8cae773 | 3 | |
jb8414 | 0:38a7a8cae773 | 4 | #include "mbed.h" |
mazgch | 19:277dd9f95c9d | 5 | #include "rtos.h" |
mazgch | 19:277dd9f95c9d | 6 | #define RTOS_H |
mazgch | 5:df776765d890 | 7 | #include "GPS.h" //GPS |
jb8414 | 0:38a7a8cae773 | 8 | |
mazgch | 3:dac7a2335ba5 | 9 | //------------------------------------------------------------------------------------ |
mazgch | 3:dac7a2335ba5 | 10 | // You need to configure these cellular modem / SIM parameters. |
mazgch | 3:dac7a2335ba5 | 11 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
mazgch | 3:dac7a2335ba5 | 12 | //------------------------------------------------------------------------------------ |
mazgch | 1:c73a98da0e7a | 13 | #include "MDM.h" |
mazgch | 3:dac7a2335ba5 | 14 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
mazgch | 1:c73a98da0e7a | 15 | #define SIMPIN NULL |
mazgch | 3:dac7a2335ba5 | 16 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
mazgch | 3:dac7a2335ba5 | 17 | contract with the network operator. You can also try to look-up your settings in |
mazgch | 3:dac7a2335ba5 | 18 | google: https://www.google.de/search?q=APN+list */ |
mazgch | 7:10d3cc37fe4c | 19 | #define APN NULL |
mazgch | 3:dac7a2335ba5 | 20 | //! Set the user name for your APN, or NULL if not needed |
mazgch | 1:c73a98da0e7a | 21 | #define USERNAME NULL |
mazgch | 3:dac7a2335ba5 | 22 | //! Set the password for your APN, or NULL if not needed |
mazgch | 1:c73a98da0e7a | 23 | #define PASSWORD NULL |
mazgch | 3:dac7a2335ba5 | 24 | //------------------------------------------------------------------------------------ |
mazgch | 1:c73a98da0e7a | 25 | |
mazgch | 5:df776765d890 | 26 | char feedId[] = "81b9fcc5a8585c55ae622488f50d8de0"; // Feed you want to post to |
mazgch | 5:df776765d890 | 27 | char m2xKey[] = "1e1133cd475954868602c0f7503d4f22"; // Your M2X access key |
jb8414 | 0:38a7a8cae773 | 28 | |
jb8414 | 0:38a7a8cae773 | 29 | char name[] = "<location name>"; // Name of current location of datasource |
jb8414 | 0:38a7a8cae773 | 30 | double latitude = 33.007872; |
jb8414 | 0:38a7a8cae773 | 31 | double longitude = -96.751614; // You can also read those values from a GPS |
jb8414 | 0:38a7a8cae773 | 32 | double elevation = 697.00; |
mazgch | 5:df776765d890 | 33 | bool location_valid = false; |
jb8414 | 0:38a7a8cae773 | 34 | |
jb8414 | 0:38a7a8cae773 | 35 | Client client; |
jb8414 | 0:38a7a8cae773 | 36 | M2XStreamClient m2xClient(&client, m2xKey); |
jb8414 | 0:38a7a8cae773 | 37 | |
mazgch | 16:5f9a8ea108bb | 38 | void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { |
jb8414 | 0:38a7a8cae773 | 39 | printf("Found a data point, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 40 | printf("At: %s Value: %s\r\n", at, value); |
jb8414 | 0:38a7a8cae773 | 41 | } |
jb8414 | 0:38a7a8cae773 | 42 | |
jb8414 | 0:38a7a8cae773 | 43 | void on_location_found(const char* name, |
jb8414 | 0:38a7a8cae773 | 44 | double latitude, |
jb8414 | 0:38a7a8cae773 | 45 | double longitude, |
jb8414 | 0:38a7a8cae773 | 46 | double elevation, |
jb8414 | 0:38a7a8cae773 | 47 | const char* timestamp, |
jb8414 | 0:38a7a8cae773 | 48 | int index, |
jb8414 | 0:38a7a8cae773 | 49 | void* context) { |
jb8414 | 0:38a7a8cae773 | 50 | printf("Found a location, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 51 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
jb8414 | 0:38a7a8cae773 | 52 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
jb8414 | 0:38a7a8cae773 | 53 | } |
jb8414 | 0:38a7a8cae773 | 54 | |
mazgch | 19:277dd9f95c9d | 55 | void gpsTask(void const* argument) |
mazgch | 19:277dd9f95c9d | 56 | { |
mazgch | 19:277dd9f95c9d | 57 | GPSI2C gps; |
mazgch | 19:277dd9f95c9d | 58 | char buf[256]; |
mazgch | 19:277dd9f95c9d | 59 | while (1) { |
mazgch | 19:277dd9f95c9d | 60 | int ret = gps.getMessage(buf, sizeof(buf)); |
mazgch | 19:277dd9f95c9d | 61 | if (ret > 0) { |
mazgch | 19:277dd9f95c9d | 62 | int len = LENGTH(ret); |
mazgch | 19:277dd9f95c9d | 63 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { |
mazgch | 19:277dd9f95c9d | 64 | // talker is $GA=Galileo $GB=Beidou $GL=Glonass $GN=Combined $GP=GPS |
mazgch | 19:277dd9f95c9d | 65 | if ((buf[0] == '$') || buf[1] == 'G') { |
mazgch | 19:277dd9f95c9d | 66 | #define _CHECK_TALKER(s) ((buf[3] == s[0]) && (buf[4] == s[1]) && (buf[5] == s[2])) |
mazgch | 19:277dd9f95c9d | 67 | if (_CHECK_TALKER("GGA")) { |
mazgch | 19:277dd9f95c9d | 68 | //printf("%.*sr\n", len, buf); |
mazgch | 19:277dd9f95c9d | 69 | char ch; |
mazgch | 19:277dd9f95c9d | 70 | if (gps.getNmeaAngle(2,buf,len,latitude) && |
mazgch | 19:277dd9f95c9d | 71 | gps.getNmeaAngle(4,buf,len,longitude) && |
mazgch | 19:277dd9f95c9d | 72 | gps.getNmeaItem(6,buf,len,ch) && |
mazgch | 19:277dd9f95c9d | 73 | gps.getNmeaItem(9,buf,len,elevation)) { |
mazgch | 19:277dd9f95c9d | 74 | printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); |
mazgch | 19:277dd9f95c9d | 75 | location_valid = ch == '1' || ch == '2' || ch == '6'; |
mazgch | 19:277dd9f95c9d | 76 | } |
mazgch | 19:277dd9f95c9d | 77 | } |
mazgch | 19:277dd9f95c9d | 78 | } |
mazgch | 19:277dd9f95c9d | 79 | } |
mazgch | 19:277dd9f95c9d | 80 | } else |
mazgch | 19:277dd9f95c9d | 81 | Thread::wait(100); |
mazgch | 19:277dd9f95c9d | 82 | } |
mazgch | 19:277dd9f95c9d | 83 | } |
mazgch | 19:277dd9f95c9d | 84 | |
jb8414 | 0:38a7a8cae773 | 85 | int main() { |
mazgch | 19:277dd9f95c9d | 86 | #if defined(TARGET_STM) |
mazgch | 17:cad6c0ab9811 | 87 | MDMSerial mdm(D8,D2); // use the serrial port on D2 / D8 |
mazgch | 17:cad6c0ab9811 | 88 | #else |
mazgch | 1:c73a98da0e7a | 89 | MDMSerial mdm; |
mazgch | 17:cad6c0ab9811 | 90 | #endif |
mazgch | 5:df776765d890 | 91 | GPSI2C gps; |
mazgch | 19:277dd9f95c9d | 92 | Thread task2(gpsTask, NULL, osPriorityNormal, 2048); |
mazgch | 19:277dd9f95c9d | 93 | |
mazgch | 9:08fdd1036e93 | 94 | //mdm.setDebug(4); // enable this for debugging issues |
mazgch | 3:dac7a2335ba5 | 95 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
mazgch | 1:c73a98da0e7a | 96 | return -1; |
mazgch | 5:df776765d890 | 97 | |
mazgch | 5:df776765d890 | 98 | char buf[256]; |
jb8414 | 0:38a7a8cae773 | 99 | |
mazgch | 5:df776765d890 | 100 | Timer tmr; |
mazgch | 5:df776765d890 | 101 | tmr.reset(); |
mazgch | 5:df776765d890 | 102 | tmr.start(); |
mazgch | 19:277dd9f95c9d | 103 | while (1) { |
mazgch | 10:ba926a8f1fe6 | 104 | if (tmr.read_ms() > 1000) { |
mazgch | 5:df776765d890 | 105 | tmr.reset(); |
mazgch | 5:df776765d890 | 106 | tmr.start(); |
mazgch | 8:7ca1b7a712b7 | 107 | int response; |
mazgch | 5:df776765d890 | 108 | |
mazgch | 8:7ca1b7a712b7 | 109 | MDMParser::NetStatus status; |
mazgch | 8:7ca1b7a712b7 | 110 | if (mdm.checkNetStatus(&status)) { |
mazgch | 8:7ca1b7a712b7 | 111 | sprintf(buf, "%d", status.rssi); |
mazgch | 16:5f9a8ea108bb | 112 | response = m2xClient.updateStreamValue(feedId, "rssi", buf); |
mazgch | 10:ba926a8f1fe6 | 113 | printf("Put response code: %d\r\n", response); |
mazgch | 8:7ca1b7a712b7 | 114 | if (response == -1) while (true) ; |
mazgch | 8:7ca1b7a712b7 | 115 | } |
mazgch | 16:5f9a8ea108bb | 116 | #define READING |
mazgch | 6:7a1e717a0d1e | 117 | #ifdef READING |
mazgch | 13:b04452198625 | 118 | // read signal strength |
mazgch | 16:5f9a8ea108bb | 119 | response = m2xClient.listStreamValues(feedId, "rssi", on_data_point_found, NULL); |
mazgch | 5:df776765d890 | 120 | printf("Fetch response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 121 | if (response == -1) while (true) ; |
mazgch | 6:7a1e717a0d1e | 122 | #endif |
mazgch | 5:df776765d890 | 123 | // update location |
mazgch | 5:df776765d890 | 124 | if (location_valid) { |
mazgch | 5:df776765d890 | 125 | response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation); |
mazgch | 5:df776765d890 | 126 | printf("updateLocation response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 127 | if (response == -1) while (true) ; |
mazgch | 5:df776765d890 | 128 | } |
mazgch | 6:7a1e717a0d1e | 129 | #ifdef READING |
mazgch | 5:df776765d890 | 130 | // read location |
mazgch | 5:df776765d890 | 131 | response = m2xClient.readLocation(feedId, on_location_found, NULL); |
mazgch | 5:df776765d890 | 132 | printf("readLocation response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 133 | if (response == -1) while (true) ; |
mazgch | 6:7a1e717a0d1e | 134 | #endif |
mazgch | 5:df776765d890 | 135 | } |
mazgch | 5:df776765d890 | 136 | else { |
mazgch | 19:277dd9f95c9d | 137 | Thread::wait(100); |
mazgch | 5:df776765d890 | 138 | } |
jb8414 | 0:38a7a8cae773 | 139 | } |
mazgch | 1:c73a98da0e7a | 140 | |
mazgch | 1:c73a98da0e7a | 141 | mdm.disconnect(); |
mazgch | 1:c73a98da0e7a | 142 | mdm.powerOff(); |
mazgch | 19:277dd9f95c9d | 143 | } |