The easy-connect library for GR-PEACH.

Dependencies:   LWIPBP3595Interface_STA_for_mbed-os

Dependents:   GR-PEACH_HVC-P2_sample_client GR-PEACH_HVC-P2_IoTPlatform_http GR-PEACH_IoT_Platform_HTTP_sample

Specifying connectivity method

Add the following to your mbed_app.json file:

mbed_app.json

{
    "config": {
        "network-interface":{
            "help": "Options are ETHERNET, WIFI_ESP8266, WIFI_BP3595",
            "value": "ETHERNET"
        }
    }
}

To specify MAC address, add fllowing function to main.cpp. (When using Wifi, setting of MAC address is not necessary.)

Specify MAC address

// set mac address
void mbed_mac_address(char *mac) {
    mac[0] = 0x00;
    mac[1] = 0x02;
    mac[2] = 0xF7;
    mac[3] = 0xF0;
    mac[4] = 0x00;
    mac[5] = 0x00;
}


Wifi settings

If you choose BP3595, you'll also need to add the WiFi SSID, password and security type:

mbed_app.json

{
    "config": {
        "network-interface":{
            "help": "Options are ETHERNET, WIFI_ESP8266, WIFI_BP3595",
            "value": "WIFI_BP3595"
        },
        "wifi-ssid": {
            "help": "WiFi SSID",
            "value": "\"SSID\""
        },
        "wifi-password": {
            "help": "WIFI Password",
            "value": "\"Password\""
        },
        "wifi-security":{
            "help": "Options are NSAPI_SECURITY_WEP, NSAPI_SECURITY_WPA, NSAPI_SECURITY_WPA2, NSAPI_SECURITY_WPA_WPA2",
            "value": "NSAPI_SECURITY_WPA_WPA2"
        }
    }
}


Using Easy Connect from your application

Easy Connect has just one function which will either return a NetworkInterface-pointer or NULL:

main.cpp

#include "easy-connect.h"
 
