project
Dependencies: MbedJSONValue DISCO_L475VG_IOT01A_wifi mbed-http
main.cpp
00001 #include "mbed.h" 00002 #include "wifi.h" 00003 #include "US100.h" 00004 #include "NetworkInterface.h" 00005 #include "ISM43362Interface.h" 00006 #include "http_request.h" 00007 #include "ServoMove.h" 00008 #include <string> 00009 00010 /* Private defines -----------------------------------------------------------*/ 00011 #define WIFI_WRITE_TIMEOUT 10000 00012 #define WIFI_READ_TIMEOUT 10000 00013 #define CONNECTION_TRIAL_MAX 10 00014 00015 /* Private typedef------------------------------------------------------------*/ 00016 /* Private macro -------------------------------------------------------------*/ 00017 /* Private variables ---------------------------------------------------------*/ 00018 Serial pc(SERIAL_TX, SERIAL_RX); 00019 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); 00020 uint8_t MAC_Addr[6]; 00021 uint8_t IP_Addr[4]; 00022 00023 // Interval(second) to do tasking &sensing 00024 const int INTERVAL = 1; 00025 // Distance sensor 00026 US100 sensor(PC_3, PC_4); 00027 // Select network interface 00028 NetworkInterface* network = &wifi; 00029 00030 int main() 00031 { 00032 00033 pc.baud(9600); 00034 00035 printf("\n"); 00036 printf("************************************************************\n"); 00037 printf("*** IoT Final Project - TSENG, I-SHENG ***\n"); 00038 printf("************************************************************\n"); 00039 00040 /*Initialize WIFI module */ 00041 if(WIFI_Init() == WIFI_STATUS_OK) { 00042 printf("> WIFI Module Initialized.\n"); 00043 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) { 00044 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n", 00045 MAC_Addr[0], 00046 MAC_Addr[1], 00047 MAC_Addr[2], 00048 MAC_Addr[3], 00049 MAC_Addr[4], 00050 MAC_Addr[5]); 00051 } else { 00052 printf("> ERROR : CANNOT get MAC address\n"); 00053 } 00054 00055 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) { 00056 printf("> es-wifi module connected \n"); 00057 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) { 00058 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n", 00059 IP_Addr[0], 00060 IP_Addr[1], 00061 IP_Addr[2], 00062 IP_Addr[3]); 00063 } else { 00064 printf("> ERROR : es-wifi module CANNOT get IP address\n"); 00065 } 00066 } else { 00067 printf("> ERROR : es-wifi module NOT connected\n"); 00068 } 00069 } else { 00070 printf("> ERROR : WIFI Module cannot be initialized.\n"); 00071 } 00072 00073 while(1){ 00074 char c[] = ""; 00075 int d = sensor.distance(); 00076 sprintf(c, "%d", d); 00077 printf("Distance = %d \n", d); 00078 00079 char body[] = "{\"result\":"; 00080 char body_1[] = "}"; 00081 strcat(body,c); 00082 strcat(body,body_1); 00083 00084 HttpRequest* POST_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(1)/Observations"); 00085 POST_request->set_header("Content-Type", "application/json"); 00086 HttpResponse* POST_response = POST_request->send(body, strlen(body)); 00087 printf("HTTP POST sending...\n"); 00088 // if response is NULL, check response->get_error() 00089 00090 printf("status is %d - %s\n", POST_response->get_status_code(), POST_response->get_status_message()); 00091 printf("body is:\n%s\n", POST_response->get_body_as_string().c_str()); 00092 00093 delete POST_request; // also clears out the response 00094 00095 00096 HttpRequest* request = new HttpRequest(network, HTTP_GET, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations?$top=1&$orderby=phenomenonTime%20desc"); 00097 HttpResponse* response = request->send(); 00098 // if response is NULL, check response->get_error() 00099 00100 // printf("status is %d - %s\n", response->get_status_code(), response->get_status_message()); 00101 // printf("body is:\n%s\n", response->get_body_as_string().c_str()); 00102 00103 string JSON = response->get_body_as_string(); 00104 string LOCK = "LOCK"; 00105 string UNLOCK = "unlock"; 00106 00107 if(strstr(JSON.c_str(),LOCK.c_str()) != NULL){ 00108 printf("LOCK\n"); 00109 servo.pulsewidth_us(700); 00110 /* const char lock[] = "{\"result\":\"LOCK\"}"; 00111 HttpRequest* LOCK_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations"); 00112 LOCK_request->set_header("Content-Type", "application/json"); 00113 HttpResponse* LOCK_response = LOCK_request->send(lock, strlen(lock)); 00114 // if response is NULL, check response->get_error() 00115 00116 printf("status is %d - %s\n", LOCK_response->get_status_code(), LOCK_response->get_status_message()); 00117 // printf("body is:\n%s\n", LOCK_response->get_body_as_string().c_str()); 00118 00119 delete LOCK_request; // also clears out the response 00120 */ 00121 }else if(strstr(JSON.c_str(),UNLOCK.c_str()) != NULL){ 00122 printf("UNLOCK\n"); 00123 servo.pulsewidth_us(2500); 00124 /* const char unlock[] = "{\"result\":\"unlock\"}"; 00125 HttpRequest* UNLOCK_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(2)/Observations"); 00126 UNLOCK_request->set_header("Content-Type", "application/json"); 00127 HttpResponse* UNLOCK_response = UNLOCK_request->send(unlock, strlen(unlock)); 00128 // if response is NULL, check response->get_error() 00129 00130 printf("status is %d - %s\n", UNLOCK_response->get_status_code(), UNLOCK_response->get_status_message()); 00131 // printf("body is:\n%s\n", UNLOCK_response->get_body_as_string().c_str()); 00132 00133 delete UNLOCK_request; // also clears out the response 00134 */ 00135 } 00136 00137 delete request; 00138 00139 printf("%s\n","END..."); 00140 wait(INTERVAL); 00141 } 00142 } 00143 00144 00145 00146 00147 00148 00149 00150
Generated on Tue Jul 12 2022 18:25:21 by
1.7.2