Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: MbedJSONValue mbed-http HTS221 IoTKit
main.cpp@2:a91d74f48f9a, 2020-12-18 (annotated)
- Committer:
- sayhey
- Date:
- Fri Dec 18 19:56:38 2020 +0000
- Revision:
- 2:a91d74f48f9a
- Parent:
- 1:06af85d78aa1
Ready for publication
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| sayhey | 0:6c6307c24f92 | 1 | #include "mbed.h" |
| sayhey | 0:6c6307c24f92 | 2 | #include "https_request.h" |
| sayhey | 0:6c6307c24f92 | 3 | #include "network-helper.h" |
| sayhey | 0:6c6307c24f92 | 4 | #include "TCPSocket.h" |
| sayhey | 0:6c6307c24f92 | 5 | #include "NTPClient.h" |
| sayhey | 0:6c6307c24f92 | 6 | #include "HTS221Sensor.h" |
| sayhey | 0:6c6307c24f92 | 7 | #include "MbedJSONValue.h" |
| sayhey | 0:6c6307c24f92 | 8 | #include "OLEDDisplay.h" |
| sayhey | 0:6c6307c24f92 | 9 | |
| sayhey | 0:6c6307c24f92 | 10 | // Name of the Device shown on Website |
| sayhey | 2:a91d74f48f9a | 11 | char* DeviceName = "ENTER_YOUR_DEVICE_NAME_HERE"; |
| sayhey | 0:6c6307c24f92 | 12 | |
| sayhey | 0:6c6307c24f92 | 13 | // API Token |
| sayhey | 2:a91d74f48f9a | 14 | char* token = "Bearer ENTER_API_TOKEN_HERE"; |
| sayhey | 0:6c6307c24f92 | 15 | |
| sayhey | 0:6c6307c24f92 | 16 | // Route to submit Temp |
| sayhey | 2:a91d74f48f9a | 17 | char* ApiRoute = "ENTER_API_ENDPOINT_HERE"; |
| sayhey | 0:6c6307c24f92 | 18 | |
| sayhey | 0:6c6307c24f92 | 19 | // Trusted Root Certs |
| sayhey | 0:6c6307c24f92 | 20 | const char* DST_ROOT_CA_X3 = "-----BEGIN CERTIFICATE-----\n" |
| sayhey | 0:6c6307c24f92 | 21 | "MIIDSjCCAjKgAwIBAgIQRK+wgNajJ7qJMDmGLvhAazANBgkqhkiG9w0BAQUFADA/\n" |
| sayhey | 0:6c6307c24f92 | 22 | "MSQwIgYDVQQKExtEaWdpdGFsIFNpZ25hdHVyZSBUcnVzdCBDby4xFzAVBgNVBAMT\n" |
| sayhey | 0:6c6307c24f92 | 23 | "DkRTVCBSb290IENBIFgzMB4XDTAwMDkzMDIxMTIxOVoXDTIxMDkzMDE0MDExNVow\n" |
| sayhey | 0:6c6307c24f92 | 24 | "PzEkMCIGA1UEChMbRGlnaXRhbCBTaWduYXR1cmUgVHJ1c3QgQ28uMRcwFQYDVQQD\n" |
| sayhey | 0:6c6307c24f92 | 25 | "Ew5EU1QgUm9vdCBDQSBYMzCCASIwDQYJKoZIhvcNAQEBBQADggEPADCCAQoCggEB\n" |
| sayhey | 0:6c6307c24f92 | 26 | "AN+v6ZdQCINXtMxiZfaQguzH0yxrMMpb7NnDfcdAwRgUi+DoM3ZJKuM/IUmTrE4O\n" |
| sayhey | 0:6c6307c24f92 | 27 | "rz5Iy2Xu/NMhD2XSKtkyj4zl93ewEnu1lcCJo6m67XMuegwGMoOifooUMM0RoOEq\n" |
| sayhey | 0:6c6307c24f92 | 28 | "OLl5CjH9UL2AZd+3UWODyOKIYepLYYHsUmu5ouJLGiifSKOeDNoJjj4XLh7dIN9b\n" |
| sayhey | 0:6c6307c24f92 | 29 | "xiqKqy69cK3FCxolkHRyxXtqqzTWMIn/5WgTe1QLyNau7Fqckh49ZLOMxt+/yUFw\n" |
| sayhey | 0:6c6307c24f92 | 30 | "7BZy1SbsOFU5Q9D8/RhcQPGX69Wam40dutolucbY38EVAjqr2m7xPi71XAicPNaD\n" |
| sayhey | 0:6c6307c24f92 | 31 | "aeQQmxkqtilX4+U9m5/wAl0CAwEAAaNCMEAwDwYDVR0TAQH/BAUwAwEB/zAOBgNV\n" |
| sayhey | 0:6c6307c24f92 | 32 | "HQ8BAf8EBAMCAQYwHQYDVR0OBBYEFMSnsaR7LHH62+FLkHX/xBVghYkQMA0GCSqG\n" |
| sayhey | 0:6c6307c24f92 | 33 | "SIb3DQEBBQUAA4IBAQCjGiybFwBcqR7uKGY3Or+Dxz9LwwmglSBd49lZRNI+DT69\n" |
| sayhey | 0:6c6307c24f92 | 34 | "ikugdB/OEIKcdBodfpga3csTS7MgROSR6cz8faXbauX+5v3gTt23ADq1cEmv8uXr\n" |
| sayhey | 0:6c6307c24f92 | 35 | "AvHRAosZy5Q6XkjEGB5YGV8eAlrwDPGxrancWYaLbumR9YbK+rlmM6pZW87ipxZz\n" |
| sayhey | 0:6c6307c24f92 | 36 | "R8srzJmwN0jP41ZL9c8PDHIyh8bwRLtTcm1D9SZImlJnt1ir/md2cXjbDaJWFBM5\n" |
| sayhey | 0:6c6307c24f92 | 37 | "JDGFoqgCWjBH4d1QB7wCCZAA62RjYJsWvIjJEubSfZGL+T0yjWW06XyxV3bqxbYo\n" |
| sayhey | 0:6c6307c24f92 | 38 | "Ob8VZRzI9neWagqNdwvYkQsEjgfbKbYK7p2CNTUQ\n" |
| sayhey | 0:6c6307c24f92 | 39 | "-----END CERTIFICATE-----\n"; |
| sayhey | 0:6c6307c24f92 | 40 | |
| sayhey | 0:6c6307c24f92 | 41 | // Declare LEDs |
| sayhey | 0:6c6307c24f92 | 42 | DigitalOut Led1(MBED_CONF_IOTKIT_LED1); |
| sayhey | 0:6c6307c24f92 | 43 | DigitalOut Led2(MBED_CONF_IOTKIT_LED2); |
| sayhey | 0:6c6307c24f92 | 44 | DigitalOut Led3(MBED_CONF_IOTKIT_LED3); |
| sayhey | 0:6c6307c24f92 | 45 | DigitalOut Led4(MBED_CONF_IOTKIT_LED4); |
| sayhey | 0:6c6307c24f92 | 46 | |
| sayhey | 0:6c6307c24f92 | 47 | // OLED Init |
| sayhey | 0:6c6307c24f92 | 48 | OLEDDisplay oled( MBED_CONF_IOTKIT_OLED_RST, MBED_CONF_IOTKIT_OLED_SDA, MBED_CONF_IOTKIT_OLED_SCL ); |
| sayhey | 0:6c6307c24f92 | 49 | |
| sayhey | 0:6c6307c24f92 | 50 | // Temp Sensor Init |
| sayhey | 0:6c6307c24f92 | 51 | static DevI2C devI2c( MBED_CONF_IOTKIT_I2C_SDA, MBED_CONF_IOTKIT_I2C_SCL ); |
| sayhey | 0:6c6307c24f92 | 52 | static HTS221Sensor hum_temp(&devI2c); |
| sayhey | 0:6c6307c24f92 | 53 | |
| sayhey | 0:6c6307c24f92 | 54 | // Network |
| sayhey | 0:6c6307c24f92 | 55 | NetworkInterface* network; |
| sayhey | 0:6c6307c24f92 | 56 | |
| sayhey | 0:6c6307c24f92 | 57 | //Global temp var |
| sayhey | 0:6c6307c24f92 | 58 | float temp; |
| sayhey | 0:6c6307c24f92 | 59 | |
| sayhey | 0:6c6307c24f92 | 60 | bool InitTime() { |
| sayhey | 0:6c6307c24f92 | 61 | oled.printf("Getting Time..."); |
| sayhey | 0:6c6307c24f92 | 62 | NTPClient ntp(network); |
| sayhey | 0:6c6307c24f92 | 63 | time_t timestamp = ntp.get_timestamp(); |
| sayhey | 0:6c6307c24f92 | 64 | |
| sayhey | 0:6c6307c24f92 | 65 | if (timestamp < 0) { |
| sayhey | 0:6c6307c24f92 | 66 | printf("An error occurred when getting the time. Code: %ld\r\n", timestamp); |
| sayhey | 0:6c6307c24f92 | 67 | return false; |
| sayhey | 0:6c6307c24f92 | 68 | } |
| sayhey | 0:6c6307c24f92 | 69 | else { |
| sayhey | 0:6c6307c24f92 | 70 | set_time(timestamp); |
| sayhey | 0:6c6307c24f92 | 71 | return true; |
| sayhey | 0:6c6307c24f92 | 72 | } |
| sayhey | 0:6c6307c24f92 | 73 | } |
| sayhey | 0:6c6307c24f92 | 74 | |
| sayhey | 0:6c6307c24f92 | 75 | bool InitializeNetwork() { |
| sayhey | 0:6c6307c24f92 | 76 | |
| sayhey | 0:6c6307c24f92 | 77 | oled.clear(); |
| sayhey | 0:6c6307c24f92 | 78 | oled.printf("Trying to Connect to Network...\n"); |
| sayhey | 0:6c6307c24f92 | 79 | printf("Trying to Connect to Network...\n"); |
| sayhey | 0:6c6307c24f92 | 80 | |
| sayhey | 0:6c6307c24f92 | 81 | // Network Init |
| sayhey | 0:6c6307c24f92 | 82 | network = connect_to_default_network_interface(); |
| sayhey | 0:6c6307c24f92 | 83 | oled.clear(); |
| sayhey | 0:6c6307c24f92 | 84 | |
| sayhey | 0:6c6307c24f92 | 85 | if (!network) { |
| sayhey | 0:6c6307c24f92 | 86 | oled.printf("Cannot connect to the network\n"); |
| sayhey | 0:6c6307c24f92 | 87 | printf("Cannot connect to the network\n"); |
| sayhey | 0:6c6307c24f92 | 88 | return false; |
| sayhey | 0:6c6307c24f92 | 89 | } |
| sayhey | 0:6c6307c24f92 | 90 | else { |
| sayhey | 0:6c6307c24f92 | 91 | oled.printf("Connected to Network\n"); |
| sayhey | 0:6c6307c24f92 | 92 | printf("Connected to Network\n"); |
| sayhey | 0:6c6307c24f92 | 93 | return true; |
| sayhey | 0:6c6307c24f92 | 94 | } |
| sayhey | 0:6c6307c24f92 | 95 | } |
| sayhey | 0:6c6307c24f92 | 96 | |
| sayhey | 0:6c6307c24f92 | 97 | void PrintTemp() { |
| sayhey | 0:6c6307c24f92 | 98 | oled.clear(); |
| sayhey | 0:6c6307c24f92 | 99 | printf("Temp: %3.2f\n", temp); |
| sayhey | 0:6c6307c24f92 | 100 | oled.printf("Temp: %3.2f\n", temp); |
| sayhey | 0:6c6307c24f92 | 101 | } |
| sayhey | 0:6c6307c24f92 | 102 | |
| sayhey | 0:6c6307c24f92 | 103 | char* SendRequest(float temperature) { |
| sayhey | 0:6c6307c24f92 | 104 | |
| sayhey | 0:6c6307c24f92 | 105 | char stringTemp[20]; |
| sayhey | 0:6c6307c24f92 | 106 | sprintf(stringTemp, "%f", temperature); |
| sayhey | 0:6c6307c24f92 | 107 | char timeBuffer[32]; |
| sayhey | 0:6c6307c24f92 | 108 | |
| sayhey | 0:6c6307c24f92 | 109 | time_t seconds = time(NULL); |
| sayhey | 0:6c6307c24f92 | 110 | |
| sayhey | 0:6c6307c24f92 | 111 | strftime(timeBuffer, 32, "%F %T", localtime(&seconds)); |
| sayhey | 0:6c6307c24f92 | 112 | |
| sayhey | 0:6c6307c24f92 | 113 | HttpsRequest* post_req = new HttpsRequest(network, DST_ROOT_CA_X3, HTTP_POST, ApiRoute); |
| sayhey | 0:6c6307c24f92 | 114 | post_req->set_header("Content-Type", "application/json"); |
| sayhey | 0:6c6307c24f92 | 115 | post_req->set_header("Authorization", token); |
| sayhey | 0:6c6307c24f92 | 116 | |
| sayhey | 0:6c6307c24f92 | 117 | char body[255] = "{\"recorded\":\""; |
| sayhey | 0:6c6307c24f92 | 118 | strcat(body, stringTemp); |
| sayhey | 0:6c6307c24f92 | 119 | strcat(body, "\",\"recorded_at\":\""); |
| sayhey | 0:6c6307c24f92 | 120 | strcat(body, timeBuffer); |
| sayhey | 0:6c6307c24f92 | 121 | strcat(body, "\",\"device\":\""); |
| sayhey | 0:6c6307c24f92 | 122 | strcat(body, DeviceName); |
| sayhey | 0:6c6307c24f92 | 123 | strcat(body, "\"}"); |
| sayhey | 0:6c6307c24f92 | 124 | |
| sayhey | 0:6c6307c24f92 | 125 | printf("%s\n", body); |
| sayhey | 0:6c6307c24f92 | 126 | |
| sayhey | 0:6c6307c24f92 | 127 | HttpResponse* post_res = post_req->send(body, strlen(body)); |
| sayhey | 0:6c6307c24f92 | 128 | if (!post_res) { |
| sayhey | 0:6c6307c24f92 | 129 | printf("HttpRequest failed (error code %d)\n", post_req->get_error()); |
| sayhey | 0:6c6307c24f92 | 130 | } |
| sayhey | 0:6c6307c24f92 | 131 | |
| sayhey | 0:6c6307c24f92 | 132 | char* resp = (char*)post_res->get_body_as_string().c_str(); |
| sayhey | 0:6c6307c24f92 | 133 | |
| sayhey | 0:6c6307c24f92 | 134 | delete post_req; |
| sayhey | 0:6c6307c24f92 | 135 | printf("%s\n", resp); |
| sayhey | 0:6c6307c24f92 | 136 | return resp; |
| sayhey | 0:6c6307c24f92 | 137 | } |
| sayhey | 0:6c6307c24f92 | 138 | |
| sayhey | 0:6c6307c24f92 | 139 | void SetLEDStatusCode(char* response) { |
| sayhey | 0:6c6307c24f92 | 140 | MbedJSONValue parser; |
| sayhey | 0:6c6307c24f92 | 141 | |
| sayhey | 0:6c6307c24f92 | 142 | parse(parser, response); |
| sayhey | 0:6c6307c24f92 | 143 | |
| sayhey | 0:6c6307c24f92 | 144 | int statusCode; |
| sayhey | 0:6c6307c24f92 | 145 | |
| sayhey | 0:6c6307c24f92 | 146 | statusCode = parser["code"].get<int>(); |
| sayhey | 0:6c6307c24f92 | 147 | |
| sayhey | 0:6c6307c24f92 | 148 | if (statusCode == 0) { |
| sayhey | 0:6c6307c24f92 | 149 | PrintTemp(); |
| sayhey | 0:6c6307c24f92 | 150 | Led1.write(0); |
| sayhey | 0:6c6307c24f92 | 151 | Led2.write(0); |
| sayhey | 0:6c6307c24f92 | 152 | Led3.write(0); |
| sayhey | 0:6c6307c24f92 | 153 | Led4.write(0); |
| sayhey | 0:6c6307c24f92 | 154 | } |
| sayhey | 0:6c6307c24f92 | 155 | else if(statusCode == 1) { |
| sayhey | 0:6c6307c24f92 | 156 | PrintTemp(); |
| sayhey | 0:6c6307c24f92 | 157 | printf("Temperature is above the limits!\n"); |
| sayhey | 0:6c6307c24f92 | 158 | oled.printf("Temperature is above the limits!\n"); |
| sayhey | 0:6c6307c24f92 | 159 | Led1.write(1); |
| sayhey | 0:6c6307c24f92 | 160 | Led2.write(0); |
| sayhey | 0:6c6307c24f92 | 161 | Led3.write(0); |
| sayhey | 0:6c6307c24f92 | 162 | Led4.write(0); |
| sayhey | 0:6c6307c24f92 | 163 | } |
| sayhey | 0:6c6307c24f92 | 164 | else if(statusCode == 2) { |
| sayhey | 0:6c6307c24f92 | 165 | PrintTemp(); |
| sayhey | 0:6c6307c24f92 | 166 | printf("Temperature is below the limits!\n"); |
| sayhey | 0:6c6307c24f92 | 167 | oled.printf("Temperature is below the limits!\n"); |
| sayhey | 0:6c6307c24f92 | 168 | Led1.write(0); |
| sayhey | 0:6c6307c24f92 | 169 | Led2.write(1); |
| sayhey | 0:6c6307c24f92 | 170 | Led3.write(0); |
| sayhey | 0:6c6307c24f92 | 171 | Led4.write(0); |
| sayhey | 0:6c6307c24f92 | 172 | } |
| sayhey | 0:6c6307c24f92 | 173 | else if(statusCode == 3) { |
| sayhey | 0:6c6307c24f92 | 174 | PrintTemp(); |
| sayhey | 0:6c6307c24f92 | 175 | printf("Undefined Error!\n"); |
| sayhey | 0:6c6307c24f92 | 176 | oled.printf("Undefined Error!\n"); |
| sayhey | 0:6c6307c24f92 | 177 | Led1.write(0); |
| sayhey | 0:6c6307c24f92 | 178 | Led2.write(0); |
| sayhey | 0:6c6307c24f92 | 179 | Led3.write(1); |
| sayhey | 0:6c6307c24f92 | 180 | Led4.write(0); |
| sayhey | 0:6c6307c24f92 | 181 | } |
| sayhey | 0:6c6307c24f92 | 182 | else if(statusCode == 4) { |
| sayhey | 0:6c6307c24f92 | 183 | PrintTemp(); |
| sayhey | 0:6c6307c24f92 | 184 | printf("Undefined Error!\n"); |
| sayhey | 0:6c6307c24f92 | 185 | Led1.write(0); |
| sayhey | 0:6c6307c24f92 | 186 | Led2.write(0); |
| sayhey | 0:6c6307c24f92 | 187 | Led3.write(0); |
| sayhey | 0:6c6307c24f92 | 188 | Led4.write(1); |
| sayhey | 0:6c6307c24f92 | 189 | } |
| sayhey | 0:6c6307c24f92 | 190 | } |
| sayhey | 0:6c6307c24f92 | 191 | |
| sayhey | 0:6c6307c24f92 | 192 | void InitializeTempSensor() { |
| sayhey | 0:6c6307c24f92 | 193 | |
| sayhey | 0:6c6307c24f92 | 194 | hum_temp.init(NULL); |
| sayhey | 0:6c6307c24f92 | 195 | hum_temp.enable(); |
| sayhey | 0:6c6307c24f92 | 196 | } |
| sayhey | 0:6c6307c24f92 | 197 | |
| sayhey | 0:6c6307c24f92 | 198 | int main() { |
| sayhey | 0:6c6307c24f92 | 199 | |
| sayhey | 0:6c6307c24f92 | 200 | if (!InitializeNetwork()) { |
| sayhey | 0:6c6307c24f92 | 201 | return 1; |
| sayhey | 0:6c6307c24f92 | 202 | } |
| sayhey | 0:6c6307c24f92 | 203 | |
| sayhey | 0:6c6307c24f92 | 204 | if (!InitTime()) { |
| sayhey | 0:6c6307c24f92 | 205 | return 1; |
| sayhey | 0:6c6307c24f92 | 206 | } |
| sayhey | 0:6c6307c24f92 | 207 | |
| sayhey | 0:6c6307c24f92 | 208 | time_t seconds = time(NULL); |
| sayhey | 0:6c6307c24f92 | 209 | printf("\rDate & Time: \r%s", ctime(&seconds)); |
| sayhey | 0:6c6307c24f92 | 210 | |
| sayhey | 0:6c6307c24f92 | 211 | InitializeTempSensor(); |
| sayhey | 0:6c6307c24f92 | 212 | |
| sayhey | 0:6c6307c24f92 | 213 | wait(2); |
| sayhey | 0:6c6307c24f92 | 214 | |
| sayhey | 0:6c6307c24f92 | 215 | while (true) { |
| sayhey | 0:6c6307c24f92 | 216 | oled.clear(); |
| sayhey | 0:6c6307c24f92 | 217 | hum_temp.get_temperature(&temp); |
| sayhey | 0:6c6307c24f92 | 218 | SetLEDStatusCode(SendRequest(temp)); |
| sayhey | 0:6c6307c24f92 | 219 | } |
| sayhey | 0:6c6307c24f92 | 220 | } |