https://github.com/ryantm/attcarhomehackathon
Dependencies: EthernetInterface LM75B M2XStreamClient jsonlite mbed-rtos mbed
Fork of m2x-demo-all by
main.cpp
00001 #include <jsonlite.h> 00002 #include "M2XStreamClient.h" 00003 00004 #include "mbed.h" 00005 #include "EthernetInterface.h" 00006 #include "LM75B.h" //I2C Temperature Sensor 00007 00008 00009 char feedId[] = "c6eabf437b8c69efbb4e4a8d5c60c04d"; // Feed you want to post to 00010 char m2xKey[] = "10bc8a4dc4a37c5dc549b41ffaa6d6c1"; // Your M2X access key 00011 char streamName[] = "danger_bit"; // Stream you want to post to 00012 00013 DigitalOut garage(PTB9); 00014 DigitalOut led(LED_RED); 00015 DigitalOut bled(LED_BLUE); 00016 00017 /* 00018 char feeidId[] = "08a3bceeee7e7bfa32340aadd282ea23"; // Feed you want to post to 00019 char m2xKey[] = "41e5905679b7a6fa668303e00da65826"; // Your M2X access key 00020 char streamName[] = "garagecommand"; // Stream you want to post to 00021 */ 00022 int danger = 0; 00023 00024 Client client; 00025 M2XStreamClient m2xClient(&client, m2xKey); 00026 00027 EthernetInterface eth; 00028 00029 void on_data_point_found(const char* at, const char* value, int index, void* context) { 00030 printf("Found a data point, index: %d\r\n", index); 00031 printf("At: %s Value: %s\r\n", at, value); 00032 00033 if(index == 0) 00034 { 00035 danger = atoi(value); 00036 00037 if (danger == 1) 00038 { 00039 printf("Carbonmonoxide levels are dangerous. \n"); 00040 garage = 0; 00041 00042 } 00043 00044 if (danger == 0) 00045 { 00046 garage = 1; 00047 } 00048 00049 } 00050 } 00051 00052 void on_location_found(const char* name, 00053 double latitude, 00054 double longitude, 00055 double elevation, 00056 const char* timestamp, 00057 int index, 00058 void* context) { 00059 printf("Found a location, index: %d\r\n", index); 00060 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); 00061 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); 00062 } 00063 00064 int main() { 00065 garage = 1; 00066 led = 0; 00067 00068 eth.init(); 00069 eth.connect(); 00070 printf("IP Address: %s\r\n", eth.getIPAddress()); 00071 00072 char amb_temp[6]; 00073 00074 00075 while (true) { 00076 00077 // read temperature 00078 int response = m2xClient.fetchValues(feedId, streamName, on_data_point_found, NULL, NULL, NULL, "1"); 00079 printf("Fetch response code: %d\r\n", response); 00080 if (response == -1) 00081 bled = 0; 00082 else 00083 bled = 1; 00084 00085 led = !led; 00086 00087 // wait 5 secs and then loop 00088 delay(2000); 00089 } 00090 }
Generated on Thu Aug 4 2022 06:59:27 by 1.7.2