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: C12832 CCS811 Sht31 TSL2561
Fork of ARM_HACK_THE_BURGH by
main.cpp@57:a2340579d7ad, 2018-03-10 (annotated)
- Committer:
- Ottomanslap
- Date:
- Sat Mar 10 17:46:39 2018 +0000
- Revision:
- 57:a2340579d7ad
- Parent:
- 56:bea82468efd3
- Child:
- 58:44e839410957
Json print temp co2 hum light
Who changed what in which revision?
| User | Revision | Line number | New 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 | 57:a2340579d7ad | 23 | #include <string> |
| Ottomanslap | 57:a2340579d7ad | 24 | using std::string; |
| mbed_official | 0:857719181846 | 25 | |
| mbed_official | 32:bca3f5f442b3 | 26 | #define WIFI_ESP8266 1 |
| mbed_official | 44:63be19b7a3db | 27 | #define WIFI_IDW0XX1 2 |
| mbed_official | 32:bca3f5f442b3 | 28 | |
| CJW | 56:bea82468efd3 | 29 | #if 1 |
| mbed_official | 1:aea78e21a7da | 30 | #include "OdinWiFiInterface.h" |
| mbed_official | 1:aea78e21a7da | 31 | OdinWiFiInterface wifi; |
| mbed_official | 10:5b5beb106156 | 32 | |
| mbed_official | 21:b2f2f6a840b4 | 33 | #elif TARGET_REALTEK_RTL8195AM |
| mbed_official | 21:b2f2f6a840b4 | 34 | #include "RTWInterface.h" |
| mbed_official | 21:b2f2f6a840b4 | 35 | RTWInterface wifi; |
| mbed_official | 21:b2f2f6a840b4 | 36 | |
| mbed_official | 33:12f0df4d51d7 | 37 | #else // External WiFi modules |
| mbed_official | 21:b2f2f6a840b4 | 38 | |
| mbed_official | 32:bca3f5f442b3 | 39 | #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266 |
| mbed_official | 1:aea78e21a7da | 40 | #include "ESP8266Interface.h" |
| mbed_official | 10:5b5beb106156 | 41 | ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
| mbed_official | 44:63be19b7a3db | 42 | #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
| mbed_official | 32:bca3f5f442b3 | 43 | #include "SpwfSAInterface.h" |
| mbed_official | 32:bca3f5f442b3 | 44 | SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX); |
| mbed_official | 44:63be19b7a3db | 45 | #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1 |
| mbed_official | 10:5b5beb106156 | 46 | |
| mbed_official | 33:12f0df4d51d7 | 47 | #endif |
| CJW | 56:bea82468efd3 | 48 | Serial pc(USBTX, USBRX); // tx, rx |
| CJW | 56:bea82468efd3 | 49 | |
| CJW | 56:bea82468efd3 | 50 | C12832 lcd(PE_14, PE_12, PD_12, PD_11, PE_9); |
| Ottomanslap | 57:a2340579d7ad | 51 | Sht31 sht31(PF_0, PF_1); //TEMP SENSOR: I2C_SDA, I2C_SCL |
| Ottomanslap | 57:a2340579d7ad | 52 | CCS811 ccs811(PF_0, PF_1); //IAQ SENSOR: I2C_SDA, I2C_SCL |
| Ottomanslap | 57:a2340579d7ad | 53 | TSL2561 tsl2561(PF_0, PF_1, TSL2561_ADDR_HIGH); //LIGHT SENSOR: I2C_SDA, I2C_SCL |
| Ottomanslap | 57:a2340579d7ad | 54 | |
| Ottomanslap | 57:a2340579d7ad | 55 | |
| CJW | 56:bea82468efd3 | 56 | |
| CJW | 56:bea82468efd3 | 57 | void lcd_print(const char* message) |
| CJW | 56:bea82468efd3 | 58 | { |
| CJW | 56:bea82468efd3 | 59 | lcd.cls(); |
| CJW | 56:bea82468efd3 | 60 | lcd.locate(0, 3); |
| CJW | 56:bea82468efd3 | 61 | pc.printf(message); |
| CJW | 56:bea82468efd3 | 62 | } |
| mbed_official | 0:857719181846 | 63 | |
| mbed_official | 0:857719181846 | 64 | const char *sec2str(nsapi_security_t sec) |
| mbed_official | 0:857719181846 | 65 | { |
| mbed_official | 0:857719181846 | 66 | switch (sec) { |
| mbed_official | 0:857719181846 | 67 | case NSAPI_SECURITY_NONE: |
| mbed_official | 0:857719181846 | 68 | return "None"; |
| mbed_official | 0:857719181846 | 69 | case NSAPI_SECURITY_WEP: |
| mbed_official | 0:857719181846 | 70 | return "WEP"; |
| mbed_official | 0:857719181846 | 71 | case NSAPI_SECURITY_WPA: |
| mbed_official | 0:857719181846 | 72 | return "WPA"; |
| mbed_official | 0:857719181846 | 73 | case NSAPI_SECURITY_WPA2: |
| mbed_official | 0:857719181846 | 74 | return "WPA2"; |
| mbed_official | 0:857719181846 | 75 | case NSAPI_SECURITY_WPA_WPA2: |
| mbed_official | 0:857719181846 | 76 | return "WPA/WPA2"; |
| mbed_official | 0:857719181846 | 77 | case NSAPI_SECURITY_UNKNOWN: |
| mbed_official | 0:857719181846 | 78 | default: |
| mbed_official | 0:857719181846 | 79 | return "Unknown"; |
| mbed_official | 0:857719181846 | 80 | } |
| mbed_official | 0:857719181846 | 81 | } |
| mbed_official | 0:857719181846 | 82 | |
| mbed_official | 37:3a31525e2971 | 83 | int scan_demo(WiFiInterface *wifi) |
| mbed_official | 0:857719181846 | 84 | { |
| mbed_official | 0:857719181846 | 85 | WiFiAccessPoint *ap; |
| mbed_official | 0:857719181846 | 86 | |
| CJW | 56:bea82468efd3 | 87 | pc.printf("Scan:\n"); |
| mbed_official | 0:857719181846 | 88 | |
| mbed_official | 0:857719181846 | 89 | int count = wifi->scan(NULL,0); |
| mbed_official | 0:857719181846 | 90 | |
| mbed_official | 0:857719181846 | 91 | /* Limit number of network arbitrary to 15 */ |
| mbed_official | 0:857719181846 | 92 | count = count < 15 ? count : 15; |
| mbed_official | 0:857719181846 | 93 | |
| mbed_official | 0:857719181846 | 94 | ap = new WiFiAccessPoint[count]; |
| mbed_official | 0:857719181846 | 95 | count = wifi->scan(ap, count); |
| CJW | 56:bea82468efd3 | 96 | for (int i = 0; i < count; i++) { |
| CJW | 56:bea82468efd3 | 97 | 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 | 98 | sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2], |
| CJW | 56:bea82468efd3 | 99 | 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 | 100 | } |
| CJW | 56:bea82468efd3 | 101 | pc.printf("%d networks available.\n", count); |
| mbed_official | 0:857719181846 | 102 | |
| mbed_official | 0:857719181846 | 103 | delete[] ap; |
| mbed_official | 37:3a31525e2971 | 104 | return count; |
| mbed_official | 0:857719181846 | 105 | } |
| mbed_official | 0:857719181846 | 106 | |
| mbed_official | 0:857719181846 | 107 | void http_demo(NetworkInterface *net) |
| mbed_official | 0:857719181846 | 108 | { |
| mbed_official | 0:857719181846 | 109 | TCPSocket socket; |
| mbed_official | 24:323569a565ec | 110 | nsapi_error_t response; |
| mbed_official | 0:857719181846 | 111 | |
| mbed_official | 35:052c1ba06ce7 | 112 | printf("Sending HTTP request to www.arm.com...\n"); |
| mbed_official | 0:857719181846 | 113 | |
| mbed_official | 0:857719181846 | 114 | // Open a socket on the network interface, and create a TCP connection to www.arm.com |
| mbed_official | 0:857719181846 | 115 | socket.open(net); |
| mbed_official | 24:323569a565ec | 116 | response = socket.connect("www.arm.com", 80); |
| mbed_official | 24:323569a565ec | 117 | if(0 != response) { |
| mbed_official | 35:052c1ba06ce7 | 118 | printf("Error connecting: %d\n", response); |
| mbed_official | 24:323569a565ec | 119 | socket.close(); |
| mbed_official | 24:323569a565ec | 120 | return; |
| mbed_official | 24:323569a565ec | 121 | } |
| mbed_official | 0:857719181846 | 122 | |
| mbed_official | 0:857719181846 | 123 | // Send a simple http request |
| mbed_official | 0:857719181846 | 124 | char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n"; |
| mbed_official | 33:12f0df4d51d7 | 125 | nsapi_size_t size = strlen(sbuffer); |
| mbed_official | 24:323569a565ec | 126 | response = 0; |
| CJW | 56:bea82468efd3 | 127 | while(size) { |
| mbed_official | 24:323569a565ec | 128 | response = socket.send(sbuffer+response, size); |
| mbed_official | 24:323569a565ec | 129 | if (response < 0) { |
| mbed_official | 35:052c1ba06ce7 | 130 | printf("Error sending data: %d\n", response); |
| mbed_official | 24:323569a565ec | 131 | socket.close(); |
| mbed_official | 24:323569a565ec | 132 | return; |
| mbed_official | 24:323569a565ec | 133 | } else { |
| mbed_official | 24:323569a565ec | 134 | size -= response; |
| mbed_official | 24:323569a565ec | 135 | // Check if entire message was sent or not |
| mbed_official | 35:052c1ba06ce7 | 136 | printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer); |
| mbed_official | 24:323569a565ec | 137 | } |
| mbed_official | 24:323569a565ec | 138 | } |
| mbed_official | 0:857719181846 | 139 | |
| mbed_official | 0:857719181846 | 140 | // Recieve a simple http response and print out the response line |
| mbed_official | 0:857719181846 | 141 | char rbuffer[64]; |
| mbed_official | 24:323569a565ec | 142 | response = socket.recv(rbuffer, sizeof rbuffer); |
| mbed_official | 24:323569a565ec | 143 | if (response < 0) { |
| mbed_official | 35:052c1ba06ce7 | 144 | printf("Error receiving data: %d\n", response); |
| mbed_official | 24:323569a565ec | 145 | } else { |
| mbed_official | 35:052c1ba06ce7 | 146 | printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer); |
| mbed_official | 24:323569a565ec | 147 | } |
| mbed_official | 0:857719181846 | 148 | |
| mbed_official | 0:857719181846 | 149 | // Close the socket to return its memory and bring down the network interface |
| mbed_official | 0:857719181846 | 150 | socket.close(); |
| mbed_official | 0:857719181846 | 151 | } |
| Ottomanslap | 57:a2340579d7ad | 152 | void display(const char* out){ |
| Ottomanslap | 57:a2340579d7ad | 153 | printf("%s\n", out); |
| Ottomanslap | 57:a2340579d7ad | 154 | } |
| mbed_official | 0:857719181846 | 155 | |
| mbed_official | 0:857719181846 | 156 | int main() |
| mbed_official | 0:857719181846 | 157 | { |
| mbed_official | 37:3a31525e2971 | 158 | int count = 0; |
| mbed_official | 37:3a31525e2971 | 159 | |
| CJW | 56:bea82468efd3 | 160 | pc.printf("WiFi example\n\n"); |
| mbed_official | 0:857719181846 | 161 | |
| mbed_official | 37:3a31525e2971 | 162 | count = scan_demo(&wifi); |
| mbed_official | 37:3a31525e2971 | 163 | if (count == 0) { |
| CJW | 56:bea82468efd3 | 164 | pc.printf("No WIFI APNs found - can't continue further.\n"); |
| mbed_official | 37:3a31525e2971 | 165 | return -1; |
| mbed_official | 37:3a31525e2971 | 166 | } |
| mbed_official | 0:857719181846 | 167 | |
| mbed_official | 37:3a31525e2971 | 168 | printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID); |
| mbed_official | 0:857719181846 | 169 | int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2); |
| mbed_official | 0:857719181846 | 170 | if (ret != 0) { |
| CJW | 56:bea82468efd3 | 171 | pc.printf("\nConnection error\n"); |
| mbed_official | 0:857719181846 | 172 | return -1; |
| mbed_official | 0:857719181846 | 173 | } |
| mbed_official | 0:857719181846 | 174 | |
| CJW | 56:bea82468efd3 | 175 | pc.printf("Success\n\n"); |
| mbed_official | 35:052c1ba06ce7 | 176 | printf("MAC: %s\n", wifi.get_mac_address()); |
| mbed_official | 35:052c1ba06ce7 | 177 | printf("IP: %s\n", wifi.get_ip_address()); |
| mbed_official | 35:052c1ba06ce7 | 178 | printf("Netmask: %s\n", wifi.get_netmask()); |
| mbed_official | 35:052c1ba06ce7 | 179 | printf("Gateway: %s\n", wifi.get_gateway()); |
| mbed_official | 35:052c1ba06ce7 | 180 | printf("RSSI: %d\n\n", wifi.get_rssi()); |
| mbed_official | 0:857719181846 | 181 | |
| mbed_official | 0:857719181846 | 182 | http_demo(&wifi); |
| mbed_official | 0:857719181846 | 183 | |
| mbed_official | 0:857719181846 | 184 | wifi.disconnect(); |
| mbed_official | 0:857719181846 | 185 | |
| mbed_official | 35:052c1ba06ce7 | 186 | printf("\nDone\n"); |
| Ottomanslap | 57:a2340579d7ad | 187 | |
| Ottomanslap | 57:a2340579d7ad | 188 | ccs811.init(); |
| Ottomanslap | 57:a2340579d7ad | 189 | tsl2561.begin(); |
| Ottomanslap | 57:a2340579d7ad | 190 | tsl2561.setGain(TSL2561_GAIN_0X); |
| Ottomanslap | 57:a2340579d7ad | 191 | tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS); |
| Ottomanslap | 57:a2340579d7ad | 192 | |
| Ottomanslap | 57:a2340579d7ad | 193 | while(1) { |
| Ottomanslap | 57:a2340579d7ad | 194 | |
| Ottomanslap | 57:a2340579d7ad | 195 | string json = "{\n"; |
| Ottomanslap | 57:a2340579d7ad | 196 | |
| Ottomanslap | 57:a2340579d7ad | 197 | //Light |
| Ottomanslap | 57:a2340579d7ad | 198 | |
| Ottomanslap | 57:a2340579d7ad | 199 | int x = tsl2561.getLuminosity(TSL2561_VISIBLE); |
| Ottomanslap | 57:a2340579d7ad | 200 | |
| Ottomanslap | 57:a2340579d7ad | 201 | //printf("VIS: %d\n", x); |
| Ottomanslap | 57:a2340579d7ad | 202 | |
| Ottomanslap | 57:a2340579d7ad | 203 | char buffer [8]; |
| Ottomanslap | 57:a2340579d7ad | 204 | int n; |
| Ottomanslap | 57:a2340579d7ad | 205 | n = sprintf (buffer, "%d", x); |
| Ottomanslap | 57:a2340579d7ad | 206 | |
| Ottomanslap | 57:a2340579d7ad | 207 | json += "\t \"Light\" : "; |
| Ottomanslap | 57:a2340579d7ad | 208 | json += buffer; |
| Ottomanslap | 57:a2340579d7ad | 209 | json += ".0,\n"; |
| Ottomanslap | 57:a2340579d7ad | 210 | |
| Ottomanslap | 57:a2340579d7ad | 211 | //Air |
| Ottomanslap | 57:a2340579d7ad | 212 | |
| Ottomanslap | 57:a2340579d7ad | 213 | uint16_t eco2, tvoc; |
| Ottomanslap | 57:a2340579d7ad | 214 | ccs811.readData(&eco2, &tvoc); |
| Ottomanslap | 57:a2340579d7ad | 215 | |
| Ottomanslap | 57:a2340579d7ad | 216 | //printf("eCO2:%dppm\n", eco2); |
| Ottomanslap | 57:a2340579d7ad | 217 | |
| Ottomanslap | 57:a2340579d7ad | 218 | n = sprintf (buffer, "%d", eco2); |
| Ottomanslap | 57:a2340579d7ad | 219 | |
| Ottomanslap | 57:a2340579d7ad | 220 | json += "\t \"eCO2\" : "; |
| Ottomanslap | 57:a2340579d7ad | 221 | json += buffer; |
| Ottomanslap | 57:a2340579d7ad | 222 | json += ".0,\n"; |
| Ottomanslap | 57:a2340579d7ad | 223 | |
| Ottomanslap | 57:a2340579d7ad | 224 | //Hum and Temp |
| Ottomanslap | 57:a2340579d7ad | 225 | |
| Ottomanslap | 57:a2340579d7ad | 226 | float t = sht31.readTemperature(); |
| Ottomanslap | 57:a2340579d7ad | 227 | float h = sht31.readHumidity(); |
| Ottomanslap | 57:a2340579d7ad | 228 | |
| Ottomanslap | 57:a2340579d7ad | 229 | //printf("TEMP:%3.2fC\n", t); |
| Ottomanslap | 57:a2340579d7ad | 230 | //printf("HUM:%3.2f%%\n", h); |
| Ottomanslap | 57:a2340579d7ad | 231 | |
| Ottomanslap | 57:a2340579d7ad | 232 | n = sprintf (buffer, "%3.2f", t); |
| Ottomanslap | 57:a2340579d7ad | 233 | |
| Ottomanslap | 57:a2340579d7ad | 234 | json += "\t \"Tempr\" : "; |
| Ottomanslap | 57:a2340579d7ad | 235 | json += buffer; |
| Ottomanslap | 57:a2340579d7ad | 236 | json += ",\n"; |
| Ottomanslap | 57:a2340579d7ad | 237 | |
| Ottomanslap | 57:a2340579d7ad | 238 | n = sprintf (buffer, "%3.2f", h); |
| Ottomanslap | 57:a2340579d7ad | 239 | |
| Ottomanslap | 57:a2340579d7ad | 240 | json += "\t \"Humid\" : "; |
| Ottomanslap | 57:a2340579d7ad | 241 | json += buffer; |
| Ottomanslap | 57:a2340579d7ad | 242 | json += ",\n"; |
| Ottomanslap | 57:a2340579d7ad | 243 | |
| Ottomanslap | 57:a2340579d7ad | 244 | json += "}\n\n"; |
| Ottomanslap | 57:a2340579d7ad | 245 | printf("%s", json); |
| Ottomanslap | 57:a2340579d7ad | 246 | wait(1); |
| Ottomanslap | 57:a2340579d7ad | 247 | } |
| mbed_official | 0:857719181846 | 248 | } |
