Deep Slumber, codename ISA, is a program made for the arm MBED during Hack The Burgh 2018 that analyses light, temperature, humidity and CO2 levels in a room. It uploads this information onto an SQL server through a rest API, providing the necessary environment for data processing. Further improvements we hope to provide are the regulation of said parameters by wifi connection to electric heaters, wifi enabled controllable lightbulbs and other iot gadgets as well as a website that will provide recommendations for sleep cycle improvements.

Dependencies:   C12832 CCS811 Sht31 TSL2561

Fork of ARM_HACK_THE_BURGH by Carey Williams

Committer:
Ottomanslap
Date:
Sun Mar 11 02:51:01 2018 +0000
Revision:
60:49746f10fef0
Parent:
59:22dd28696d59
SUCCESS

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mbed_official 0:857719181846 1 /* WiFi Example
mbed_official 0:857719181846 2 * Copyright (c) 2016 ARM Limited
mbed_official 0:857719181846 3 *
mbed_official 0:857719181846 4 * Licensed under the Apache License, Version 2.0 (the "License");
mbed_official 0:857719181846 5 * you may not use this file except in compliance with the License.
mbed_official 0:857719181846 6 * You may obtain a copy of the License at
mbed_official 0:857719181846 7 *
mbed_official 0:857719181846 8 * http://www.apache.org/licenses/LICENSE-2.0
mbed_official 0:857719181846 9 *
mbed_official 0:857719181846 10 * Unless required by applicable law or agreed to in writing, software
mbed_official 0:857719181846 11 * distributed under the License is distributed on an "AS IS" BASIS,
mbed_official 0:857719181846 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
mbed_official 0:857719181846 13 * See the License for the specific language governing permissions and
mbed_official 0:857719181846 14 * limitations under the License.
mbed_official 0:857719181846 15 */
mbed_official 0:857719181846 16
mbed_official 0:857719181846 17 #include "mbed.h"
mbed_official 0:857719181846 18 #include "TCPSocket.h"
CJW 56:bea82468efd3 19 #include "C12832.h"
Ottomanslap 57:a2340579d7ad 20 #include "Sht31.h"
Ottomanslap 57:a2340579d7ad 21 #include "CCS811.h"
Ottomanslap 57:a2340579d7ad 22 #include "TSL2561.h"
Ottomanslap 59:22dd28696d59 23 #include <cstdlib>
Ottomanslap 57:a2340579d7ad 24 #include <string>
Ottomanslap 57:a2340579d7ad 25 using std::string;
mbed_official 0:857719181846 26
mbed_official 32:bca3f5f442b3 27 #define WIFI_ESP8266 1
mbed_official 44:63be19b7a3db 28 #define WIFI_IDW0XX1 2
mbed_official 32:bca3f5f442b3 29
CJW 56:bea82468efd3 30 #if 1
mbed_official 1:aea78e21a7da 31 #include "OdinWiFiInterface.h"
mbed_official 1:aea78e21a7da 32 OdinWiFiInterface wifi;
mbed_official 10:5b5beb106156 33
mbed_official 21:b2f2f6a840b4 34 #elif TARGET_REALTEK_RTL8195AM
mbed_official 21:b2f2f6a840b4 35 #include "RTWInterface.h"
mbed_official 21:b2f2f6a840b4 36 RTWInterface wifi;
mbed_official 21:b2f2f6a840b4 37
mbed_official 33:12f0df4d51d7 38 #else // External WiFi modules
mbed_official 21:b2f2f6a840b4 39
mbed_official 32:bca3f5f442b3 40 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
mbed_official 1:aea78e21a7da 41 #include "ESP8266Interface.h"
mbed_official 10:5b5beb106156 42 ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 44:63be19b7a3db 43 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 32:bca3f5f442b3 44 #include "SpwfSAInterface.h"
mbed_official 32:bca3f5f442b3 45 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
mbed_official 44:63be19b7a3db 46 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
mbed_official 10:5b5beb106156 47
mbed_official 33:12f0df4d51d7 48 #endif
CJW 56:bea82468efd3 49 Serial pc(USBTX, USBRX); // tx, rx
CJW 56:bea82468efd3 50
CJW 56:bea82468efd3 51 C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
Ottomanslap 57:a2340579d7ad 52 Sht31 sht31(PF_0, PF_1); //TEMP SENSOR: I2C_SDA, I2C_SCL
Ottomanslap 57:a2340579d7ad 53 CCS811 ccs811(PF_0, PF_1); //IAQ SENSOR: I2C_SDA, I2C_SCL
Ottomanslap 57:a2340579d7ad 54 TSL2561 tsl2561(PF_0, PF_1, TSL2561_ADDR_HIGH); //LIGHT SENSOR: I2C_SDA, I2C_SCL
Ottomanslap 57:a2340579d7ad 55
Ottomanslap 57:a2340579d7ad 56
CJW 56:bea82468efd3 57
CJW 56:bea82468efd3 58 void lcd_print(const char* message)
CJW 56:bea82468efd3 59 {
CJW 56:bea82468efd3 60 lcd.cls();
CJW 56:bea82468efd3 61 lcd.locate(0, 3);
CJW 56:bea82468efd3 62 pc.printf(message);
CJW 56:bea82468efd3 63 }
mbed_official 0:857719181846 64
mbed_official 0:857719181846 65 const char *sec2str(nsapi_security_t sec)
mbed_official 0:857719181846 66 {
mbed_official 0:857719181846 67 switch (sec) {
mbed_official 0:857719181846 68 case NSAPI_SECURITY_NONE:
mbed_official 0:857719181846 69 return "None";
mbed_official 0:857719181846 70 case NSAPI_SECURITY_WEP:
mbed_official 0:857719181846 71 return "WEP";
mbed_official 0:857719181846 72 case NSAPI_SECURITY_WPA:
mbed_official 0:857719181846 73 return "WPA";
mbed_official 0:857719181846 74 case NSAPI_SECURITY_WPA2:
mbed_official 0:857719181846 75 return "WPA2";
mbed_official 0:857719181846 76 case NSAPI_SECURITY_WPA_WPA2:
mbed_official 0:857719181846 77 return "WPA/WPA2";
mbed_official 0:857719181846 78 case NSAPI_SECURITY_UNKNOWN:
mbed_official 0:857719181846 79 default:
mbed_official 0:857719181846 80 return "Unknown";
mbed_official 0:857719181846 81 }
mbed_official 0:857719181846 82 }
mbed_official 0:857719181846 83
mbed_official 37:3a31525e2971 84 int scan_demo(WiFiInterface *wifi)
mbed_official 0:857719181846 85 {
mbed_official 0:857719181846 86 WiFiAccessPoint *ap;
mbed_official 0:857719181846 87
CJW 56:bea82468efd3 88 pc.printf("Scan:\n");
mbed_official 0:857719181846 89
mbed_official 0:857719181846 90 int count = wifi->scan(NULL,0);
mbed_official 0:857719181846 91
mbed_official 0:857719181846 92 /* Limit number of network arbitrary to 15 */
mbed_official 0:857719181846 93 count = count < 15 ? count : 15;
mbed_official 0:857719181846 94
mbed_official 0:857719181846 95 ap = new WiFiAccessPoint[count];
mbed_official 0:857719181846 96 count = wifi->scan(ap, count);
CJW 56:bea82468efd3 97 for (int i = 0; i < count; i++) {
CJW 56:bea82468efd3 98 pc.printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
CJW 56:bea82468efd3 99 sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
CJW 56:bea82468efd3 100 ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
mbed_official 0:857719181846 101 }
CJW 56:bea82468efd3 102 pc.printf("%d networks available.\n", count);
mbed_official 0:857719181846 103
mbed_official 0:857719181846 104 delete[] ap;
mbed_official 37:3a31525e2971 105 return count;
mbed_official 0:857719181846 106 }
mbed_official 0:857719181846 107
Ottomanslap 59:22dd28696d59 108 void http_demo(NetworkInterface *net, string ip, string port, string body)
mbed_official 0:857719181846 109 {
mbed_official 0:857719181846 110 TCPSocket socket;
mbed_official 24:323569a565ec 111 nsapi_error_t response;
mbed_official 0:857719181846 112
Ottomanslap 59:22dd28696d59 113 printf("Sending HTTP request to %s:%s...\n", ip.data(), port.data());
mbed_official 0:857719181846 114
mbed_official 0:857719181846 115 // Open a socket on the network interface, and create a TCP connection to www.arm.com
mbed_official 0:857719181846 116 socket.open(net);
Ottomanslap 59:22dd28696d59 117 response = socket.connect(ip.data(), std::atoi(port.data()));
mbed_official 24:323569a565ec 118 if(0 != response) {
mbed_official 35:052c1ba06ce7 119 printf("Error connecting: %d\n", response);
mbed_official 24:323569a565ec 120 socket.close();
mbed_official 24:323569a565ec 121 return;
mbed_official 24:323569a565ec 122 }
Ottomanslap 59:22dd28696d59 123
Ottomanslap 59:22dd28696d59 124 int body_size = strlen(body.data());
Ottomanslap 59:22dd28696d59 125 char body_size_s [8];
Ottomanslap 59:22dd28696d59 126 sprintf (body_size_s, "%d", body_size);
Ottomanslap 59:22dd28696d59 127 string body_size_string = body_size_s;
Ottomanslap 59:22dd28696d59 128
mbed_official 0:857719181846 129 // Send a simple http request
Ottomanslap 59:22dd28696d59 130 string stringbuffer = "";
Ottomanslap 59:22dd28696d59 131 stringbuffer += "POST /data HTTP/1.1\r\n";
Ottomanslap 59:22dd28696d59 132 stringbuffer += "Host: " + ip + "\r\n";
Ottomanslap 59:22dd28696d59 133 stringbuffer += "Content-Type: application/json\r\n";
Ottomanslap 60:49746f10fef0 134 //stringbuffer += "Cache-Control: no-cache\r\n";
Ottomanslap 59:22dd28696d59 135 stringbuffer += "Content-Length: " + body_size_string + "\r\n";
Ottomanslap 59:22dd28696d59 136 stringbuffer += "\r\n";
Ottomanslap 59:22dd28696d59 137 stringbuffer += body;
Ottomanslap 59:22dd28696d59 138 //POST /data HTTP/1.1
Ottomanslap 59:22dd28696d59 139 //Host: www.arm.com
Ottomanslap 59:22dd28696d59 140 //Content-Type: application/json
Ottomanslap 59:22dd28696d59 141 //Content-Length: + body_size
Ottomanslap 59:22dd28696d59 142 //
Ottomanslap 59:22dd28696d59 143 //+ body
Ottomanslap 59:22dd28696d59 144
Ottomanslap 59:22dd28696d59 145 const char* sbuffer = stringbuffer.data();
Ottomanslap 59:22dd28696d59 146
mbed_official 33:12f0df4d51d7 147 nsapi_size_t size = strlen(sbuffer);
mbed_official 24:323569a565ec 148 response = 0;
CJW 56:bea82468efd3 149 while(size) {
mbed_official 24:323569a565ec 150 response = socket.send(sbuffer+response, size);
mbed_official 24:323569a565ec 151 if (response < 0) {
mbed_official 35:052c1ba06ce7 152 printf("Error sending data: %d\n", response);
mbed_official 24:323569a565ec 153 socket.close();
mbed_official 24:323569a565ec 154 return;
mbed_official 24:323569a565ec 155 } else {
mbed_official 24:323569a565ec 156 size -= response;
mbed_official 24:323569a565ec 157 // Check if entire message was sent or not
mbed_official 35:052c1ba06ce7 158 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 24:323569a565ec 159 }
mbed_official 24:323569a565ec 160 }
mbed_official 0:857719181846 161
mbed_official 0:857719181846 162 // Recieve a simple http response and print out the response line
mbed_official 0:857719181846 163 char rbuffer[64];
mbed_official 24:323569a565ec 164 response = socket.recv(rbuffer, sizeof rbuffer);
mbed_official 24:323569a565ec 165 if (response < 0) {
mbed_official 35:052c1ba06ce7 166 printf("Error receiving data: %d\n", response);
mbed_official 24:323569a565ec 167 } else {
mbed_official 35:052c1ba06ce7 168 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 24:323569a565ec 169 }
mbed_official 0:857719181846 170
mbed_official 0:857719181846 171 // Close the socket to return its memory and bring down the network interface
mbed_official 0:857719181846 172 socket.close();
mbed_official 0:857719181846 173 }
Ottomanslap 57:a2340579d7ad 174 void display(const char* out){
Ottomanslap 57:a2340579d7ad 175 printf("%s\n", out);
Ottomanslap 57:a2340579d7ad 176 }
mbed_official 0:857719181846 177
mbed_official 0:857719181846 178 int main()
mbed_official 0:857719181846 179 {
mbed_official 37:3a31525e2971 180 int count = 0;
mbed_official 37:3a31525e2971 181
CJW 56:bea82468efd3 182 pc.printf("WiFi example\n\n");
mbed_official 0:857719181846 183
mbed_official 37:3a31525e2971 184 count = scan_demo(&wifi);
mbed_official 37:3a31525e2971 185 if (count == 0) {
CJW 56:bea82468efd3 186 pc.printf("No WIFI APNs found - can't continue further.\n");
mbed_official 37:3a31525e2971 187 return -1;
mbed_official 37:3a31525e2971 188 }
mbed_official 0:857719181846 189
mbed_official 37:3a31525e2971 190 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
mbed_official 0:857719181846 191 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 192 if (ret != 0) {
CJW 56:bea82468efd3 193 pc.printf("\nConnection error\n");
mbed_official 0:857719181846 194 return -1;
mbed_official 0:857719181846 195 }
mbed_official 0:857719181846 196
CJW 56:bea82468efd3 197 pc.printf("Success\n\n");
mbed_official 35:052c1ba06ce7 198 printf("MAC: %s\n", wifi.get_mac_address());
mbed_official 35:052c1ba06ce7 199 printf("IP: %s\n", wifi.get_ip_address());
mbed_official 35:052c1ba06ce7 200 printf("Netmask: %s\n", wifi.get_netmask());
mbed_official 35:052c1ba06ce7 201 printf("Gateway: %s\n", wifi.get_gateway());
mbed_official 35:052c1ba06ce7 202 printf("RSSI: %d\n\n", wifi.get_rssi());
Ottomanslap 57:a2340579d7ad 203
Ottomanslap 57:a2340579d7ad 204 ccs811.init();
Ottomanslap 57:a2340579d7ad 205 tsl2561.begin();
Ottomanslap 57:a2340579d7ad 206 tsl2561.setGain(TSL2561_GAIN_0X);
Ottomanslap 57:a2340579d7ad 207 tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS);
Ottomanslap 57:a2340579d7ad 208
Ottomanslap 60:49746f10fef0 209
Ottomanslap 58:44e839410957 210
Ottomanslap 60:49746f10fef0 211 string ip = "34.245.151.229";
Ottomanslap 60:49746f10fef0 212 string port = "5000";
Ottomanslap 59:22dd28696d59 213
Ottomanslap 57:a2340579d7ad 214 while(1) {
Ottomanslap 57:a2340579d7ad 215
Ottomanslap 57:a2340579d7ad 216 string json = "{\n";
Ottomanslap 57:a2340579d7ad 217
Ottomanslap 57:a2340579d7ad 218 //Light
Ottomanslap 57:a2340579d7ad 219
Ottomanslap 57:a2340579d7ad 220 int x = tsl2561.getLuminosity(TSL2561_VISIBLE);
Ottomanslap 57:a2340579d7ad 221
Ottomanslap 57:a2340579d7ad 222 //printf("VIS: %d\n", x);
Ottomanslap 57:a2340579d7ad 223
Ottomanslap 57:a2340579d7ad 224 char buffer [8];
Ottomanslap 59:22dd28696d59 225 sprintf (buffer, "%d", x);
Ottomanslap 57:a2340579d7ad 226
Ottomanslap 57:a2340579d7ad 227 json += "\t \"Light\" : ";
Ottomanslap 57:a2340579d7ad 228 json += buffer;
Ottomanslap 57:a2340579d7ad 229 json += ".0,\n";
Ottomanslap 57:a2340579d7ad 230
Ottomanslap 57:a2340579d7ad 231 //Air
Ottomanslap 57:a2340579d7ad 232
Ottomanslap 57:a2340579d7ad 233 uint16_t eco2, tvoc;
Ottomanslap 57:a2340579d7ad 234 ccs811.readData(&eco2, &tvoc);
Ottomanslap 57:a2340579d7ad 235
Ottomanslap 57:a2340579d7ad 236 //printf("eCO2:%dppm\n", eco2);
Ottomanslap 57:a2340579d7ad 237
Ottomanslap 59:22dd28696d59 238 sprintf (buffer, "%d", eco2);
Ottomanslap 57:a2340579d7ad 239
Ottomanslap 57:a2340579d7ad 240 json += "\t \"eCO2\" : ";
Ottomanslap 57:a2340579d7ad 241 json += buffer;
Ottomanslap 57:a2340579d7ad 242 json += ".0,\n";
Ottomanslap 57:a2340579d7ad 243
Ottomanslap 57:a2340579d7ad 244 //Hum and Temp
Ottomanslap 57:a2340579d7ad 245
Ottomanslap 57:a2340579d7ad 246 float t = sht31.readTemperature();
Ottomanslap 57:a2340579d7ad 247 float h = sht31.readHumidity();
Ottomanslap 57:a2340579d7ad 248
Ottomanslap 57:a2340579d7ad 249 //printf("TEMP:%3.2fC\n", t);
Ottomanslap 57:a2340579d7ad 250 //printf("HUM:%3.2f%%\n", h);
Ottomanslap 57:a2340579d7ad 251
Ottomanslap 59:22dd28696d59 252 sprintf (buffer, "%3.2f", t);
Ottomanslap 57:a2340579d7ad 253
Ottomanslap 57:a2340579d7ad 254 json += "\t \"Tempr\" : ";
Ottomanslap 57:a2340579d7ad 255 json += buffer;
Ottomanslap 57:a2340579d7ad 256 json += ",\n";
Ottomanslap 57:a2340579d7ad 257
Ottomanslap 59:22dd28696d59 258 sprintf (buffer, "%3.2f", h);
Ottomanslap 57:a2340579d7ad 259
Ottomanslap 57:a2340579d7ad 260 json += "\t \"Humid\" : ";
Ottomanslap 57:a2340579d7ad 261 json += buffer;
Ottomanslap 60:49746f10fef0 262 json += "\n";
Ottomanslap 57:a2340579d7ad 263
Ottomanslap 57:a2340579d7ad 264 json += "}\n\n";
Ottomanslap 59:22dd28696d59 265 printf("%s", json.data());
Ottomanslap 59:22dd28696d59 266
Ottomanslap 59:22dd28696d59 267 http_demo(&wifi, ip, port, json);
Ottomanslap 59:22dd28696d59 268
Ottomanslap 60:49746f10fef0 269 wait(600);
Ottomanslap 57:a2340579d7ad 270 }
Ottomanslap 59:22dd28696d59 271
Ottomanslap 59:22dd28696d59 272 wifi.disconnect();
Ottomanslap 59:22dd28696d59 273
Ottomanslap 59:22dd28696d59 274 printf("\nDone\n");
mbed_official 0:857719181846 275 }