project

Dependencies:   MbedJSONValue DISCO_L475VG_IOT01A_wifi mbed-http

Files at this revision

API Documentation at this revision

Comitter:
Eason0505
Date:
Mon Jan 07 11:19:49 2019 +0000
Commit message:
IoT Project code

Changed in this revision

DISCO_L475VG_IOT01A_wifi.lib Show annotated file Show diff for this revision Revisions of this file
MbedJSONValue.lib Show annotated file Show diff for this revision Revisions of this file
ServoMove.cpp Show annotated file Show diff for this revision Revisions of this file
ServoMove.h Show annotated file Show diff for this revision Revisions of this file
US100.cpp Show annotated file Show diff for this revision Revisions of this file
US100.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed-http.lib Show annotated file Show diff for this revision Revisions of this file
mbed-os.lib Show annotated file Show diff for this revision Revisions of this file
mbed_app.json Show annotated file Show diff for this revision Revisions of this file
mbed_app_ism43362.json Show annotated file Show diff for this revision Revisions of this file
wifi-ism43362.lib Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 8bcd728684c5 DISCO_L475VG_IOT01A_wifi.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/DISCO_L475VG_IOT01A_wifi.lib	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/ST/code/DISCO_L475VG_IOT01A_wifi/#c61a93635433
diff -r 000000000000 -r 8bcd728684c5 MbedJSONValue.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/MbedJSONValue.lib	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,1 @@
+https://os.mbed.com/users/samux/code/MbedJSONValue/#10a99cdf7846
diff -r 000000000000 -r 8bcd728684c5 ServoMove.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServoMove.cpp	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,38 @@
+#include "ServoMove.h"
+PwmOut servo(PA_6);
+Serial pc1(USBTX, USBRX);
+
+void ServoConvert(double cnv)
+{
+    if (cnv > 144) {
+        servo.pulsewidth_us(1000); //0deg
+        pc1.printf("cnv1 =%f\n\r",cnv);
+    } else if(cnv >108  && cnv <144) {
+        servo.pulsewidth_us(1100);
+        pc1.printf("cnv2 =%f\n\r",cnv);
+    } else if (cnv >72 && cnv <108) {
+        servo.pulsewidth_us(1200);
+        pc1.printf("cnv3 =%f\n\r",cnv);
+    } else if (cnv > 36&& cnv <72) {
+        servo.pulsewidth_us(1300);
+        pc1.printf("cnv4 =%f\n\r",cnv);
+    } else if(cnv > 0&& cnv <36) {
+        servo.pulsewidth_us(1400);
+        pc1.printf("cnv5 =%f\n\r",cnv);
+    } else if(cnv > -36&& cnv <0) {
+        servo.pulsewidth_us(1500);
+        pc1.printf("cnv6 =%f\n\r",cnv);
+    } else if(cnv > -72&& cnv <-36) {
+        servo.pulsewidth_us(1600);
+        pc1.printf("cnv7 =%f\n\r",cnv);
+    } else if(cnv > -108&& cnv <-72) {
+        servo.pulsewidth_us(1700);
+        pc1.printf("cnv8 =%f\n\r",cnv);
+    } else if(cnv > -144&& cnv <-108) {
+        servo.pulsewidth_us(1800);
+        pc1.printf("cnv9 =%f\n\r",cnv);
+    } else if(cnv > -180&& cnv <-144) {
+        servo.pulsewidth_us(1900);
+        pc1.printf("cnv10 =%f\n\r",cnv);
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 8bcd728684c5 ServoMove.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ServoMove.h	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,6 @@
+#ifndef SERVOMOVE_H
+#define SERVOMOVE_H
+#include "mbed.h"
+extern PwmOut servo;
+void ServoConvert(double cnv); // function prototype
+#endif
\ No newline at end of file
diff -r 000000000000 -r 8bcd728684c5 US100.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/US100.cpp	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,29 @@
+#include "US100.h"
+#include "mbed.h"
+US100::US100(PinName t, PinName e) : trig(t), echo(e) {}
+// Trigger Echo
+// ______ _____________,,,,,,,,,
+// ____| 10us |_________| 150us-25ms, or 38ms if no obstacle
+//
+//return echo duration in us (refer to digram above)
+int US100::echo_duration()
+{
+    timer.reset();
+    trig = 0;
+    wait_us(10);
+    trig = 1;
+    wait_us(10);
+    trig = 0;
+    while(echo == 0);
+    timer.start();
+    while(echo == 1);
+    timer.stop();
+    return timer.read_us();
+}
+
+int US100::distance()
+{
+int duration = echo_duration();
+int distacne_mm = duration*0.34/2/10;
+return distacne_mm;
+}
\ No newline at end of file
diff -r 000000000000 -r 8bcd728684c5 US100.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/US100.h	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,16 @@
+#ifndef US100_H
+#define US100_H
+#include "mbed.h"
+class US100
+{
+public:
+    US100(PinName t, PinName e);
+    int echo_duration();
+    int distance();
+private:
+    DigitalOut trig;
+    DigitalIn echo;
+    Timer timer;
+};
+
+#endif
\ No newline at end of file
diff -r 000000000000 -r 8bcd728684c5 main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,150 @@
+#include "mbed.h"
+#include "wifi.h"
+#include "US100.h"
+#include "NetworkInterface.h"
+#include "ISM43362Interface.h"
+#include "http_request.h"
+#include "ServoMove.h"
+#include <string>
+
+/* Private defines -----------------------------------------------------------*/
+#define WIFI_WRITE_TIMEOUT 10000
+#define WIFI_READ_TIMEOUT  10000
+#define CONNECTION_TRIAL_MAX          10
+
+/* Private typedef------------------------------------------------------------*/
+/* Private macro -------------------------------------------------------------*/
+/* Private variables ---------------------------------------------------------*/
+Serial pc(SERIAL_TX, SERIAL_RX);
+ISM43362Interface wifi(MBED_CONF_APP_WIFI_SPI_MOSI, MBED_CONF_APP_WIFI_SPI_MISO, MBED_CONF_APP_WIFI_SPI_SCLK, MBED_CONF_APP_WIFI_SPI_NSS, MBED_CONF_APP_WIFI_RESET, MBED_CONF_APP_WIFI_DATAREADY, MBED_CONF_APP_WIFI_WAKEUP, false);
+uint8_t  MAC_Addr[6]; 
+uint8_t  IP_Addr[4];
+
+// Interval(second) to do tasking &sensing
+const int INTERVAL  = 1;
+// Distance sensor
+US100 sensor(PC_3, PC_4);
+// Select network interface 
+NetworkInterface* network = &wifi;
+
+int main()
+{
+
+    pc.baud(9600);
+
+    printf("\n");
+    printf("************************************************************\n");
+    printf("***          IoT Final Project - TSENG, I-SHENG          ***\n");
+    printf("************************************************************\n");
+
+    /*Initialize  WIFI module */
+    if(WIFI_Init() ==  WIFI_STATUS_OK) {
+        printf("> WIFI Module Initialized.\n");  
+        if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
+            printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",     
+                   MAC_Addr[0],
+                   MAC_Addr[1],
+                   MAC_Addr[2],
+                   MAC_Addr[3],
+                   MAC_Addr[4],
+                   MAC_Addr[5]);   
+        } else {
+            printf("> ERROR : CANNOT get MAC address\n");
+        }
+    
+        if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
+            printf("> es-wifi module connected \n");
+            if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
+                printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",     
+                       IP_Addr[0],
+                       IP_Addr[1],
+                       IP_Addr[2],
+                       IP_Addr[3]); 
+            } else {
+                printf("> ERROR : es-wifi module CANNOT get IP address\n");
+            }
+        } else {
+            printf("> ERROR : es-wifi module NOT connected\n");
+        }
+    } else {
+        printf("> ERROR : WIFI Module cannot be initialized.\n"); 
+    }
+    
+    while(1){      
+        char c[] = "";
+        int d = sensor.distance();
+        sprintf(c, "%d", d);
+        printf("Distance = %d \n", d);
+
+        char body[] = "{\"result\":";
+        char body_1[] = "}";
+        strcat(body,c);
+        strcat(body,body_1);
+
+        HttpRequest* POST_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(1)/Observations");
+        POST_request->set_header("Content-Type", "application/json");
+        HttpResponse* POST_response = POST_request->send(body, strlen(body));
+        printf("HTTP POST sending...\n");
+        // if response is NULL, check response->get_error()
+
+        printf("status is %d - %s\n", POST_response->get_status_code(), POST_response->get_status_message());
+        printf("body is:\n%s\n", POST_response->get_body_as_string().c_str());
+        
+        delete POST_request; // also clears out the response
+        
+                
+        HttpRequest* request = new HttpRequest(network, HTTP_GET, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations?$top=1&$orderby=phenomenonTime%20desc");
+        HttpResponse* response = request->send();
+        // if response is NULL, check response->get_error()
+
+//        printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
+//        printf("body is:\n%s\n", response->get_body_as_string().c_str());
+        
+        string JSON = response->get_body_as_string();
+        string LOCK = "LOCK";
+        string UNLOCK = "unlock";
+
+        if(strstr(JSON.c_str(),LOCK.c_str()) != NULL){
+            printf("LOCK\n");
+            servo.pulsewidth_us(700);
+/*            const char lock[] = "{\"result\":\"LOCK\"}";
+            HttpRequest* LOCK_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations");
+            LOCK_request->set_header("Content-Type", "application/json");
+            HttpResponse* LOCK_response = LOCK_request->send(lock, strlen(lock));
+            // if response is NULL, check response->get_error()
+
+            printf("status is %d - %s\n", LOCK_response->get_status_code(), LOCK_response->get_status_message());
+//            printf("body is:\n%s\n", LOCK_response->get_body_as_string().c_str());
+        
+            delete LOCK_request; // also clears out the response
+*/
+        }else if(strstr(JSON.c_str(),UNLOCK.c_str()) != NULL){
+            printf("UNLOCK\n");
+            servo.pulsewidth_us(2500);
+/*            const char unlock[] = "{\"result\":\"unlock\"}";
+            HttpRequest* UNLOCK_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations");
+            UNLOCK_request->set_header("Content-Type", "application/json");
+            HttpResponse* UNLOCK_response = UNLOCK_request->send(unlock, strlen(unlock));
+            // if response is NULL, check response->get_error()
+
+            printf("status is %d - %s\n", UNLOCK_response->get_status_code(), UNLOCK_response->get_status_message());
+//            printf("body is:\n%s\n", UNLOCK_response->get_body_as_string().c_str());
+        
+            delete UNLOCK_request; // also clears out the response
+*/
+        }
+      
+        delete request;
+        
+        printf("%s\n","END...");
+        wait(INTERVAL);
+    }
+}
+
+
+
+
+
+        
+
+
diff -r 000000000000 -r 8bcd728684c5 mbed-http.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-http.lib	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,1 @@
+https://developer.mbed.org/teams/sandbox/code/mbed-http/#fa4d71265625
diff -r 000000000000 -r 8bcd728684c5 mbed-os.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed-os.lib	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/mbed-os/#2fd0c5cfbd83fce62da6308f9d64c0ab64e1f0d6
diff -r 000000000000 -r 8bcd728684c5 mbed_app.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app.json	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,83 @@
+{
+    "config": {
+        "wifi-shield": {
+            "help": "Options are internal, WIFI_IDW0XX1",
+            "value": "internal"
+        },
+        "wifi-ssid": {
+            "help": "WiFi SSID",
+            "value": "\"EASON\""
+        },
+        "wifi-password": {
+            "help": "WiFi Password",
+            "value": "\"00000000\""
+        },
+        "wifi-tx": {
+            "help": "TX pin for serial connection to external device",
+            "value": "D1"
+        },
+        "wifi-rx": {
+            "help": "RX pin for serial connection to external device",
+            "value": "D0"
+        },
+        "wifi-spi_miso": {
+            "help": "SPI-MISO connection to external device",
+            "value": "PC_11"
+        },
+        "wifi-spi_mosi": {
+            "help": "SPI-MOSI connection to external device",
+            "value": "PC_12"
+        },
+        "wifi-spi_sclk": {
+            "help": "SPI-CLOCK connection to external device",
+            "value": "PC_10"
+        },
+        "wifi-spi_nss": {
+            "help": "SPI chip select of external device",
+            "value": "PE_0"
+        },
+        "wifi-reset": {
+            "help": "WIFI module reset pin",
+            "value": "PE_8"
+        },
+        "wifi-dataready": {
+            "help": "WIFI module data ready pin",
+            "value": "PE_1"
+        },
+        "wifi-wakeup": {
+            "help": "WIFI module wakeup pin",
+            "value": "PB_12"
+        }
+    },
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": true
+        },
+        "NUCLEO_L476RG": {
+            "wifi-tx": "D8",
+            "wifi-rx": "D2"
+        },
+        "NUCLEO_F401RE": {
+            "wifi-tx": "D8",
+            "wifi-rx": "D2"
+        },
+        "DISCO_L475VG_IOT1A": {
+            "wifi-spi_miso": "PC_11",
+            "wifi-spi_mosi": "PC_12",
+            "wifi-spi_sclk": "PC_10",
+            "wifi-spi_nss": "PE_0",
+            "wifi-reset": "PE_8",
+            "wifi-dataready": "PE_1",
+            "wifi-wakeup": "PB_12"
+        },
+        "DISCO_F413ZH": {
+            "wifi-spi_miso": "PB_4",
+            "wifi-spi_mosi": "PB_5",
+            "wifi-spi_sclk": "PB_12",
+            "wifi-spi_nss": "PG_11",
+            "wifi-reset": "PH_1",
+            "wifi-dataready": "PG_12",
+            "wifi-wakeup": "PB_15"
+        }
+    }
+}
diff -r 000000000000 -r 8bcd728684c5 mbed_app_ism43362.json
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed_app_ism43362.json	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,86 @@
+{
+    "config": {
+        "wifi-shield": {
+            "help": "Options are internal, WIFI_ESP8266, WIFI_IDW0XX1",
+            "value": "internal"
+        },
+        "wifi-ssid": {
+            "help": "WiFi SSID",
+            "value": "\"EASON\""
+        },
+        "wifi-password": {
+            "help": "WiFi Password",
+            "value": "\"00000000\""
+        },
+        "wifi-tx": {
+            "help": "TX pin for serial connection to external device",
+            "value": "D1"
+        },
+        "wifi-rx": {
+            "help": "RX pin for serial connection to external device",
+            "value": "D0"
+        },
+        "wifi-spi_miso": {
+            "help": "SPI-MISO connection to external device",
+            "value": "PC_11"
+        },
+        "wifi-spi_mosi": {
+            "help": "SPI-MOSI connection to external device",
+            "value": "PC_12"
+        },
+        "wifi-spi_sclk": {
+            "help": "SPI-CLOCK connection to external device",
+            "value": "PC_10"
+        },
+        "wifi-spi_nss": {
+            "help": "SPI chip select of external device",
+            "value": "PE_0"
+        },
+        "wifi-reset": {
+            "help": "WIFI module reset pin",
+            "value": "PE_8"
+        },
+        "wifi-dataready": {
+            "help": "WIFI module data ready pin",
+            "value": "PE_1"
+        },
+        "wifi-wakeup": {
+            "help": "WIFI module wakeup pin",
+            "value": "PB_12"
+        }
+    },
+    "target_overrides": {
+        "*": {
+            "platform.stdio-convert-newlines": true
+        },
+        "UBLOX_EVK_ODIN_W2": {
+            "target.device_has": ["EMAC"]
+        },
+        "NUCLEO_L476RG": {
+            "wifi-tx": "D8",
+            "wifi-rx": "D2"
+        },
+        "NUCLEO_F401RE": {
+            "wifi-tx": "D8",
+            "wifi-rx": "D2"
+        },
+        "DISCO_L475VG_IOT1A": {
+            "wifi-spi_miso": "PC_11",
+            "wifi-spi_mosi": "PC_12",
+            "wifi-spi_sclk": "PC_10",
+            "wifi-spi_nss": "PE_0",
+            "wifi-reset": "PE_8",
+            "wifi-dataready": "PE_1",
+            "wifi-wakeup": "PB_12"
+        },
+        "DISCO_F413ZH": {
+            "wifi-spi_miso": "PB_4",
+            "wifi-spi_mosi": "PB_5",
+            "wifi-spi_sclk": "PB_12",
+            "wifi-spi_nss": "PG_11",
+            "wifi-reset": "PH_1",
+            "wifi-dataready": "PG_12",
+            "wifi-wakeup": "PB_15"
+        }
+    }
+}
diff -r 000000000000 -r 8bcd728684c5 wifi-ism43362.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/wifi-ism43362.lib	Mon Jan 07 11:19:49 2019 +0000
@@ -0,0 +1,1 @@
+https://github.com/ARMmbed/wifi-ism43362/#e4ecc27e87d96072f7df62a25ef007986dc95c4e