https://github.com/ryantm/attcarhomehackathon
Dependencies: EthernetInterface LM75B M2XStreamClient jsonlite mbed-rtos mbed
Fork of m2x-demo-all by
main.cpp@1:225ed64c9353, 2014-09-07 (annotated)
- Committer:
- buf006
- Date:
- Sun Sep 07 20:33:58 2014 +0000
- Revision:
- 1:225ed64c9353
- Parent:
- 0:38a7a8cae773
- Child:
- 2:3bec0454c754
add AT&T car and home hackathon home module mbed code
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 "EthernetInterface.h" |
jb8414 | 0:38a7a8cae773 | 6 | #include "LM75B.h" //I2C Temperature Sensor |
jb8414 | 0:38a7a8cae773 | 7 | |
buf006 | 1:225ed64c9353 | 8 | |
buf006 | 1:225ed64c9353 | 9 | char feedId[] = "c6eabf437b8c69efbb4e4a8d5c60c04d"; // Feed you want to post to |
buf006 | 1:225ed64c9353 | 10 | char m2xKey[] = "10bc8a4dc4a37c5dc549b41ffaa6d6c1"; // Your M2X access key |
buf006 | 1:225ed64c9353 | 11 | char streamName[] = "danger_bit"; // Stream you want to post to |
buf006 | 1:225ed64c9353 | 12 | |
buf006 | 1:225ed64c9353 | 13 | DigitalOut garage(PTB9); |
buf006 | 1:225ed64c9353 | 14 | |
buf006 | 1:225ed64c9353 | 15 | |
buf006 | 1:225ed64c9353 | 16 | /* |
buf006 | 1:225ed64c9353 | 17 | char feeidId[] = "08a3bceeee7e7bfa32340aadd282ea23"; // Feed you want to post to |
buf006 | 1:225ed64c9353 | 18 | char m2xKey[] = "41e5905679b7a6fa668303e00da65826"; // Your M2X access key |
buf006 | 1:225ed64c9353 | 19 | char streamName[] = "garagecommand"; // Stream you want to post to |
buf006 | 1:225ed64c9353 | 20 | */ |
buf006 | 1:225ed64c9353 | 21 | int danger = 0; |
jb8414 | 0:38a7a8cae773 | 22 | |
jb8414 | 0:38a7a8cae773 | 23 | char name[] = "<location name>"; // Name of current location of datasource |
jb8414 | 0:38a7a8cae773 | 24 | double latitude = 33.007872; |
jb8414 | 0:38a7a8cae773 | 25 | double longitude = -96.751614; // You can also read those values from a GPS |
jb8414 | 0:38a7a8cae773 | 26 | double elevation = 697.00; |
jb8414 | 0:38a7a8cae773 | 27 | |
jb8414 | 0:38a7a8cae773 | 28 | Client client; |
jb8414 | 0:38a7a8cae773 | 29 | M2XStreamClient m2xClient(&client, m2xKey); |
jb8414 | 0:38a7a8cae773 | 30 | |
jb8414 | 0:38a7a8cae773 | 31 | EthernetInterface eth; |
buf006 | 1:225ed64c9353 | 32 | //LM75B tmp(p28,p27); // I2C Temperature Sensor |
jb8414 | 0:38a7a8cae773 | 33 | |
jb8414 | 0:38a7a8cae773 | 34 | void on_data_point_found(const char* at, const char* value, int index, void* context) { |
jb8414 | 0:38a7a8cae773 | 35 | printf("Found a data point, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 36 | printf("At: %s Value: %s\r\n", at, value); |
buf006 | 1:225ed64c9353 | 37 | |
buf006 | 1:225ed64c9353 | 38 | if(index == 0) |
buf006 | 1:225ed64c9353 | 39 | { |
buf006 | 1:225ed64c9353 | 40 | danger = atoi(value); |
buf006 | 1:225ed64c9353 | 41 | |
buf006 | 1:225ed64c9353 | 42 | if (danger == 1) |
buf006 | 1:225ed64c9353 | 43 | { |
buf006 | 1:225ed64c9353 | 44 | printf("Carbonmonoxide levels are dangerous. \n"); |
buf006 | 1:225ed64c9353 | 45 | garage = 0; |
buf006 | 1:225ed64c9353 | 46 | |
buf006 | 1:225ed64c9353 | 47 | } |
buf006 | 1:225ed64c9353 | 48 | |
buf006 | 1:225ed64c9353 | 49 | } |
jb8414 | 0:38a7a8cae773 | 50 | } |
jb8414 | 0:38a7a8cae773 | 51 | |
jb8414 | 0:38a7a8cae773 | 52 | void on_location_found(const char* name, |
jb8414 | 0:38a7a8cae773 | 53 | double latitude, |
jb8414 | 0:38a7a8cae773 | 54 | double longitude, |
jb8414 | 0:38a7a8cae773 | 55 | double elevation, |
jb8414 | 0:38a7a8cae773 | 56 | const char* timestamp, |
jb8414 | 0:38a7a8cae773 | 57 | int index, |
jb8414 | 0:38a7a8cae773 | 58 | void* context) { |
jb8414 | 0:38a7a8cae773 | 59 | printf("Found a location, index: %d\r\n", index); |
jb8414 | 0:38a7a8cae773 | 60 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
jb8414 | 0:38a7a8cae773 | 61 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
jb8414 | 0:38a7a8cae773 | 62 | } |
jb8414 | 0:38a7a8cae773 | 63 | |
jb8414 | 0:38a7a8cae773 | 64 | int main() { |
jb8414 | 0:38a7a8cae773 | 65 | eth.init(); |
jb8414 | 0:38a7a8cae773 | 66 | eth.connect(); |
jb8414 | 0:38a7a8cae773 | 67 | printf("IP Address: %s\r\n", eth.getIPAddress()); |
jb8414 | 0:38a7a8cae773 | 68 | |
jb8414 | 0:38a7a8cae773 | 69 | char amb_temp[6]; |
jb8414 | 0:38a7a8cae773 | 70 | |
buf006 | 1:225ed64c9353 | 71 | garage = 1; |
buf006 | 1:225ed64c9353 | 72 | |
jb8414 | 0:38a7a8cae773 | 73 | while (true) { |
jb8414 | 0:38a7a8cae773 | 74 | |
jb8414 | 0:38a7a8cae773 | 75 | // read temp |
buf006 | 1:225ed64c9353 | 76 | //sprintf(amb_temp, "%0.2f", tmp.read()); |
jb8414 | 0:38a7a8cae773 | 77 | |
jb8414 | 0:38a7a8cae773 | 78 | // post temperature |
buf006 | 1:225ed64c9353 | 79 | //int response = m2xClient.post(feedId, streamName, amb_temp); |
buf006 | 1:225ed64c9353 | 80 | //printf("Post response code: %d\r\n", response); |
buf006 | 1:225ed64c9353 | 81 | //if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 82 | |
jb8414 | 0:38a7a8cae773 | 83 | // read temperature |
buf006 | 1:225ed64c9353 | 84 | int response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL); |
jb8414 | 0:38a7a8cae773 | 85 | printf("Fetch response code: %d\r\n", response); |
buf006 | 1:225ed64c9353 | 86 | //if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 87 | |
jb8414 | 0:38a7a8cae773 | 88 | // update location |
buf006 | 1:225ed64c9353 | 89 | //response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation); |
buf006 | 1:225ed64c9353 | 90 | //printf("updateLocation response code: %d\r\n", response); |
buf006 | 1:225ed64c9353 | 91 | //if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 92 | |
jb8414 | 0:38a7a8cae773 | 93 | // read location |
buf006 | 1:225ed64c9353 | 94 | //response = m2xClient.readLocation(feedId, on_location_found, NULL); |
buf006 | 1:225ed64c9353 | 95 | //printf("readLocation response code: %d\r\n", response); |
buf006 | 1:225ed64c9353 | 96 | //if (response == -1) while (true) ; |
jb8414 | 0:38a7a8cae773 | 97 | |
buf006 | 1:225ed64c9353 | 98 | // wait 5 secs and then loop |
buf006 | 1:225ed64c9353 | 99 | delay(5000); |
jb8414 | 0:38a7a8cae773 | 100 | } |
jb8414 | 0:38a7a8cae773 | 101 | } |