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:
Tue Mar 14 05:41:15 2017 +0000
Revision:
1:adf177867e43
Parent:
0:77c289709567
The security of wifi can be changed from mbed_app.json.

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 1:adf177867e43 40 #ifndef MBED_CONF_APP_WIFI_SECURITY
dkato 1:adf177867e43 41 #define MBED_CONF_APP_WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2
dkato 1:adf177867e43 42 #endif
dkato 0:77c289709567 43
dkato 0:77c289709567 44 NetworkInterface* easy_connect(bool log_messages = false) {
dkato 0:77c289709567 45 NetworkInterface* network_interface = 0;
dkato 0:77c289709567 46 int connect_success = -1;
dkato 0:77c289709567 47 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
dkato 0:77c289709567 48 if (log_messages) {
dkato 0:77c289709567 49 printf("[EasyConnect] Using WiFi (ESP8266) \n");
dkato 0:77c289709567 50 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
dkato 0:77c289709567 51 }
dkato 1:adf177867e43 52 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
dkato 0:77c289709567 53 network_interface = &wifi;
dkato 0:77c289709567 54 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595
dkato 0:77c289709567 55 if (log_messages) {
dkato 0:77c289709567 56 printf("[EasyConnect] Using WiFi (BP3595) \n");
dkato 0:77c289709567 57 printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
dkato 0:77c289709567 58 }
dkato 1:adf177867e43 59 connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
dkato 0:77c289709567 60 network_interface = &wifi;
dkato 0:77c289709567 61 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
dkato 0:77c289709567 62 if (log_messages) {
dkato 0:77c289709567 63 printf("[EasyConnect] Using Ethernet\n");
dkato 0:77c289709567 64 }
dkato 0:77c289709567 65 connect_success = eth.connect();
dkato 0:77c289709567 66 network_interface = ð
dkato 0:77c289709567 67 #endif
dkato 0:77c289709567 68 if(connect_success == 0) {
dkato 0:77c289709567 69 if (log_messages) {
dkato 0:77c289709567 70 printf("[EasyConnect] Connected to Network successfully\n");
dkato 0:77c289709567 71 }
dkato 0:77c289709567 72 } else {
dkato 0:77c289709567 73 if (log_messages) {
dkato 0:77c289709567 74 printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
dkato 0:77c289709567 75 }
dkato 0:77c289709567 76 return NULL;
dkato 0:77c289709567 77 }
dkato 0:77c289709567 78 const char *ip_addr = network_interface->get_ip_address();
dkato 0:77c289709567 79 const char *mac_addr = network_interface->get_mac_address();
dkato 0:77c289709567 80 if (ip_addr == NULL) {
dkato 0:77c289709567 81 if (log_messages) {
dkato 0:77c289709567 82 printf("[EasyConnect] ERROR - No IP address\n");
dkato 0:77c289709567 83 }
dkato 0:77c289709567 84 return NULL;
dkato 0:77c289709567 85 }
dkato 0:77c289709567 86 if (mac_addr == NULL) {
dkato 0:77c289709567 87 if (log_messages) {
dkato 0:77c289709567 88 printf("[EasyConnect] ERROR - No MAC address\n");
dkato 0:77c289709567 89 }
dkato 0:77c289709567 90 return NULL;
dkato 0:77c289709567 91 }
dkato 0:77c289709567 92 if (log_messages) {
dkato 0:77c289709567 93 printf("[EasyConnect] IP address %s\n", ip_addr);
dkato 0:77c289709567 94 printf("[EasyConnect] MAC address %s\n", mac_addr);
dkato 0:77c289709567 95 }
dkato 0:77c289709567 96 return network_interface;
dkato 0:77c289709567 97 }
dkato 0:77c289709567 98
dkato 0:77c289709567 99 #endif // __MAGIC_CONNECT_H__