OneNet_IoT_demo for ASC platform

Dependencies:   Common_lib ESP8266 EdpKit_lib cJSON_lib driver_mbed_HP20x driver_mbed_TH02 wifi_example

Fork of mbed-os-example-esp8266 by ESP8266

Revision:
2:1dbc3aa8ae3a
Parent:
0:b887535f68bf
diff -r b4a718e62e0b -r 1dbc3aa8ae3a main.cpp
--- a/main.cpp	Thu Jan 12 22:05:15 2017 +0000
+++ b/main.cpp	Wed Mar 29 03:14:41 2017 +0000
@@ -1,117 +1,74 @@
-/* WiFi Example
- * Copyright (c) 2016 ARM Limited
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- *     http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
+/************************************************************/
+/*  (C) 2016 Beijing ARM Accelerator Technology Co., Ltd.   */
+/*  Description: Demo for ASC Platform.                     */
+/*   Performed an example for getting data form Sensors,    */
+/*   then send data to OneNet Cloud by using EDP protocol   */
+/*  Author: ss.pan                                          */
+/*  Version: 1.01                                           */
+/*  Date: 2017-03-28                                        */
+/************************************************************/
+#include "mbed.h"
+#include "wifi_example.h"
+#include "driver_mbed_TH02.h"
+#include <driver_mbed_HP20x.h>
+#include <driver_mbed_KalmanFilter.h>
 
-#include "mbed.h"
-#include "TCPSocket.h"
+unsigned char ret = 0;
 
+KalmanFilter p_filter;    //Kalman filter for pressure
+KalmanFilter a_filter;    //Kalman filter for altitude
 
-#include "ESP8266Interface.h"
-ESP8266Interface wifi(D1, D0);
+float temperature = -999.999;
+float humidity    = -999.999;
+float pressure    = -999.999;
+float pressure_filtered = -999.999;
+float altitude    = -999.999;
+float altitude_filtered = -999.999;
 
-const char *sec2str(nsapi_security_t sec)
+void getSensorDat(void);
+
+/**
+* @brief  main
+**/
+int main()
 {
-    switch (sec) {
-        case NSAPI_SECURITY_NONE:
-            return "None";
-        case NSAPI_SECURITY_WEP:
-            return "WEP";
-        case NSAPI_SECURITY_WPA:
-            return "WPA";
-        case NSAPI_SECURITY_WPA2:
-            return "WPA2";
-        case NSAPI_SECURITY_WPA_WPA2:
-            return "WPA/WPA2";
-        case NSAPI_SECURITY_UNKNOWN:
-        default:
-            return "Unknown";
+    connectInit();
+    devLink(DEVICEID,APIKEY);
+    while(1) {
+        getSensorDat();
+        if(!checkSocketStatus()) {
+            sendJsonDat("humidity ", humidity);
+            wait_ms(500);
+            sendJsonDat("temperature", temperature);
+            wait_ms(500);
+            sendJsonDat("pressure", pressure_filtered);
+            wait_ms(500);
+            sendJsonDat("altitude", altitude_filtered);
+            wait_ms(500);
+        }
     }
 }
 
-void scan_demo(WiFiInterface *wifi)
+/**
+* @brief  get sensor data
+**/
+void getSensorDat(void)
 {
-    WiFiAccessPoint *ap;
-
-    printf("Scan:\r\n");
-
-    int count = wifi->scan(NULL,0);
-
-    /* Limit number of network arbitrary to 15 */
-    count = count < 15 ? count : 15;
-
-    ap = new WiFiAccessPoint[count];
-    count = wifi->scan(ap, count);
-    for (int i = 0; i < count; i++)
-    {
-        printf("Network: %s secured: %s BSSID: %hhX:%hhX:%hhX:%hhx:%hhx:%hhx RSSI: %hhd Ch: %hhd\r\n", ap[i].get_ssid(),
-               sec2str(ap[i].get_security()), ap[i].get_bssid()[0], ap[i].get_bssid()[1], ap[i].get_bssid()[2],
-               ap[i].get_bssid()[3], ap[i].get_bssid()[4], ap[i].get_bssid()[5], ap[i].get_rssi(), ap[i].get_channel());
-    }
-    printf("%d networks available.\r\n", count);
-
-    delete[] ap;
-}
-
-void http_demo(NetworkInterface *net)
-{
-    TCPSocket socket;
-
-    printf("Sending HTTP request to www.arm.com...\r\n");
-
-    // Open a socket on the network interface, and create a TCP connection to www.arm.com
-    socket.open(net);
-    socket.connect("www.arm.com", 80);
+    int ret;
+    temperature = myTH02.ReadTemperature();
+    humidity    = myTH02.ReadHumidity();
+    wait_ms(50);
+    HP20x.begin();
+    wait_ms(100);
 
-    // Send a simple http request
-    char sbuffer[] = "GET / HTTP/1.1\r\nHost: www.arm.com\r\n\r\n";
-    int scount = socket.send(sbuffer, sizeof sbuffer);
-    printf("sent %d [%.*s]\r\n", scount, strstr(sbuffer, "\r\n")-sbuffer, sbuffer);
-
-    // Recieve a simple http response and print out the response line
-    char rbuffer[64];
-    int rcount = socket.recv(rbuffer, sizeof rbuffer);
-    printf("recv %d [%.*s]\r\n", rcount, strstr(rbuffer, "\r\n")-rbuffer, rbuffer);
-
-    // Close the socket to return its memory and bring down the network interface
-    socket.close();
-}
-
-int main()
-{
-    printf("WiFi example\r\n\r\n");
-
-    // Scan for available access points 
-    scan_demo(&wifi);
+    ret = HP20x.isAvailable();
+    if(OK_HP20X_DEV == ret) {
+        long p_data = HP20x.ReadPressure();
+        pressure = p_data/100.0f;
+        pressure_filtered = p_filter.Filter(pressure);
 
-    printf("\r\nConnecting...\r\n");
-    int ret = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
-    if (ret != 0) {
-        printf("\r\nConnection error\r\n");
-        return -1;
+        long a_data = HP20x.ReadAltitude();
+        altitude = a_data/100.0f;
+        altitude_filtered = a_filter.Filter(altitude);
     }
-
-    printf("Success\r\n\r\n");
-    printf("MAC: %s\r\n", wifi.get_mac_address());
-    printf("IP: %s\r\n", wifi.get_ip_address());
-    printf("Netmask: %s\r\n", wifi.get_netmask());
-    printf("Gateway: %s\r\n", wifi.get_gateway());
-    printf("RSSI: %d\r\n\r\n", wifi.get_rssi());
-
-    http_demo(&wifi);
-
-    wifi.disconnect();
-
-    printf("\r\nDone\r\n");
-}
+}
\ No newline at end of file