port to cellular
Dependencies: C027_Support LM75B M2XStreamClient jsonlite mbed mbed-rtos
Fork of m2x-demo-all by
main.cpp@10:ba926a8f1fe6, 2014-09-13 (annotated)
- Committer:
- mazgch
- Date:
- Sat Sep 13 16:06:06 2014 +0000
- Revision:
- 10:ba926a8f1fe6
- Parent:
- 9:08fdd1036e93
use improved M2X client
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" |
jb8414 | 0:38a7a8cae773 | 5 | #include "LM75B.h" //I2C Temperature Sensor |
mazgch | 5:df776765d890 | 6 | #include "GPS.h" //GPS |
jb8414 | 0:38a7a8cae773 | 7 | |
mazgch | 3:dac7a2335ba5 | 8 | //------------------------------------------------------------------------------------ |
mazgch | 3:dac7a2335ba5 | 9 | // You need to configure these cellular modem / SIM parameters. |
mazgch | 3:dac7a2335ba5 | 10 | // These parameters are ignored for LISA-C200 variants and can be left NULL. |
mazgch | 3:dac7a2335ba5 | 11 | //------------------------------------------------------------------------------------ |
mazgch | 1:c73a98da0e7a | 12 | #include "MDM.h" |
mazgch | 3:dac7a2335ba5 | 13 | //! Set your secret SIM pin here (e.g. "1234"). Check your SIM manual. |
mazgch | 1:c73a98da0e7a | 14 | #define SIMPIN NULL |
mazgch | 3:dac7a2335ba5 | 15 | /*! The APN of your network operator SIM, sometimes it is "internet" check your |
mazgch | 3:dac7a2335ba5 | 16 | contract with the network operator. You can also try to look-up your settings in |
mazgch | 3:dac7a2335ba5 | 17 | google: https://www.google.de/search?q=APN+list */ |
mazgch | 7:10d3cc37fe4c | 18 | #define APN NULL |
mazgch | 3:dac7a2335ba5 | 19 | //! Set the user name for your APN, or NULL if not needed |
mazgch | 1:c73a98da0e7a | 20 | #define USERNAME NULL |
mazgch | 3:dac7a2335ba5 | 21 | //! Set the password for your APN, or NULL if not needed |
mazgch | 1:c73a98da0e7a | 22 | #define PASSWORD NULL |
mazgch | 3:dac7a2335ba5 | 23 | //------------------------------------------------------------------------------------ |
mazgch | 1:c73a98da0e7a | 24 | |
mazgch | 5:df776765d890 | 25 | char feedId[] = "81b9fcc5a8585c55ae622488f50d8de0"; // Feed you want to post to |
mazgch | 5:df776765d890 | 26 | char m2xKey[] = "1e1133cd475954868602c0f7503d4f22"; // Your M2X access key |
jb8414 | 0:38a7a8cae773 | 27 | |
jb8414 | 0:38a7a8cae773 | 28 | char name[] = "<location name>"; // Name of current location of datasource |
jb8414 | 0:38a7a8cae773 | 29 | double latitude = 33.007872; |
jb8414 | 0:38a7a8cae773 | 30 | double longitude = -96.751614; // You can also read those values from a GPS |
jb8414 | 0:38a7a8cae773 | 31 | double elevation = 697.00; |
mazgch | 5:df776765d890 | 32 | bool location_valid = false; |
jb8414 | 0:38a7a8cae773 | 33 | |
jb8414 | 0:38a7a8cae773 | 34 | Client client; |
jb8414 | 0:38a7a8cae773 | 35 | M2XStreamClient m2xClient(&client, m2xKey); |
jb8414 | 0:38a7a8cae773 | 36 | |
mazgch | 1:c73a98da0e7a | 37 | LM75B tmp(SDA,SCL); // I2C Temperature Sensor |
jb8414 | 0:38a7a8cae773 | 38 | |
jb8414 | 0:38a7a8cae773 | 39 | void on_data_point_found(const char* at, const char* value, int index, void* context) { |
jb8414 | 0:38a7a8cae773 | 40 | printf("Found a data point, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 41 | printf("At: %s Value: %s\r\n", at, value); |
jb8414 | 0:38a7a8cae773 | 42 | } |
jb8414 | 0:38a7a8cae773 | 43 | |
jb8414 | 0:38a7a8cae773 | 44 | void on_location_found(const char* name, |
jb8414 | 0:38a7a8cae773 | 45 | double latitude, |
jb8414 | 0:38a7a8cae773 | 46 | double longitude, |
jb8414 | 0:38a7a8cae773 | 47 | double elevation, |
jb8414 | 0:38a7a8cae773 | 48 | const char* timestamp, |
jb8414 | 0:38a7a8cae773 | 49 | int index, |
jb8414 | 0:38a7a8cae773 | 50 | void* context) { |
jb8414 | 0:38a7a8cae773 | 51 | printf("Found a location, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 52 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
jb8414 | 0:38a7a8cae773 | 53 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
jb8414 | 0:38a7a8cae773 | 54 | } |
jb8414 | 0:38a7a8cae773 | 55 | |
jb8414 | 0:38a7a8cae773 | 56 | int main() { |
mazgch | 1:c73a98da0e7a | 57 | MDMSerial mdm; |
mazgch | 5:df776765d890 | 58 | GPSI2C gps; |
mazgch | 9:08fdd1036e93 | 59 | //mdm.setDebug(4); // enable this for debugging issues |
mazgch | 3:dac7a2335ba5 | 60 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
mazgch | 1:c73a98da0e7a | 61 | return -1; |
mazgch | 5:df776765d890 | 62 | |
mazgch | 5:df776765d890 | 63 | char buf[256]; |
jb8414 | 0:38a7a8cae773 | 64 | |
mazgch | 5:df776765d890 | 65 | Timer tmr; |
mazgch | 5:df776765d890 | 66 | tmr.reset(); |
mazgch | 5:df776765d890 | 67 | tmr.start(); |
mazgch | 5:df776765d890 | 68 | while (true) { |
mazgch | 5:df776765d890 | 69 | int ret; |
mazgch | 5:df776765d890 | 70 | // extract the location information from the GPS NMEA data |
mazgch | 5:df776765d890 | 71 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) { |
mazgch | 5:df776765d890 | 72 | int len = LENGTH(ret); |
mazgch | 5:df776765d890 | 73 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { |
mazgch | 5:df776765d890 | 74 | if (!strncmp("$GPGGA", buf, 6)) { |
mazgch | 5:df776765d890 | 75 | char ch; |
mazgch | 5:df776765d890 | 76 | if (gps.getNmeaAngle(2,buf,len,latitude) && |
mazgch | 5:df776765d890 | 77 | gps.getNmeaAngle(4,buf,len,longitude) && |
mazgch | 5:df776765d890 | 78 | gps.getNmeaItem(6,buf,len,ch) && |
mazgch | 5:df776765d890 | 79 | gps.getNmeaItem(9,buf,len,elevation)) { |
mazgch | 5:df776765d890 | 80 | printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); |
mazgch | 5:df776765d890 | 81 | location_valid = ch == '1' || ch == '2' || ch == '6'; |
mazgch | 5:df776765d890 | 82 | } |
mazgch | 5:df776765d890 | 83 | } |
mazgch | 5:df776765d890 | 84 | } |
mazgch | 5:df776765d890 | 85 | } |
jb8414 | 0:38a7a8cae773 | 86 | |
mazgch | 10:ba926a8f1fe6 | 87 | if (tmr.read_ms() > 1000) { |
mazgch | 5:df776765d890 | 88 | tmr.reset(); |
mazgch | 5:df776765d890 | 89 | tmr.start(); |
mazgch | 8:7ca1b7a712b7 | 90 | int response; |
mazgch | 5:df776765d890 | 91 | |
mazgch | 8:7ca1b7a712b7 | 92 | MDMParser::NetStatus status; |
mazgch | 8:7ca1b7a712b7 | 93 | if (mdm.checkNetStatus(&status)) { |
mazgch | 8:7ca1b7a712b7 | 94 | sprintf(buf, "%d", status.rssi); |
mazgch | 10:ba926a8f1fe6 | 95 | response = m2xClient.put(feedId, "rssi", buf); |
mazgch | 10:ba926a8f1fe6 | 96 | printf("Put response code: %d\r\n", response); |
mazgch | 8:7ca1b7a712b7 | 97 | if (response == -1) while (true) ; |
mazgch | 8:7ca1b7a712b7 | 98 | } |
mazgch | 6:7a1e717a0d1e | 99 | //#define READING |
mazgch | 6:7a1e717a0d1e | 100 | #ifdef READING |
mazgch | 5:df776765d890 | 101 | // read temperature |
mazgch | 8:7ca1b7a712b7 | 102 | response = m2xClient.fetchValues(feedId, "rssi", on_data_point_found, NULL); |
mazgch | 5:df776765d890 | 103 | printf("Fetch response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 104 | if (response == -1) while (true) ; |
mazgch | 6:7a1e717a0d1e | 105 | #endif |
mazgch | 5:df776765d890 | 106 | // update location |
mazgch | 5:df776765d890 | 107 | if (location_valid) { |
mazgch | 5:df776765d890 | 108 | response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation); |
mazgch | 5:df776765d890 | 109 | printf("updateLocation response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 110 | if (response == -1) while (true) ; |
mazgch | 5:df776765d890 | 111 | } |
mazgch | 6:7a1e717a0d1e | 112 | #ifdef READING |
mazgch | 5:df776765d890 | 113 | // read location |
mazgch | 5:df776765d890 | 114 | response = m2xClient.readLocation(feedId, on_location_found, NULL); |
mazgch | 5:df776765d890 | 115 | printf("readLocation response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 116 | if (response == -1) while (true) ; |
mazgch | 6:7a1e717a0d1e | 117 | #endif |
mazgch | 5:df776765d890 | 118 | } |
mazgch | 5:df776765d890 | 119 | else { |
mazgch | 5:df776765d890 | 120 | delay(100); |
mazgch | 5:df776765d890 | 121 | } |
jb8414 | 0:38a7a8cae773 | 122 | } |
mazgch | 1:c73a98da0e7a | 123 | |
mazgch | 1:c73a98da0e7a | 124 | mdm.disconnect(); |
mazgch | 1:c73a98da0e7a | 125 | mdm.powerOff(); |
jb8414 | 0:38a7a8cae773 | 126 | } |