port to cellular
Dependencies: C027_Support LM75B M2XStreamClient jsonlite mbed mbed-rtos
Fork of m2x-demo-all by
main.cpp@1:c73a98da0e7a, 2014-05-13 (annotated)
- Committer:
- mazgch
- Date:
- Tue May 13 07:18:59 2014 +0000
- Revision:
- 1:c73a98da0e7a
- Parent:
- 0:38a7a8cae773
- Child:
- 2:83dd0b8109ca
inital port
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 |
jb8414 | 0:38a7a8cae773 | 6 | |
mazgch | 1:c73a98da0e7a | 7 | #include "C027.h" |
mazgch | 1:c73a98da0e7a | 8 | #include "MDM.h" |
mazgch | 1:c73a98da0e7a | 9 | |
mazgch | 1:c73a98da0e7a | 10 | //---------------------------------------------------------------------- |
mazgch | 1:c73a98da0e7a | 11 | // You may need to configure these parameters |
mazgch | 1:c73a98da0e7a | 12 | |
mazgch | 1:c73a98da0e7a | 13 | /** Set your secret SIM pin here "1234" |
mazgch | 1:c73a98da0e7a | 14 | */ |
mazgch | 1:c73a98da0e7a | 15 | #define SIMPIN NULL |
mazgch | 1:c73a98da0e7a | 16 | |
mazgch | 1:c73a98da0e7a | 17 | /** The APN of your network operator, sometimes it is "internet" |
mazgch | 1:c73a98da0e7a | 18 | check your contract with the network operator |
mazgch | 1:c73a98da0e7a | 19 | */ |
mazgch | 1:c73a98da0e7a | 20 | #define APN "gprs.swisscom.ch" |
mazgch | 1:c73a98da0e7a | 21 | |
mazgch | 1:c73a98da0e7a | 22 | /** Set the user name for your APN, or NULL if not needed |
mazgch | 1:c73a98da0e7a | 23 | */ |
mazgch | 1:c73a98da0e7a | 24 | #define USERNAME NULL |
mazgch | 1:c73a98da0e7a | 25 | |
mazgch | 1:c73a98da0e7a | 26 | /** Set the password for your APN, or NULL if not needed |
mazgch | 1:c73a98da0e7a | 27 | */ |
mazgch | 1:c73a98da0e7a | 28 | #define PASSWORD NULL |
mazgch | 1:c73a98da0e7a | 29 | |
mazgch | 1:c73a98da0e7a | 30 | C027 c027; |
mazgch | 1:c73a98da0e7a | 31 | |
jb8414 | 0:38a7a8cae773 | 32 | char feedId[] = "<feed id>"; // Feed you want to post to |
jb8414 | 0:38a7a8cae773 | 33 | char m2xKey[] = "<m2x api key>"; // Your M2X access key |
jb8414 | 0:38a7a8cae773 | 34 | char streamName[] = "<stream name>"; // Stream you want to post to |
jb8414 | 0:38a7a8cae773 | 35 | |
jb8414 | 0:38a7a8cae773 | 36 | char name[] = "<location name>"; // Name of current location of datasource |
jb8414 | 0:38a7a8cae773 | 37 | double latitude = 33.007872; |
jb8414 | 0:38a7a8cae773 | 38 | double longitude = -96.751614; // You can also read those values from a GPS |
jb8414 | 0:38a7a8cae773 | 39 | double elevation = 697.00; |
jb8414 | 0:38a7a8cae773 | 40 | |
jb8414 | 0:38a7a8cae773 | 41 | Client client; |
jb8414 | 0:38a7a8cae773 | 42 | M2XStreamClient m2xClient(&client, m2xKey); |
jb8414 | 0:38a7a8cae773 | 43 | |
mazgch | 1:c73a98da0e7a | 44 | LM75B tmp(SDA,SCL); // I2C Temperature Sensor |
jb8414 | 0:38a7a8cae773 | 45 | |
jb8414 | 0:38a7a8cae773 | 46 | void on_data_point_found(const char* at, const char* value, int index, void* context) { |
jb8414 | 0:38a7a8cae773 | 47 | printf("Found a data point, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 48 | printf("At: %s Value: %s\r\n", at, value); |
jb8414 | 0:38a7a8cae773 | 49 | } |
jb8414 | 0:38a7a8cae773 | 50 | |
jb8414 | 0:38a7a8cae773 | 51 | void on_location_found(const char* name, |
jb8414 | 0:38a7a8cae773 | 52 | double latitude, |
jb8414 | 0:38a7a8cae773 | 53 | double longitude, |
jb8414 | 0:38a7a8cae773 | 54 | double elevation, |
jb8414 | 0:38a7a8cae773 | 55 | const char* timestamp, |
jb8414 | 0:38a7a8cae773 | 56 | int index, |
jb8414 | 0:38a7a8cae773 | 57 | void* context) { |
jb8414 | 0:38a7a8cae773 | 58 | printf("Found a location, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 59 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
jb8414 | 0:38a7a8cae773 | 60 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
jb8414 | 0:38a7a8cae773 | 61 | } |
jb8414 | 0:38a7a8cae773 | 62 | |
jb8414 | 0:38a7a8cae773 | 63 | int main() { |
mazgch | 1:c73a98da0e7a | 64 | // turn on the supplies of the Modem and the GPS |
mazgch | 1:c73a98da0e7a | 65 | c027.mdmPower(true); |
mazgch | 1:c73a98da0e7a | 66 | printf("Modem Initialize\r\n"); |
mazgch | 1:c73a98da0e7a | 67 | MDMSerial mdm; |
mazgch | 1:c73a98da0e7a | 68 | if (!mdm.connect(SIMPIN, APN,USERNAME,PASSWORD, true)) |
mazgch | 1:c73a98da0e7a | 69 | return -1; |
jb8414 | 0:38a7a8cae773 | 70 | |
jb8414 | 0:38a7a8cae773 | 71 | char amb_temp[6]; |
jb8414 | 0:38a7a8cae773 | 72 | |
jb8414 | 0:38a7a8cae773 | 73 | while (true) { |
jb8414 | 0:38a7a8cae773 | 74 | |
jb8414 | 0:38a7a8cae773 | 75 | // read temp |
jb8414 | 0:38a7a8cae773 | 76 | sprintf(amb_temp, "%0.2f", tmp.read()); |
jb8414 | 0:38a7a8cae773 | 77 | |
jb8414 | 0:38a7a8cae773 | 78 | // post temperature |
jb8414 | 0:38a7a8cae773 | 79 | int response = m2xClient.post(feedId, streamName, amb_temp); |
jb8414 | 0:38a7a8cae773 | 80 | printf("Post response code: %d\r\n", response); |
jb8414 | 0:38a7a8cae773 | 81 | if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 82 | |
jb8414 | 0:38a7a8cae773 | 83 | // read temperature |
jb8414 | 0:38a7a8cae773 | 84 | response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL); |
jb8414 | 0:38a7a8cae773 | 85 | printf("Fetch response code: %d\r\n", response); |
jb8414 | 0:38a7a8cae773 | 86 | if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 87 | |
jb8414 | 0:38a7a8cae773 | 88 | // update location |
jb8414 | 0:38a7a8cae773 | 89 | response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation); |
jb8414 | 0:38a7a8cae773 | 90 | printf("updateLocation response code: %d\r\n", response); |
jb8414 | 0:38a7a8cae773 | 91 | if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 92 | |
jb8414 | 0:38a7a8cae773 | 93 | // read location |
jb8414 | 0:38a7a8cae773 | 94 | response = m2xClient.readLocation(feedId, on_location_found, NULL); |
jb8414 | 0:38a7a8cae773 | 95 | printf("readLocation response code: %d\r\n", response); |
jb8414 | 0:38a7a8cae773 | 96 | if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 97 | |
jb8414 | 0:38a7a8cae773 | 98 | // wait 60 secs and then loop |
jb8414 | 0:38a7a8cae773 | 99 | delay(60000); |
jb8414 | 0:38a7a8cae773 | 100 | } |
mazgch | 1:c73a98da0e7a | 101 | |
mazgch | 1:c73a98da0e7a | 102 | mdm.disconnect(); |
mazgch | 1:c73a98da0e7a | 103 | mdm.powerOff(); |
mazgch | 1:c73a98da0e7a | 104 | c027.mdmPower(false); |
mazgch | 1:c73a98da0e7a | 105 | printf("Done\n"); |
jb8414 | 0:38a7a8cae773 | 106 | } |