port to cellular
Dependencies: C027_Support LM75B M2XStreamClient jsonlite mbed mbed-rtos
Fork of m2x-demo-all by
main.cpp@5:df776765d890, 2014-06-05 (annotated)
- Committer:
- mazgch
- Date:
- Thu Jun 05 09:13:43 2014 +0000
- Revision:
- 5:df776765d890
- Parent:
- 3:dac7a2335ba5
- Child:
- 6:7a1e717a0d1e
added in GPS support
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 | 1:c73a98da0e7a | 18 | #define APN "gprs.swisscom.ch" |
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 |
mazgch | 5:df776765d890 | 27 | char streamName[] = "amb_temp"; // Stream you want to post to |
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 | 1:c73a98da0e7a | 38 | LM75B tmp(SDA,SCL); // I2C Temperature Sensor |
jb8414 | 0:38a7a8cae773 | 39 | |
jb8414 | 0:38a7a8cae773 | 40 | void on_data_point_found(const char* at, const char* value, int index, void* context) { |
jb8414 | 0:38a7a8cae773 | 41 | printf("Found a data point, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 42 | printf("At: %s Value: %s\r\n", at, value); |
jb8414 | 0:38a7a8cae773 | 43 | } |
jb8414 | 0:38a7a8cae773 | 44 | |
jb8414 | 0:38a7a8cae773 | 45 | void on_location_found(const char* name, |
jb8414 | 0:38a7a8cae773 | 46 | double latitude, |
jb8414 | 0:38a7a8cae773 | 47 | double longitude, |
jb8414 | 0:38a7a8cae773 | 48 | double elevation, |
jb8414 | 0:38a7a8cae773 | 49 | const char* timestamp, |
jb8414 | 0:38a7a8cae773 | 50 | int index, |
jb8414 | 0:38a7a8cae773 | 51 | void* context) { |
jb8414 | 0:38a7a8cae773 | 52 | printf("Found a location, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 53 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
jb8414 | 0:38a7a8cae773 | 54 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
jb8414 | 0:38a7a8cae773 | 55 | } |
jb8414 | 0:38a7a8cae773 | 56 | |
jb8414 | 0:38a7a8cae773 | 57 | int main() { |
mazgch | 1:c73a98da0e7a | 58 | MDMSerial mdm; |
mazgch | 5:df776765d890 | 59 | GPSI2C gps; |
mazgch | 3:dac7a2335ba5 | 60 | //mdm.setDebug(4); // enable this for debugging issues |
mazgch | 3:dac7a2335ba5 | 61 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD)) |
mazgch | 1:c73a98da0e7a | 62 | return -1; |
mazgch | 5:df776765d890 | 63 | |
mazgch | 5:df776765d890 | 64 | char buf[256]; |
jb8414 | 0:38a7a8cae773 | 65 | |
mazgch | 5:df776765d890 | 66 | Timer tmr; |
mazgch | 5:df776765d890 | 67 | tmr.reset(); |
mazgch | 5:df776765d890 | 68 | tmr.start(); |
mazgch | 5:df776765d890 | 69 | while (true) { |
mazgch | 5:df776765d890 | 70 | int ret; |
mazgch | 5:df776765d890 | 71 | // extract the location information from the GPS NMEA data |
mazgch | 5:df776765d890 | 72 | while ((ret = gps.getMessage(buf, sizeof(buf))) > 0) { |
mazgch | 5:df776765d890 | 73 | int len = LENGTH(ret); |
mazgch | 5:df776765d890 | 74 | if ((PROTOCOL(ret) == GPSParser::NMEA) && (len > 6)) { |
mazgch | 5:df776765d890 | 75 | if (!strncmp("$GPGGA", buf, 6)) { |
mazgch | 5:df776765d890 | 76 | char ch; |
mazgch | 5:df776765d890 | 77 | if (gps.getNmeaAngle(2,buf,len,latitude) && |
mazgch | 5:df776765d890 | 78 | gps.getNmeaAngle(4,buf,len,longitude) && |
mazgch | 5:df776765d890 | 79 | gps.getNmeaItem(6,buf,len,ch) && |
mazgch | 5:df776765d890 | 80 | gps.getNmeaItem(9,buf,len,elevation)) { |
mazgch | 5:df776765d890 | 81 | printf("GPS Location: %.5f %.5f %.1f %c\r\n", latitude, longitude, elevation, ch); |
mazgch | 5:df776765d890 | 82 | location_valid = ch == '1' || ch == '2' || ch == '6'; |
mazgch | 5:df776765d890 | 83 | } |
mazgch | 5:df776765d890 | 84 | } |
mazgch | 5:df776765d890 | 85 | } |
mazgch | 5:df776765d890 | 86 | } |
jb8414 | 0:38a7a8cae773 | 87 | |
mazgch | 5:df776765d890 | 88 | if (tmr.read_ms() > 60000) { |
mazgch | 5:df776765d890 | 89 | tmr.reset(); |
mazgch | 5:df776765d890 | 90 | tmr.start(); |
mazgch | 5:df776765d890 | 91 | |
mazgch | 5:df776765d890 | 92 | // read temp |
mazgch | 5:df776765d890 | 93 | sprintf(buf, "%0.2f", tmp.read()); |
mazgch | 5:df776765d890 | 94 | |
mazgch | 5:df776765d890 | 95 | // post temperature |
mazgch | 5:df776765d890 | 96 | int response = m2xClient.post(feedId, streamName, buf); |
mazgch | 5:df776765d890 | 97 | printf("Post response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 98 | if (response == -1) while (true) ; |
mazgch | 5:df776765d890 | 99 | |
mazgch | 5:df776765d890 | 100 | // read temperature |
mazgch | 5:df776765d890 | 101 | response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL); |
mazgch | 5:df776765d890 | 102 | printf("Fetch response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 103 | if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 104 | |
mazgch | 5:df776765d890 | 105 | // update location |
mazgch | 5:df776765d890 | 106 | if (location_valid) { |
mazgch | 5:df776765d890 | 107 | response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation); |
mazgch | 5:df776765d890 | 108 | printf("updateLocation response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 109 | if (response == -1) while (true) ; |
mazgch | 5:df776765d890 | 110 | } |
mazgch | 5:df776765d890 | 111 | |
mazgch | 5:df776765d890 | 112 | // read location |
mazgch | 5:df776765d890 | 113 | response = m2xClient.readLocation(feedId, on_location_found, NULL); |
mazgch | 5:df776765d890 | 114 | printf("readLocation response code: %d\r\n", response); |
mazgch | 5:df776765d890 | 115 | if (response == -1) while (true) ; |
mazgch | 5:df776765d890 | 116 | } |
mazgch | 5:df776765d890 | 117 | else { |
mazgch | 5:df776765d890 | 118 | delay(100); |
mazgch | 5:df776765d890 | 119 | } |
jb8414 | 0:38a7a8cae773 | 120 | } |
mazgch | 1:c73a98da0e7a | 121 | |
mazgch | 1:c73a98da0e7a | 122 | mdm.disconnect(); |
mazgch | 1:c73a98da0e7a | 123 | mdm.powerOff(); |
jb8414 | 0:38a7a8cae773 | 124 | } |