test

Dependencies:   MbedJSONValue DISCO_L475VG_IOT01A_wifi mbed-http

main.cpp

Committer:
Eason0505
Date:
2019-01-01
Revision:
0:065178c282bd

File content as of revision 0:065178c282bd:

#include "mbed.h"
#include "wifi.h"
#include "US100.h"
#include "NetworkInterface.h"
#include "ISM43362Interface.h"
#include "http_request.h"
#include "MbedJSONValue.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];

// RoLa 
DigitalOut M0(PD_14);
DigitalOut M1(PB_0);
Serial uart(PA_0, PA_1);//TX4,RX4
string inputdata;
double cnv;

// 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* 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", response->get_status_code(), response->get_status_message());
        printf("body is:\n%s\n", response->get_body_as_string().c_str());

        delete POST_request; // also clears out the response
        wait(INTERVAL);
    }
}