test

Dependencies:   MbedJSONValue DISCO_L475VG_IOT01A_wifi mbed-http

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

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 "MbedJSONValue.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 // RoLa 
00024 DigitalOut M0(PD_14);
00025 DigitalOut M1(PB_0);
00026 Serial uart(PA_0, PA_1);//TX4,RX4
00027 string inputdata;
00028 double cnv;
00029 
00030 // Interval(second) to do tasking &sensing
00031 const int INTERVAL  = 1;
00032 // Distance sensor
00033 US100 sensor(PC_3, PC_4);
00034 // Select network interface 
00035 NetworkInterface* network = &wifi;     
00036 
00037 int main()
00038 {
00039 
00040     pc.baud(9600);
00041 
00042     printf("\n");
00043     printf("************************************************************\n");
00044     printf("***          IoT Final Project - TSENG, I-SHENG          ***\n");
00045     printf("************************************************************\n");
00046 
00047     /*Initialize  WIFI module */
00048     if(WIFI_Init() ==  WIFI_STATUS_OK) {
00049         printf("> WIFI Module Initialized.\n");  
00050         if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
00051             printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",     
00052                    MAC_Addr[0],
00053                    MAC_Addr[1],
00054                    MAC_Addr[2],
00055                    MAC_Addr[3],
00056                    MAC_Addr[4],
00057                    MAC_Addr[5]);   
00058         } else {
00059             printf("> ERROR : CANNOT get MAC address\n");
00060         }
00061     
00062         if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
00063             printf("> es-wifi module connected \n");
00064             if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
00065                 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",     
00066                        IP_Addr[0],
00067                        IP_Addr[1],
00068                        IP_Addr[2],
00069                        IP_Addr[3]); 
00070             } else {
00071                 printf("> ERROR : es-wifi module CANNOT get IP address\n");
00072             }
00073         } else {
00074             printf("> ERROR : es-wifi module NOT connected\n");
00075         }
00076     } else {
00077         printf("> ERROR : WIFI Module cannot be initialized.\n"); 
00078     }
00079     while(1){      
00080         char c[] = "";
00081         int d = sensor.distance();
00082         sprintf(c, "%d", d);
00083         printf("Distance = %d \n", d);
00084 
00085         char body[] = "{\"result\":";
00086         char body_1[] = "}";
00087         strcat(body,c);
00088         strcat(body,body_1);
00089 
00090         HttpRequest* POST_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(1)/Observations");
00091         POST_request->set_header("Content-Type", "application/json");
00092         HttpResponse* response = POST_request->send(body, strlen(body));
00093         printf("HTTP POST sending...\n");
00094         // if response is NULL, check response->get_error()
00095 
00096         printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
00097         printf("body is:\n%s\n", response->get_body_as_string().c_str());
00098 
00099         delete POST_request; // also clears out the response
00100         wait(INTERVAL);
00101     }
00102 }