test

Dependencies:   MbedJSONValue DISCO_L475VG_IOT01A_wifi mbed-http

Committer:
Eason0505
Date:
Tue Jan 01 11:48:52 2019 +0000
Revision:
0:065178c282bd
test

Who changed what in which revision?

UserRevisionLine numberNew contents of line
Eason0505 0:065178c282bd 1 #include "mbed.h"
Eason0505 0:065178c282bd 2 #include "wifi.h"
Eason0505 0:065178c282bd 3 #include "US100.h"
Eason0505 0:065178c282bd 4 #include "NetworkInterface.h"
Eason0505 0:065178c282bd 5 #include "ISM43362Interface.h"
Eason0505 0:065178c282bd 6 #include "http_request.h"
Eason0505 0:065178c282bd 7 #include "MbedJSONValue.h"
Eason0505 0:065178c282bd 8 #include <string>
Eason0505 0:065178c282bd 9
Eason0505 0:065178c282bd 10 /* Private defines -----------------------------------------------------------*/
Eason0505 0:065178c282bd 11 #define WIFI_WRITE_TIMEOUT 10000
Eason0505 0:065178c282bd 12 #define WIFI_READ_TIMEOUT 10000
Eason0505 0:065178c282bd 13 #define CONNECTION_TRIAL_MAX 10
Eason0505 0:065178c282bd 14
Eason0505 0:065178c282bd 15 /* Private typedef------------------------------------------------------------*/
Eason0505 0:065178c282bd 16 /* Private macro -------------------------------------------------------------*/
Eason0505 0:065178c282bd 17 /* Private variables ---------------------------------------------------------*/
Eason0505 0:065178c282bd 18 Serial pc(SERIAL_TX, SERIAL_RX);
Eason0505 0:065178c282bd 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:065178c282bd 20 uint8_t MAC_Addr[6];
Eason0505 0:065178c282bd 21 uint8_t IP_Addr[4];
Eason0505 0:065178c282bd 22
Eason0505 0:065178c282bd 23 // RoLa
Eason0505 0:065178c282bd 24 DigitalOut M0(PD_14);
Eason0505 0:065178c282bd 25 DigitalOut M1(PB_0);
Eason0505 0:065178c282bd 26 Serial uart(PA_0, PA_1);//TX4,RX4
Eason0505 0:065178c282bd 27 string inputdata;
Eason0505 0:065178c282bd 28 double cnv;
Eason0505 0:065178c282bd 29
Eason0505 0:065178c282bd 30 // Interval(second) to do tasking &sensing
Eason0505 0:065178c282bd 31 const int INTERVAL = 1;
Eason0505 0:065178c282bd 32 // Distance sensor
Eason0505 0:065178c282bd 33 US100 sensor(PC_3, PC_4);
Eason0505 0:065178c282bd 34 // Select network interface
Eason0505 0:065178c282bd 35 NetworkInterface* network = &wifi;
Eason0505 0:065178c282bd 36
Eason0505 0:065178c282bd 37 int main()
Eason0505 0:065178c282bd 38 {
Eason0505 0:065178c282bd 39
Eason0505 0:065178c282bd 40 pc.baud(9600);
Eason0505 0:065178c282bd 41
Eason0505 0:065178c282bd 42 printf("\n");
Eason0505 0:065178c282bd 43 printf("************************************************************\n");
Eason0505 0:065178c282bd 44 printf("*** IoT Final Project - TSENG, I-SHENG ***\n");
Eason0505 0:065178c282bd 45 printf("************************************************************\n");
Eason0505 0:065178c282bd 46
Eason0505 0:065178c282bd 47 /*Initialize WIFI module */
Eason0505 0:065178c282bd 48 if(WIFI_Init() == WIFI_STATUS_OK) {
Eason0505 0:065178c282bd 49 printf("> WIFI Module Initialized.\n");
Eason0505 0:065178c282bd 50 if(WIFI_GetMAC_Address(MAC_Addr) == WIFI_STATUS_OK) {
Eason0505 0:065178c282bd 51 printf("> es-wifi module MAC Address : %X:%X:%X:%X:%X:%X\n",
Eason0505 0:065178c282bd 52 MAC_Addr[0],
Eason0505 0:065178c282bd 53 MAC_Addr[1],
Eason0505 0:065178c282bd 54 MAC_Addr[2],
Eason0505 0:065178c282bd 55 MAC_Addr[3],
Eason0505 0:065178c282bd 56 MAC_Addr[4],
Eason0505 0:065178c282bd 57 MAC_Addr[5]);
Eason0505 0:065178c282bd 58 } else {
Eason0505 0:065178c282bd 59 printf("> ERROR : CANNOT get MAC address\n");
Eason0505 0:065178c282bd 60 }
Eason0505 0:065178c282bd 61
Eason0505 0:065178c282bd 62 if( WIFI_Connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, WIFI_ECN_WPA2_PSK) == WIFI_STATUS_OK) {
Eason0505 0:065178c282bd 63 printf("> es-wifi module connected \n");
Eason0505 0:065178c282bd 64 if(WIFI_GetIP_Address(IP_Addr) == WIFI_STATUS_OK) {
Eason0505 0:065178c282bd 65 printf("> es-wifi module got IP Address : %d.%d.%d.%d\n",
Eason0505 0:065178c282bd 66 IP_Addr[0],
Eason0505 0:065178c282bd 67 IP_Addr[1],
Eason0505 0:065178c282bd 68 IP_Addr[2],
Eason0505 0:065178c282bd 69 IP_Addr[3]);
Eason0505 0:065178c282bd 70 } else {
Eason0505 0:065178c282bd 71 printf("> ERROR : es-wifi module CANNOT get IP address\n");
Eason0505 0:065178c282bd 72 }
Eason0505 0:065178c282bd 73 } else {
Eason0505 0:065178c282bd 74 printf("> ERROR : es-wifi module NOT connected\n");
Eason0505 0:065178c282bd 75 }
Eason0505 0:065178c282bd 76 } else {
Eason0505 0:065178c282bd 77 printf("> ERROR : WIFI Module cannot be initialized.\n");
Eason0505 0:065178c282bd 78 }
Eason0505 0:065178c282bd 79 while(1){
Eason0505 0:065178c282bd 80 char c[] = "";
Eason0505 0:065178c282bd 81 int d = sensor.distance();
Eason0505 0:065178c282bd 82 sprintf(c, "%d", d);
Eason0505 0:065178c282bd 83 printf("Distance = %d \n", d);
Eason0505 0:065178c282bd 84
Eason0505 0:065178c282bd 85 char body[] = "{\"result\":";
Eason0505 0:065178c282bd 86 char body_1[] = "}";
Eason0505 0:065178c282bd 87 strcat(body,c);
Eason0505 0:065178c282bd 88 strcat(body,body_1);
Eason0505 0:065178c282bd 89
Eason0505 0:065178c282bd 90 HttpRequest* POST_request = new HttpRequest(network, HTTP_POST, "http://140.115.111.187:8080/Ray/v1.0/Datastreams(1)/Observations");
Eason0505 0:065178c282bd 91 POST_request->set_header("Content-Type", "application/json");
Eason0505 0:065178c282bd 92 HttpResponse* response = POST_request->send(body, strlen(body));
Eason0505 0:065178c282bd 93 printf("HTTP POST sending...\n");
Eason0505 0:065178c282bd 94 // if response is NULL, check response->get_error()
Eason0505 0:065178c282bd 95
Eason0505 0:065178c282bd 96 printf("status is %d - %s\n", response->get_status_code(), response->get_status_message());
Eason0505 0:065178c282bd 97 printf("body is:\n%s\n", response->get_body_as_string().c_str());
Eason0505 0:065178c282bd 98
Eason0505 0:065178c282bd 99 delete POST_request; // also clears out the response
Eason0505 0:065178c282bd 100 wait(INTERVAL);
Eason0505 0:065178c282bd 101 }
Eason0505 0:065178c282bd 102 }