Sinan Vanli / Mbed OS ISA

Dependencies:   C12832 CCS811 Sht31 TSL2561

Fork of ARM_HACK_THE_BURGH by Carey Williams

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 /* WiFi Example
00002  * Copyright (c) 2016 ARM Limited
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *     http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include "mbed.h"
00018 #include "TCPSocket.h"
00019 #include "C12832.h"
00020 #include "Sht31.h"
00021 #include "CCS811.h"
00022 #include "TSL2561.h"
00023 #include <cstdlib>
00024 #include <string>
00025 using std::string;
00026 
00027 #define WIFI_ESP8266    1
00028 #define WIFI_IDW0XX1    2
00029 
00030 #if 1
00031 #include "OdinWiFiInterface.h"
00032 OdinWiFiInterface wifi;
00033 
00034 #elif TARGET_REALTEK_RTL8195AM
00035 #include "RTWInterface.h"
00036 RTWInterface wifi;
00037 
00038 #else // External WiFi modules
00039 
00040 #if MBED_CONF_APP_WIFI_SHIELD == WIFI_ESP8266
00041 #include "ESP8266Interface.h"
00042 ESP8266Interface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
00043 #elif MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
00044 #include "SpwfSAInterface.h"
00045 SpwfSAInterface wifi(MBED_CONF_APP_WIFI_TX, MBED_CONF_APP_WIFI_RX);
00046 #endif // MBED_CONF_APP_WIFI_SHIELD == WIFI_IDW0XX1
00047 
00048 #endif
00049 Serial pc(USBTX, USBRX); // tx, rx
00050 
00051 C12832  lcd(PE_14, PE_12, PD_12, PD_11, PE_9);
00052 Sht31 sht31(PF_0, PF_1); //TEMP SENSOR: I2C_SDA, I2C_SCL
00053 CCS811 ccs811(PF_0, PF_1); //IAQ SENSOR: I2C_SDA, I2C_SCL
00054 TSL2561 tsl2561(PF_0, PF_1, TSL2561_ADDR_HIGH); //LIGHT SENSOR: I2C_SDA, I2C_SCL 
00055 
00056 
00057 
00058 void lcd_print(const char* message)
00059 {
00060     lcd.cls();
00061     lcd.locate(0, 3);
00062     pc.printf(message);
00063 }
00064 
00065 const char *sec2str(nsapi_security_t sec)
00066 {
00067     switch (sec) {
00068         case NSAPI_SECURITY_NONE:
00069             return "None";
00070         case NSAPI_SECURITY_WEP:
00071             return "WEP";
00072         case NSAPI_SECURITY_WPA:
00073             return "WPA";
00074         case NSAPI_SECURITY_WPA2:
00075             return "WPA2";
00076         case NSAPI_SECURITY_WPA_WPA2:
00077             return "WPA/WPA2";
00078         case NSAPI_SECURITY_UNKNOWN:
00079         default:
00080             return "Unknown";
00081     }
00082 }
00083 
00084 int scan_demo(WiFiInterface *wifi)
00085 {
00086     WiFiAccessPoint *ap;
00087 
00088     pc.printf("Scan:\n");
00089 
00090     int count = wifi->scan(NULL,0);
00091 
00092     /* Limit number of network arbitrary to 15 */
00093     count = count < 15 ? count : 15;
00094 
00095     ap = new WiFiAccessPoint[count];
00096     count = wifi->scan(ap, count);
00097     for (int i = 0; i < count; i++) {
00098         pc.printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\n", ap[i].get_ssid(),
00099                    sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
00100                    ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
00101     }
00102     pc.printf("%d networks available.\n", count);
00103 
00104     delete[] ap;
00105     return count;
00106 }
00107 
00108 void http_demo(NetworkInterface *net, string ip, string port, string body)
00109 {
00110     TCPSocket socket;
00111     nsapi_error_t response;
00112 
00113     printf("Sending HTTP request to %s:%s...\n", ip.data(), port.data());
00114 
00115     // Open a socket on the network interface, and create a TCP connection to www.arm.com
00116     socket.open(net);
00117     response = socket.connect(ip.data(), std::atoi(port.data()));
00118     if(0 != response) {
00119         printf("Error connecting: %d\n", response);
00120         socket.close();
00121         return;
00122     }
00123     
00124     int body_size = strlen(body.data());
00125     char body_size_s [8];
00126     sprintf (body_size_s, "%d", body_size);
00127     string body_size_string = body_size_s;
00128     
00129     // Send a simple http request
00130     string stringbuffer = "";
00131     stringbuffer += "POST /data HTTP/1.1\r\n";
00132     stringbuffer += "Host: " + ip + "\r\n";
00133     stringbuffer += "Content-Type: application/json\r\n";
00134     //stringbuffer += "Cache-Control: no-cache\r\n";
00135     stringbuffer += "Content-Length: " + body_size_string + "\r\n";
00136     stringbuffer += "\r\n";
00137     stringbuffer += body;
00138     //POST /data HTTP/1.1
00139     //Host: www.arm.com
00140     //Content-Type: application/json
00141     //Content-Length: + body_size
00142     //
00143     //+ body
00144     
00145     const char* sbuffer = stringbuffer.data();
00146     
00147     nsapi_size_t size = strlen(sbuffer);
00148     response = 0;
00149     while(size) {
00150         response = socket.send(sbuffer+response, size);
00151         if (response < 0) {
00152             printf("Error sending data: %d\n", response);
00153             socket.close();
00154             return;
00155         } else {
00156             size -= response;
00157             // Check if entire message was sent or not
00158             printf("sent %d [%.*s]\n", response, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
00159         }
00160     }
00161 
00162     // Recieve a simple http response and print out the response line
00163     char rbuffer[64];
00164     response = socket.recv(rbuffer, sizeof rbuffer);
00165     if (response < 0) {
00166         printf("Error receiving data: %d\n", response);
00167     } else {
00168         printf("recv %d [%.*s]\n", response, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
00169     }
00170 
00171     // Close the socket to return its memory and bring down the network interface
00172     socket.close();
00173 }
00174 void display(const char* out){
00175     printf("%s\n", out);
00176     }
00177 
00178 int main()
00179 {
00180     int count = 0;
00181 
00182     pc.printf("WiFi example\n\n");
00183 
00184     count = scan_demo(&wifi);
00185     if (count == 0) {
00186         pc.printf("No WIFI APNs found - can't continue further.\n");
00187         return -1;
00188     }
00189 
00190     printf("\nConnecting to %s...\n", MBED_CONF_APP_WIFI_SSID);
00191     int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
00192     if (ret != 0) {
00193         pc.printf("\nConnection error\n");
00194         return -1;
00195     }
00196 
00197     pc.printf("Success\n\n");
00198     printf("MAC: %s\n", wifi.get_mac_address());
00199     printf("IP: %s\n", wifi.get_ip_address());
00200     printf("Netmask: %s\n", wifi.get_netmask());
00201     printf("Gateway: %s\n", wifi.get_gateway());
00202     printf("RSSI: %d\n\n", wifi.get_rssi());
00203     
00204     ccs811.init();
00205     tsl2561.begin();
00206     tsl2561.setGain(TSL2561_GAIN_0X);
00207     tsl2561.setTiming(TSL2561_INTEGRATIONTIME_402MS);
00208     
00209     
00210     
00211     string ip = "34.245.151.229";
00212     string port = "5000";
00213     
00214     while(1) {
00215         
00216         string json = "{\n";
00217         
00218         //Light
00219         
00220         int x = tsl2561.getLuminosity(TSL2561_VISIBLE);
00221         
00222         //printf("VIS: %d\n", x);
00223         
00224         char buffer [8];
00225         sprintf (buffer, "%d", x);
00226         
00227         json += "\t \"Light\" : ";
00228         json += buffer;
00229         json += ".0,\n";
00230         
00231         //Air
00232         
00233         uint16_t eco2, tvoc;
00234         ccs811.readData(&eco2, &tvoc);
00235         
00236         //printf("eCO2:%dppm\n", eco2);
00237         
00238         sprintf (buffer, "%d", eco2);
00239         
00240         json += "\t \"eCO2\"  : ";
00241         json += buffer;
00242         json += ".0,\n";
00243         
00244         //Hum and Temp
00245         
00246         float t = sht31.readTemperature();
00247         float h = sht31.readHumidity();
00248         
00249         //printf("TEMP:%3.2fC\n", t);
00250         //printf("HUM:%3.2f%%\n", h);
00251         
00252         sprintf (buffer, "%3.2f", t);
00253         
00254         json += "\t \"Tempr\" : ";
00255         json += buffer;
00256         json += ",\n";
00257         
00258         sprintf (buffer, "%3.2f", h);
00259         
00260         json += "\t \"Humid\" : ";
00261         json += buffer;
00262         json += "\n";
00263         
00264         json += "}\n\n";
00265         printf("%s", json.data());
00266         
00267         http_demo(&wifi, ip, port, json);
00268         
00269         wait(600);
00270     }
00271     
00272     wifi.disconnect();
00273 
00274     printf("\nDone\n");
00275 }