project
Dependencies: MbedJSONValue DISCO_L475VG_IOT01A_wifi mbed-http
main.cpp@0:8bcd728684c5, 2019-01-07 (annotated)
- Committer:
- Eason0505
- Date:
- Mon Jan 07 11:19:49 2019 +0000
- Revision:
- 0:8bcd728684c5
IoT Project code
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Eason0505 | 0:8bcd728684c5 | 1 | #include "mbed.h" |
Eason0505 | 0:8bcd728684c5 | 2 | #include "wifi.h" |
Eason0505 | 0:8bcd728684c5 | 3 | #include "US100.h" |
Eason0505 | 0:8bcd728684c5 | 4 | #include "NetworkInterface.h" |
Eason0505 | 0:8bcd728684c5 | 5 | #include "ISM43362Interface.h" |
Eason0505 | 0:8bcd728684c5 | 6 | #include "http_request.h" |
Eason0505 | 0:8bcd728684c5 | 7 | #include "ServoMove.h" |
Eason0505 | 0:8bcd728684c5 | 8 | #include <string> |
Eason0505 | 0:8bcd728684c5 | 9 | |
Eason0505 | 0:8bcd728684c5 | 10 | /* Private defines -----------------------------------------------------------*/ |
Eason0505 | 0:8bcd728684c5 | 11 | #define WIFI_WRITE_TIMEOUT 10000 |
Eason0505 | 0:8bcd728684c5 | 12 | #define WIFI_READ_TIMEOUT 10000 |
Eason0505 | 0:8bcd728684c5 | 13 | #define CONNECTION_TRIAL_MAX 10 |
Eason0505 | 0:8bcd728684c5 | 14 | |
Eason0505 | 0:8bcd728684c5 | 15 | /* Private typedef------------------------------------------------------------*/ |
Eason0505 | 0:8bcd728684c5 | 16 | /* Private macro -------------------------------------------------------------*/ |
Eason0505 | 0:8bcd728684c5 | 17 | /* Private variables ---------------------------------------------------------*/ |
Eason0505 | 0:8bcd728684c5 | 18 | Serial pc(SERIAL_TX, SERIAL_RX); |
Eason0505 | 0:8bcd728684c5 | 19 | 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); |
Eason0505 | 0:8bcd728684c5 | 20 | uint8_t MAC_Addr[6]; |
Eason0505 | 0:8bcd728684c5 | 21 | uint8_t IP_Addr[4]; |
Eason0505 | 0:8bcd728684c5 | 22 | |
Eason0505 | 0:8bcd728684c5 | 23 | // Interval(second) to do tasking &sensing |
Eason0505 | 0:8bcd728684c5 | 24 | const int INTERVAL = 1; |
Eason0505 | 0:8bcd728684c5 | 25 | // Distance sensor |
Eason0505 | 0:8bcd728684c5 | 26 | US100 sensor(PC_3, PC_4); |
Eason0505 | 0:8bcd728684c5 | 27 | // Select network interface |
Eason0505 | 0:8bcd728684c5 | 28 | NetworkInterface* network = &wifi; |
Eason0505 | 0:8bcd728684c5 | 29 | |
Eason0505 | 0:8bcd728684c5 | 30 | int main() |
Eason0505 | 0:8bcd728684c5 | 31 | { |
Eason0505 | 0:8bcd728684c5 | 32 | |
Eason0505 | 0:8bcd728684c5 | 33 | pc.baud(9600); |
Eason0505 | 0:8bcd728684c5 | 34 | |
Eason0505 | 0:8bcd728684c5 | 35 | printf("\n"); |
Eason0505 | 0:8bcd728684c5 | 36 | printf("************************************************************\n"); |
Eason0505 | 0:8bcd728684c5 | 37 | printf("*** IoT Final Project - TSENG, I-SHENG ***\n"); |
Eason0505 | 0:8bcd728684c5 | 38 | printf("************************************************************\n"); |
Eason0505 | 0:8bcd728684c5 | 39 | |
Eason0505 | 0:8bcd728684c5 | 40 | /*Initialize WIFI module */ |
Eason0505 | 0:8bcd728684c5 | 41 | if(WIFI_Init() == WIFI_STATUS_OK) { |
Eason0505 | 0:8bcd728684c5 | 42 | printf("> WIFI Module Initialized.\n"); |
Eason0505 | 0:8bcd728684c5 | 43 | if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { |
Eason0505 | 0:8bcd728684c5 | 44 | printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", |
Eason0505 | 0:8bcd728684c5 | 45 | MAC_Addr[0], |
Eason0505 | 0:8bcd728684c5 | 46 | MAC_Addr[1], |
Eason0505 | 0:8bcd728684c5 | 47 | MAC_Addr[2], |
Eason0505 | 0:8bcd728684c5 | 48 | MAC_Addr[3], |
Eason0505 | 0:8bcd728684c5 | 49 | MAC_Addr[4], |
Eason0505 | 0:8bcd728684c5 | 50 | MAC_Addr[5]); |
Eason0505 | 0:8bcd728684c5 | 51 | } else { |
Eason0505 | 0:8bcd728684c5 | 52 | printf("> ERROR : CANNOT get MAC address\n"); |
Eason0505 | 0:8bcd728684c5 | 53 | } |
Eason0505 | 0:8bcd728684c5 | 54 | |
Eason0505 | 0:8bcd728684c5 | 55 | if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { |
Eason0505 | 0:8bcd728684c5 | 56 | printf("> es-wifi module connected \n"); |
Eason0505 | 0:8bcd728684c5 | 57 | if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { |
Eason0505 | 0:8bcd728684c5 | 58 | printf("> es-wifi module got IP Address : %d.%d.%d.%d\n", |
Eason0505 | 0:8bcd728684c5 | 59 | IP_Addr[0], |
Eason0505 | 0:8bcd728684c5 | 60 | IP_Addr[1], |
Eason0505 | 0:8bcd728684c5 | 61 | IP_Addr[2], |
Eason0505 | 0:8bcd728684c5 | 62 | IP_Addr[3]); |
Eason0505 | 0:8bcd728684c5 | 63 | } else { |
Eason0505 | 0:8bcd728684c5 | 64 | printf("> ERROR : es-wifi module CANNOT get IP address\n"); |
Eason0505 | 0:8bcd728684c5 | 65 | } |
Eason0505 | 0:8bcd728684c5 | 66 | } else { |
Eason0505 | 0:8bcd728684c5 | 67 | printf("> ERROR : es-wifi module NOT connected\n"); |
Eason0505 | 0:8bcd728684c5 | 68 | } |
Eason0505 | 0:8bcd728684c5 | 69 | } else { |
Eason0505 | 0:8bcd728684c5 | 70 | printf("> ERROR : WIFI Module cannot be initialized.\n"); |
Eason0505 | 0:8bcd728684c5 | 71 | } |
Eason0505 | 0:8bcd728684c5 | 72 | |
Eason0505 | 0:8bcd728684c5 | 73 | while(1){ |
Eason0505 | 0:8bcd728684c5 | 74 | char c[] = ""; |
Eason0505 | 0:8bcd728684c5 | 75 | int d = sensor.distance(); |
Eason0505 | 0:8bcd728684c5 | 76 | sprintf(c, "%d", d); |
Eason0505 | 0:8bcd728684c5 | 77 | printf("Distance = %d \n", d); |
Eason0505 | 0:8bcd728684c5 | 78 | |
Eason0505 | 0:8bcd728684c5 | 79 | char body[] = "{\"result\":"; |
Eason0505 | 0:8bcd728684c5 | 80 | char body_1[] = "}"; |
Eason0505 | 0:8bcd728684c5 | 81 | strcat(body,c); |
Eason0505 | 0:8bcd728684c5 | 82 | strcat(body,body_1); |
Eason0505 | 0:8bcd728684c5 | 83 | |
Eason0505 | 0:8bcd728684c5 | 84 | HttpRequest* POST_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(1)/Observations"); |
Eason0505 | 0:8bcd728684c5 | 85 | POST_request->set_header("Content-Type", "application/json"); |
Eason0505 | 0:8bcd728684c5 | 86 | HttpResponse* POST_response = POST_request->send(body, strlen(body)); |
Eason0505 | 0:8bcd728684c5 | 87 | printf("HTTP POST sending...\n"); |
Eason0505 | 0:8bcd728684c5 | 88 | // if response is NULL, check response->get_error() |
Eason0505 | 0:8bcd728684c5 | 89 | |
Eason0505 | 0:8bcd728684c5 | 90 | printf("status is %d - %s\n", POST_response->get_status_code(), POST_response->get_status_message()); |
Eason0505 | 0:8bcd728684c5 | 91 | printf("body is:\n%s\n", POST_response->get_body_as_string().c_str()); |
Eason0505 | 0:8bcd728684c5 | 92 | |
Eason0505 | 0:8bcd728684c5 | 93 | delete POST_request; // also clears out the response |
Eason0505 | 0:8bcd728684c5 | 94 | |
Eason0505 | 0:8bcd728684c5 | 95 | |
Eason0505 | 0:8bcd728684c5 | 96 | HttpRequest* request = new HttpRequest(network, HTTP_GET, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations?$top=1&$orderby=phenomenonTime%20desc"); |
Eason0505 | 0:8bcd728684c5 | 97 | HttpResponse* response = request->send(); |
Eason0505 | 0:8bcd728684c5 | 98 | // if response is NULL, check response->get_error() |
Eason0505 | 0:8bcd728684c5 | 99 | |
Eason0505 | 0:8bcd728684c5 | 100 | // printf("status is %d - %s\n", response->get_status_code(), response->get_status_message()); |
Eason0505 | 0:8bcd728684c5 | 101 | // printf("body is:\n%s\n", response->get_body_as_string().c_str()); |
Eason0505 | 0:8bcd728684c5 | 102 | |
Eason0505 | 0:8bcd728684c5 | 103 | string JSON = response->get_body_as_string(); |
Eason0505 | 0:8bcd728684c5 | 104 | string LOCK = "LOCK"; |
Eason0505 | 0:8bcd728684c5 | 105 | string UNLOCK = "unlock"; |
Eason0505 | 0:8bcd728684c5 | 106 | |
Eason0505 | 0:8bcd728684c5 | 107 | if(strstr(JSON.c_str(),LOCK.c_str()) != NULL){ |
Eason0505 | 0:8bcd728684c5 | 108 | printf("LOCK\n"); |
Eason0505 | 0:8bcd728684c5 | 109 | servo.pulsewidth_us(700); |
Eason0505 | 0:8bcd728684c5 | 110 | /* const char lock[] = "{\"result\":\"LOCK\"}"; |
Eason0505 | 0:8bcd728684c5 | 111 | HttpRequest* LOCK_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations"); |
Eason0505 | 0:8bcd728684c5 | 112 | LOCK_request->set_header("Content-Type", "application/json"); |
Eason0505 | 0:8bcd728684c5 | 113 | HttpResponse* LOCK_response = LOCK_request->send(lock, strlen(lock)); |
Eason0505 | 0:8bcd728684c5 | 114 | // if response is NULL, check response->get_error() |
Eason0505 | 0:8bcd728684c5 | 115 | |
Eason0505 | 0:8bcd728684c5 | 116 | printf("status is %d - %s\n", LOCK_response->get_status_code(), LOCK_response->get_status_message()); |
Eason0505 | 0:8bcd728684c5 | 117 | // printf("body is:\n%s\n", LOCK_response->get_body_as_string().c_str()); |
Eason0505 | 0:8bcd728684c5 | 118 | |
Eason0505 | 0:8bcd728684c5 | 119 | delete LOCK_request; // also clears out the response |
Eason0505 | 0:8bcd728684c5 | 120 | */ |
Eason0505 | 0:8bcd728684c5 | 121 | }else if(strstr(JSON.c_str(),UNLOCK.c_str()) != NULL){ |
Eason0505 | 0:8bcd728684c5 | 122 | printf("UNLOCK\n"); |
Eason0505 | 0:8bcd728684c5 | 123 | servo.pulsewidth_us(2500); |
Eason0505 | 0:8bcd728684c5 | 124 | /* const char unlock[] = "{\"result\":\"unlock\"}"; |
Eason0505 | 0:8bcd728684c5 | 125 | HttpRequest* UNLOCK_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations"); |
Eason0505 | 0:8bcd728684c5 | 126 | UNLOCK_request->set_header("Content-Type", "application/json"); |
Eason0505 | 0:8bcd728684c5 | 127 | HttpResponse* UNLOCK_response = UNLOCK_request->send(unlock, strlen(unlock)); |
Eason0505 | 0:8bcd728684c5 | 128 | // if response is NULL, check response->get_error() |
Eason0505 | 0:8bcd728684c5 | 129 | |
Eason0505 | 0:8bcd728684c5 | 130 | printf("status is %d - %s\n", UNLOCK_response->get_status_code(), UNLOCK_response->get_status_message()); |
Eason0505 | 0:8bcd728684c5 | 131 | // printf("body is:\n%s\n", UNLOCK_response->get_body_as_string().c_str()); |
Eason0505 | 0:8bcd728684c5 | 132 | |
Eason0505 | 0:8bcd728684c5 | 133 | delete UNLOCK_request; // also clears out the response |
Eason0505 | 0:8bcd728684c5 | 134 | */ |
Eason0505 | 0:8bcd728684c5 | 135 | } |
Eason0505 | 0:8bcd728684c5 | 136 | |
Eason0505 | 0:8bcd728684c5 | 137 | delete request; |
Eason0505 | 0:8bcd728684c5 | 138 | |
Eason0505 | 0:8bcd728684c5 | 139 | printf("%s\n","END..."); |
Eason0505 | 0:8bcd728684c5 | 140 | wait(INTERVAL); |
Eason0505 | 0:8bcd728684c5 | 141 | } |
Eason0505 | 0:8bcd728684c5 | 142 | } |
Eason0505 | 0:8bcd728684c5 | 143 | |
Eason0505 | 0:8bcd728684c5 | 144 | |
Eason0505 | 0:8bcd728684c5 | 145 | |
Eason0505 | 0:8bcd728684c5 | 146 | |
Eason0505 | 0:8bcd728684c5 | 147 | |
Eason0505 | 0:8bcd728684c5 | 148 | |
Eason0505 | 0:8bcd728684c5 | 149 | |
Eason0505 | 0:8bcd728684c5 | 150 |