int main(int, char**) {
    NetworkInterface* network = easy_connect(true); /* has 1 argument, enable_logging (pass in true to log to serial port) */
    if (!network) {
        printf("Connecting to the network failed... See serial output.\r\n");
        return 1;
    }
 
    // Rest of your program
}
Committer:
dkato
Date:
Wed Mar 08 07:17:06 2017 +0000
Revision:
0:77c289709567
Child:
1:adf177867e43
Delete WIFI_ODIN, MESH_LOWPAN_ND and MESH_THREAD.
; Added WIFI_BP3595.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
dkato 0:77c289709567 1 #ifndef __MAGIC_CONNECT_H__
dkato 0:77c289709567 2 #define __MAGIC_CONNECT_H__
dkato 0:77c289709567 3
dkato 0:77c289709567 4 #include "mbed.h"
dkato 0:77c289709567 5
dkato 0:77c289709567 6 #define ETHERNET 1
dkato 0:77c289709567 7 #define WIFI_ESP8266 2
dkato 0:77c289709567 8 #define WIFI_BP3595 3
dkato 0:77c289709567 9
dkato 0:77c289709567 10 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
dkato 0:77c289709567 11 #include "ESP8266Interface.h"
dkato 0:77c289709567 12
dkato 0:77c289709567 13 #ifdef MBED_CONF_APP_ESP8266_DEBUG
dkato 0:77c289709567 14 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
dkato 0:77c289709567 15 #else
dkato 0:77c289709567 16 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
dkato 0:77c289709567 17 #endif
dkato 0:77c289709567 18
dkato 0:77c289709567 19 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595
dkato 0:77c289709567 20 #include "LWIPBP3595Interface.h"
dkato 0:77c289709567 21 LWIPBP3595Interface wifi;
dkato 0:77c289709567 22 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
dkato 0:77c289709567 23 #include "EthernetInterface.h"
dkato 0:77c289709567 24 EthernetInterface eth;
dkato 0:77c289709567 25 #else
dkato 0:77c289709567 26 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
dkato 0:77c289709567 27 #endif
dkato 0:77c289709567 28
dkato 0:77c289709567 29 // This is address to mbed Device Connector
dkato 0:77c289709567 30 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
dkato 0:77c289709567 31
dkato 0:77c289709567 32 #ifdef MBED_CONF_APP_ESP8266_SSID
dkato 0:77c289709567 33 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
dkato 0:77c289709567 34 #endif
dkato 0:77c289709567 35
dkato 0:77c289709567 36 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
dkato 0:77c289709567 37 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
dkato 0:77c289709567 38 #endif
dkato 0:77c289709567 39
dkato 0:77c289709567 40
dkato 0:77c289709567 41 NetworkInterface* easy_connect(bool log_messages = false) {
dkato 0:77c289709567 42 NetworkInterface* network_interface = 0;
dkato 0:77c289709567 43 int connect_success = -1;
dkato 0:77c289709567 44 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
dkato 0:77c289709567 45 if (log_messages) {
dkato 0:77c289709567 46 printf("[EasyConnect] Using WiFi (ESP8266) \n");
dkato 0:77c289709567 47 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
dkato 0:77c289709567 48 }
dkato 0:77c289709567 49 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
dkato 0:77c289709567 50 network_interface = &wifi;
dkato 0:77c289709567 51 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595
dkato 0:77c289709567 52 if (log_messages) {
dkato 0:77c289709567 53 printf("[EasyConnect] Using WiFi (BP3595) \n");
dkato 0:77c289709567 54 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
dkato 0:77c289709567 55 }
dkato 0:77c289709567 56 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, NSAPI_SECURITY_WPA_WPA2);
dkato 0:77c289709567 57 network_interface = &wifi;
dkato 0:77c289709567 58 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
dkato 0:77c289709567 59 if (log_messages) {
dkato 0:77c289709567 60 printf("[EasyConnect] Using Ethernet\n");
dkato 0:77c289709567 61 }
dkato 0:77c289709567 62 connect_success = eth.connect();
dkato 0:77c289709567 63 network_interface = ð
dkato 0:77c289709567 64 #endif
dkato 0:77c289709567 65 if(connect_success == 0) {
dkato 0:77c289709567 66 if (log_messages) {
dkato 0:77c289709567 67 printf("[EasyConnect] Connected to Network successfully\n");
dkato 0:77c289709567 68 }
dkato 0:77c289709567 69 } else {
dkato 0:77c289709567 70 if (log_messages) {
dkato 0:77c289709567 71 printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
dkato 0:77c289709567 72 }
dkato 0:77c289709567 73 return NULL;
dkato 0:77c289709567 74 }
dkato 0:77c289709567 75 const char *ip_addr = network_interface->get_ip_address();
dkato 0:77c289709567 76 const char *mac_addr = network_interface->get_mac_address();
dkato 0:77c289709567 77 if (ip_addr == NULL) {
dkato 0:77c289709567 78 if (log_messages) {
dkato 0:77c289709567 79 printf("[EasyConnect] ERROR - No IP address\n");
dkato 0:77c289709567 80 }
dkato 0:77c289709567 81 return NULL;
dkato 0:77c289709567 82 }
dkato 0:77c289709567 83 if (mac_addr == NULL) {
dkato 0:77c289709567 84 if (log_messages) {
dkato 0:77c289709567 85 printf("[EasyConnect] ERROR - No MAC address\n");
dkato 0:77c289709567 86 }
dkato 0:77c289709567 87 return NULL;
dkato 0:77c289709567 88 }
dkato 0:77c289709567 89 if (log_messages) {
dkato 0:77c289709567 90 printf("[EasyConnect] IP address %s\n", ip_addr);
dkato 0:77c289709567 91 printf("[EasyConnect] MAC address %s\n", mac_addr);
dkato 0:77c289709567 92 }
dkato 0:77c289709567 93 return network_interface;
dkato 0:77c289709567 94 }
dkato 0:77c289709567 95
dkato 0:77c289709567 96 #endif // __MAGIC_CONNECT_H__