Car Bon car module
Dependencies: C027_Support HTTPClient-basicAuth M2XStreamClient jsonlite mbed
main.cpp@0:8ce138308888, 2014-09-07 (annotated)
- Committer:
- kwchang2
- Date:
- Sun Sep 07 20:33:03 2014 +0000
- Revision:
- 0:8ce138308888
working car module code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
kwchang2 | 0:8ce138308888 | 1 | #include "mbed.h" |
kwchang2 | 0:8ce138308888 | 2 | #include "MDM.h" |
kwchang2 | 0:8ce138308888 | 3 | #include "HTTPClient.h" |
kwchang2 | 0:8ce138308888 | 4 | #include "M2XStreamClient.h" |
kwchang2 | 0:8ce138308888 | 5 | |
kwchang2 | 0:8ce138308888 | 6 | // M2X stuff |
kwchang2 | 0:8ce138308888 | 7 | char feedId[] = "c6eabf437b8c69efbb4e4a8d5c60c04d"; // Feed you want to post to |
kwchang2 | 0:8ce138308888 | 8 | char m2xKey[] = "10bc8a4dc4a37c5dc549b41ffaa6d6c1"; // Your M2X access key |
kwchang2 | 0:8ce138308888 | 9 | char streamName[] = "danger_bit"; // Stream you want to post to |
kwchang2 | 0:8ce138308888 | 10 | |
kwchang2 | 0:8ce138308888 | 11 | // Connected Car stuff |
kwchang2 | 0:8ce138308888 | 12 | char ccUser[] = "provider"; |
kwchang2 | 0:8ce138308888 | 13 | char ccPass[] = "1234"; |
kwchang2 | 0:8ce138308888 | 14 | char ccOnUrl[] = "http://toy.hack.att.io:3000/remoteservices/v1/vehicle/engineOn/1234567890abcd"; |
kwchang2 | 0:8ce138308888 | 15 | char ccOffUrl[] = "http://toy.hack.att.io:3000/remoteservices/v1/vehicle/engineOff/1234567890abcd"; |
kwchang2 | 0:8ce138308888 | 16 | |
kwchang2 | 0:8ce138308888 | 17 | Ticker sensortick; |
kwchang2 | 0:8ce138308888 | 18 | |
kwchang2 | 0:8ce138308888 | 19 | DigitalOut heat(P2_13); |
kwchang2 | 0:8ce138308888 | 20 | AnalogIn sensor(P0_23); |
kwchang2 | 0:8ce138308888 | 21 | |
kwchang2 | 0:8ce138308888 | 22 | int volatile reading; |
kwchang2 | 0:8ce138308888 | 23 | bool volatile newReading = false; |
kwchang2 | 0:8ce138308888 | 24 | |
kwchang2 | 0:8ce138308888 | 25 | void sensor_isr() |
kwchang2 | 0:8ce138308888 | 26 | { |
kwchang2 | 0:8ce138308888 | 27 | reading = (int)(sensor.read() * 1000); |
kwchang2 | 0:8ce138308888 | 28 | newReading = true; |
kwchang2 | 0:8ce138308888 | 29 | } |
kwchang2 | 0:8ce138308888 | 30 | |
kwchang2 | 0:8ce138308888 | 31 | int main() |
kwchang2 | 0:8ce138308888 | 32 | { |
kwchang2 | 0:8ce138308888 | 33 | MDMSerial mdm; |
kwchang2 | 0:8ce138308888 | 34 | |
kwchang2 | 0:8ce138308888 | 35 | Client client; |
kwchang2 | 0:8ce138308888 | 36 | M2XStreamClient m2xClient(&client, m2xKey); |
kwchang2 | 0:8ce138308888 | 37 | |
kwchang2 | 0:8ce138308888 | 38 | DigitalOut led(LED); |
kwchang2 | 0:8ce138308888 | 39 | |
kwchang2 | 0:8ce138308888 | 40 | led = 1; |
kwchang2 | 0:8ce138308888 | 41 | sensortick.attach(sensor_isr, 2); |
kwchang2 | 0:8ce138308888 | 42 | heat = 1; |
kwchang2 | 0:8ce138308888 | 43 | |
kwchang2 | 0:8ce138308888 | 44 | printf("\nConnecting...\n"); |
kwchang2 | 0:8ce138308888 | 45 | |
kwchang2 | 0:8ce138308888 | 46 | //mdm.setDebug(4); // enable this for debugging issues |
kwchang2 | 0:8ce138308888 | 47 | if (!mdm.connect()) |
kwchang2 | 0:8ce138308888 | 48 | { |
kwchang2 | 0:8ce138308888 | 49 | printf("Connect failed!\n"); |
kwchang2 | 0:8ce138308888 | 50 | return -1; |
kwchang2 | 0:8ce138308888 | 51 | } |
kwchang2 | 0:8ce138308888 | 52 | |
kwchang2 | 0:8ce138308888 | 53 | led = 0; |
kwchang2 | 0:8ce138308888 | 54 | printf("Connected!\n"); |
kwchang2 | 0:8ce138308888 | 55 | set_time(0); |
kwchang2 | 0:8ce138308888 | 56 | |
kwchang2 | 0:8ce138308888 | 57 | HTTPClient http; |
kwchang2 | 0:8ce138308888 | 58 | HTTPMap map; |
kwchang2 | 0:8ce138308888 | 59 | char str[512]; |
kwchang2 | 0:8ce138308888 | 60 | HTTPText inText(str, 512); |
kwchang2 | 0:8ce138308888 | 61 | int ret; |
kwchang2 | 0:8ce138308888 | 62 | |
kwchang2 | 0:8ce138308888 | 63 | http.basicAuth(ccUser, ccPass); |
kwchang2 | 0:8ce138308888 | 64 | http.customHeaders(NULL, 0); |
kwchang2 | 0:8ce138308888 | 65 | |
kwchang2 | 0:8ce138308888 | 66 | printf("\nTurning on engine...\n"); |
kwchang2 | 0:8ce138308888 | 67 | do |
kwchang2 | 0:8ce138308888 | 68 | { |
kwchang2 | 0:8ce138308888 | 69 | ret = http.post(ccOnUrl, map, &inText, 15000); |
kwchang2 | 0:8ce138308888 | 70 | if (!ret) |
kwchang2 | 0:8ce138308888 | 71 | { |
kwchang2 | 0:8ce138308888 | 72 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
kwchang2 | 0:8ce138308888 | 73 | printf("Result: %s\n", str); |
kwchang2 | 0:8ce138308888 | 74 | } |
kwchang2 | 0:8ce138308888 | 75 | else |
kwchang2 | 0:8ce138308888 | 76 | { |
kwchang2 | 0:8ce138308888 | 77 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
kwchang2 | 0:8ce138308888 | 78 | } |
kwchang2 | 0:8ce138308888 | 79 | } while (ret); |
kwchang2 | 0:8ce138308888 | 80 | |
kwchang2 | 0:8ce138308888 | 81 | time_t seconds; |
kwchang2 | 0:8ce138308888 | 82 | |
kwchang2 | 0:8ce138308888 | 83 | int currentReading, highReading = 1000, prevReading = 1000; |
kwchang2 | 0:8ce138308888 | 84 | bool alarm = true; |
kwchang2 | 0:8ce138308888 | 85 | unsigned int wait = 15; |
kwchang2 | 0:8ce138308888 | 86 | |
kwchang2 | 0:8ce138308888 | 87 | while (1) |
kwchang2 | 0:8ce138308888 | 88 | { |
kwchang2 | 0:8ce138308888 | 89 | if (newReading) |
kwchang2 | 0:8ce138308888 | 90 | { |
kwchang2 | 0:8ce138308888 | 91 | led = 1; |
kwchang2 | 0:8ce138308888 | 92 | currentReading = reading; // make a copy because the isr might update it before we're done here |
kwchang2 | 0:8ce138308888 | 93 | seconds = time(NULL); |
kwchang2 | 0:8ce138308888 | 94 | printf("%d:%02d:%02d:%02d ", seconds / 86400, (seconds / 3600) % 24, (seconds / 60) % 60, seconds % 60); |
kwchang2 | 0:8ce138308888 | 95 | printf("Alarm: %s Reading: %d (prev: %d; high: %d)\n", (alarm ? "yes" : "no"), currentReading, prevReading, highReading); |
kwchang2 | 0:8ce138308888 | 96 | |
kwchang2 | 0:8ce138308888 | 97 | if (!alarm && ((currentReading - prevReading) > 50)) |
kwchang2 | 0:8ce138308888 | 98 | { |
kwchang2 | 0:8ce138308888 | 99 | // rise since previous reading |
kwchang2 | 0:8ce138308888 | 100 | printf("*** Danger!\n"); |
kwchang2 | 0:8ce138308888 | 101 | |
kwchang2 | 0:8ce138308888 | 102 | printf("Turning off engine...\n"); |
kwchang2 | 0:8ce138308888 | 103 | do |
kwchang2 | 0:8ce138308888 | 104 | { |
kwchang2 | 0:8ce138308888 | 105 | ret = http.post(ccOffUrl, map, &inText, 15000); |
kwchang2 | 0:8ce138308888 | 106 | if (!ret) |
kwchang2 | 0:8ce138308888 | 107 | { |
kwchang2 | 0:8ce138308888 | 108 | printf("Executed POST successfully - read %d characters\n", strlen(str)); |
kwchang2 | 0:8ce138308888 | 109 | printf("Result: %s\n", str); |
kwchang2 | 0:8ce138308888 | 110 | } |
kwchang2 | 0:8ce138308888 | 111 | else |
kwchang2 | 0:8ce138308888 | 112 | { |
kwchang2 | 0:8ce138308888 | 113 | printf("Error - ret = %d - HTTP return code = %d\n", ret, http.getHTTPResponseCode()); |
kwchang2 | 0:8ce138308888 | 114 | } |
kwchang2 | 0:8ce138308888 | 115 | } while (ret); |
kwchang2 | 0:8ce138308888 | 116 | |
kwchang2 | 0:8ce138308888 | 117 | do |
kwchang2 | 0:8ce138308888 | 118 | { |
kwchang2 | 0:8ce138308888 | 119 | printf("Reporting danger...\n"); |
kwchang2 | 0:8ce138308888 | 120 | ret = m2xClient.post(feedId, streamName, 1); |
kwchang2 | 0:8ce138308888 | 121 | printf("Post response code: %d\n", ret); |
kwchang2 | 0:8ce138308888 | 122 | } while (ret != 204); |
kwchang2 | 0:8ce138308888 | 123 | |
kwchang2 | 0:8ce138308888 | 124 | highReading = currentReading; |
kwchang2 | 0:8ce138308888 | 125 | alarm = true; |
kwchang2 | 0:8ce138308888 | 126 | wait = 0; |
kwchang2 | 0:8ce138308888 | 127 | } |
kwchang2 | 0:8ce138308888 | 128 | else if (alarm && ((highReading - currentReading) > 50)) |
kwchang2 | 0:8ce138308888 | 129 | { |
kwchang2 | 0:8ce138308888 | 130 | wait++; |
kwchang2 | 0:8ce138308888 | 131 | if (wait > 15) // 30 s |
kwchang2 | 0:8ce138308888 | 132 | { |
kwchang2 | 0:8ce138308888 | 133 | // fall since last high reading |
kwchang2 | 0:8ce138308888 | 134 | printf("*** Safe!\n"); |
kwchang2 | 0:8ce138308888 | 135 | |
kwchang2 | 0:8ce138308888 | 136 | |
kwchang2 | 0:8ce138308888 | 137 | do |
kwchang2 | 0:8ce138308888 | 138 | { |
kwchang2 | 0:8ce138308888 | 139 | printf("Reporting safe...\n"); |
kwchang2 | 0:8ce138308888 | 140 | ret = m2xClient.post(feedId, streamName, 0); |
kwchang2 | 0:8ce138308888 | 141 | printf("Post response code: %d\n", ret); |
kwchang2 | 0:8ce138308888 | 142 | } while (ret != 204); |
kwchang2 | 0:8ce138308888 | 143 | |
kwchang2 | 0:8ce138308888 | 144 | alarm = false; |
kwchang2 | 0:8ce138308888 | 145 | } |
kwchang2 | 0:8ce138308888 | 146 | } |
kwchang2 | 0:8ce138308888 | 147 | prevReading = currentReading; |
kwchang2 | 0:8ce138308888 | 148 | newReading = false; |
kwchang2 | 0:8ce138308888 | 149 | led = 0; |
kwchang2 | 0:8ce138308888 | 150 | } |
kwchang2 | 0:8ce138308888 | 151 | } |
kwchang2 | 0:8ce138308888 | 152 | |
kwchang2 | 0:8ce138308888 | 153 | /*mdm.disconnect(); |
kwchang2 | 0:8ce138308888 | 154 | mdm.powerOff(); |
kwchang2 | 0:8ce138308888 | 155 | |
kwchang2 | 0:8ce138308888 | 156 | while (1);*/ |
kwchang2 | 0:8ce138308888 | 157 | } |