Sinan Vanli / Mbed OS ISA

Dependencies:   C12832 CCS811 Sht31 TSL2561

Fork of ARM_HACK_THE_BURGH by Carey Williams

Committer:
Ottomanslap
Date:
Sat Mar 10 19:47:43 2018 +0000
Revision:
59:22dd28696d59
Parent:
58:44e839410957
Child:
60:49746f10fef0
Should work,; Parametrized http demo; Other stuff too; Proud Daddy; Murray helped me shitlots.

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 59:22dd28696d59 134 stringbuffer += "Content-Length: " + body_size_string + "\r\n";
Ottomanslap 59:22dd28696d59 135 stringbuffer += "\r\n";
Ottomanslap 59:22dd28696d59 136 stringbuffer += body;
Ottomanslap 59:22dd28696d59 137 //POST /data HTTP/1.1
Ottomanslap 59:22dd28696d59 138 //Host: www.arm.com
Ottomanslap 59:22dd28696d59 139 //Content-Type: application/json
Ottomanslap 59:22dd28696d59 140 //Content-Length: + body_size
Ottomanslap 59:22dd28696d59 141 //
Ottomanslap 59:22dd28696d59 142 //+ body
Ottomanslap 59:22dd28696d59 143
Ottomanslap 59:22dd28696d59 144 const char* sbuffer = stringbuffer.data();
Ottomanslap 59:22dd28696d59 145
mbed_official 33:12f0df4d51d7 146 nsapi_size_t size = strlen(sbuffer);
mbed_official 24:323569a565ec 147 response = 0;
CJW 56:bea82468efd3 148 while(size) {
mbed_official 24:323569a565ec 149 response = socket.send(sbuffer+response, size);
mbed_official 24:323569a565ec 150 if (response < 0) {
mbed_official 35:052c1ba06ce7 151 printf("Error sending data: %d\n", response);
mbed_official 24:323569a565ec 152 socket.close();
mbed_official 24:323569a565ec 153 return;
mbed_official 24:323569a565ec 154 } else {
mbed_official 24:323569a565ec 155 size -= response;
mbed_official 24:323569a565ec 156 // Check if entire message was sent or not
mbed_official 35:052c1ba06ce7 157 printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
mbed_official 24:323569a565ec 158 }
mbed_official 24:323569a565ec 159 }
mbed_official 0:857719181846 160
mbed_official 0:857719181846 161 // Recieve a simple http response and print out the response line
mbed_official 0:857719181846 162 char rbuffer[64];
mbed_official 24:323569a565ec 163 response = socket.recv(rbuffer, sizeof rbuffer);
mbed_official 24:323569a565ec 164 if (response < 0) {
mbed_official 35:052c1ba06ce7 165 printf("Error receiving data: %d\n", response);
mbed_official 24:323569a565ec 166 } else {
mbed_official 35:052c1ba06ce7 167 printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
mbed_official 24:323569a565ec 168 }
mbed_official 0:857719181846 169
mbed_official 0:857719181846 170 // Close the socket to return its memory and bring down the network interface
mbed_official 0:857719181846 171 socket.close();
mbed_official 0:857719181846 172 }
Ottomanslap 57:a2340579d7ad 173 void display(const char* out){
Ottomanslap 57:a2340579d7ad 174 printf("%s\n", out);
Ottomanslap 57:a2340579d7ad 175 }
mbed_official 0:857719181846 176
mbed_official 0:857719181846 177 int main()
mbed_official 0:857719181846 178 {
mbed_official 37:3a31525e2971 179 int count = 0;
mbed_official 37:3a31525e2971 180
CJW 56:bea82468efd3 181 pc.printf("WiFi example\n\n");
mbed_official 0:857719181846 182
mbed_official 37:3a31525e2971 183 count = scan_demo(&wifi);
mbed_official 37:3a31525e2971 184 if (count == 0) {
CJW 56:bea82468efd3 185 pc.printf("No WIFI APNs found - can't continue further.\n");
mbed_official 37:3a31525e2971 186 return -1;
mbed_official 37:3a31525e2971 187 }
mbed_official 0:857719181846 188
mbed_official 37:3a31525e2971 189 printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
mbed_official 0:857719181846 190 int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
mbed_official 0:857719181846 191 if (ret != 0) {
CJW 56:bea82468efd3 192 pc.printf("\nConnection error\n");
mbed_official 0:857719181846 193 return -1;
mbed_official 0:857719181846 194 }
mbed_official 0:857719181846 195
CJW 56:bea82468efd3 196 pc.printf("Success\n\n");
mbed_official 35:052c1ba06ce7 197 printf("MAC: %s\n", wifi.get_mac_address());
mbed_official 35:052c1ba06ce7 198 printf("IP: %s\n", wifi.get_ip_address());
mbed_official 35:052c1ba06ce7 199 printf("Netmask: %s\n", wifi.get_netmask());
mbed_official 35:052c1ba06ce7 200 printf("Gateway: %s\n", wifi.get_gateway());
mbed_official 35:052c1ba06ce7 201 printf("RSSI: %d\n\n", wifi.get_rssi());
Ottomanslap 57:a2340579d7ad 202
Ottomanslap 57:a2340579d7ad 203 ccs811.init();
Ottomanslap 57:a2340579d7ad 204 tsl2561.begin();
Ottomanslap 57:a2340579d7ad 205 tsl2561.setGain(TSL2561_GAIN_0X);
Ottomanslap 57:a2340579d7ad 206 tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS);
Ottomanslap 57:a2340579d7ad 207
Ottomanslap 58:44e839410957 208 */
Ottomanslap 58:44e839410957 209
Ottomanslap 59:22dd28696d59 210 string ip = "";
Ottomanslap 59:22dd28696d59 211 string port = "80";
Ottomanslap 59:22dd28696d59 212
Ottomanslap 57:a2340579d7ad 213 while(1) {
Ottomanslap 57:a2340579d7ad 214
Ottomanslap 57:a2340579d7ad 215 string json = "{\n";
Ottomanslap 57:a2340579d7ad 216
Ottomanslap 57:a2340579d7ad 217 //Light
Ottomanslap 57:a2340579d7ad 218
Ottomanslap 57:a2340579d7ad 219 int x = tsl2561.getLuminosity(TSL2561_VISIBLE);
Ottomanslap 57:a2340579d7ad 220
Ottomanslap 57:a2340579d7ad 221 //printf("VIS: %d\n", x);
Ottomanslap 57:a2340579d7ad 222
Ottomanslap 57:a2340579d7ad 223 char buffer [8];
Ottomanslap 59:22dd28696d59 224 sprintf (buffer, "%d", x);
Ottomanslap 57:a2340579d7ad 225
Ottomanslap 57:a2340579d7ad 226 json += "\t \"Light\" : ";
Ottomanslap 57:a2340579d7ad 227 json += buffer;
Ottomanslap 57:a2340579d7ad 228 json += ".0,\n";
Ottomanslap 57:a2340579d7ad 229
Ottomanslap 57:a2340579d7ad 230 //Air
Ottomanslap 57:a2340579d7ad 231
Ottomanslap 57:a2340579d7ad 232 uint16_t eco2, tvoc;
Ottomanslap 57:a2340579d7ad 233 ccs811.readData(&eco2, &tvoc);
Ottomanslap 57:a2340579d7ad 234
Ottomanslap 57:a2340579d7ad 235 //printf("eCO2:%dppm\n", eco2);
Ottomanslap 57:a2340579d7ad 236
Ottomanslap 59:22dd28696d59 237 sprintf (buffer, "%d", eco2);
Ottomanslap 57:a2340579d7ad 238
Ottomanslap 57:a2340579d7ad 239 json += "\t \"eCO2\" : ";
Ottomanslap 57:a2340579d7ad 240 json += buffer;
Ottomanslap 57:a2340579d7ad 241 json += ".0,\n";
Ottomanslap 57:a2340579d7ad 242
Ottomanslap 57:a2340579d7ad 243 //Hum and Temp
Ottomanslap 57:a2340579d7ad 244
Ottomanslap 57:a2340579d7ad 245 float t = sht31.readTemperature();
Ottomanslap 57:a2340579d7ad 246 float h = sht31.readHumidity();
Ottomanslap 57:a2340579d7ad 247
Ottomanslap 57:a2340579d7ad 248 //printf("TEMP:%3.2fC\n", t);
Ottomanslap 57:a2340579d7ad 249 //printf("HUM:%3.2f%%\n", h);
Ottomanslap 57:a2340579d7ad 250
Ottomanslap 59:22dd28696d59 251 sprintf (buffer, "%3.2f", t);
Ottomanslap 57:a2340579d7ad 252
Ottomanslap 57:a2340579d7ad 253 json += "\t \"Tempr\" : ";
Ottomanslap 57:a2340579d7ad 254 json += buffer;
Ottomanslap 57:a2340579d7ad 255 json += ",\n";
Ottomanslap 57:a2340579d7ad 256
Ottomanslap 59:22dd28696d59 257 sprintf (buffer, "%3.2f", h);
Ottomanslap 57:a2340579d7ad 258
Ottomanslap 57:a2340579d7ad 259 json += "\t \"Humid\" : ";
Ottomanslap 57:a2340579d7ad 260 json += buffer;
Ottomanslap 57:a2340579d7ad 261 json += ",\n";
Ottomanslap 57:a2340579d7ad 262
Ottomanslap 57:a2340579d7ad 263 json += "}\n\n";
Ottomanslap 59:22dd28696d59 264 printf("%s", json.data());
Ottomanslap 59:22dd28696d59 265
Ottomanslap 59:22dd28696d59 266 http_demo(&wifi, ip, port, json);
Ottomanslap 59:22dd28696d59 267
Ottomanslap 57:a2340579d7ad 268 wait(1);
Ottomanslap 57:a2340579d7ad 269 }
Ottomanslap 59:22dd28696d59 270
Ottomanslap 59:22dd28696d59 271 wifi.disconnect();
Ottomanslap 59:22dd28696d59 272
Ottomanslap 59:22dd28696d59 273 printf("\nDone\n");
mbed_official 0:857719181846 274 }