This is a mbed Client sample where ZXing is incorporated, and works on GR-PEACH and GR-LYCHEE.

Dependencies:   DisplayApp AsciiFont

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers easy-connect.h Source File

easy-connect.h

00001 #ifndef __MAGIC_CONNECT_H__
00002 #define __MAGIC_CONNECT_H__
00003 
00004 #include "mbed.h"
00005 
00006 #define ETHERNET        1
00007 #define WIFI_ESP8266    2
00008 #define WIFI_BP3595     3
00009 #define WIFI_ESP32      4
00010 
00011 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00012 #include "ESP8266Interface.h"
00013 
00014 #ifdef MBED_CONF_APP_ESP8266_DEBUG
00015 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX, MBED_CONF_APP_ESP8266_DEBUG);
00016 #else
00017 ESP8266Interface wifi(MBED_CONF_APP_ESP8266_TX, MBED_CONF_APP_ESP8266_RX);
00018 #endif
00019 
00020 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595
00021 #include "LWIPBP3595Interface.h"
00022 LWIPBP3595Interface wifi;
00023 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP32
00024 #include "ESP32Interface.h"
00025 ESP32Interface wifi(MBED_CONF_EASY_CONNECT_WIFI_ESP32_EN, MBED_CONF_EASY_CONNECT_WIFI_ESP32_IO0, MBED_CONF_EASY_CONNECT_WIFI_ESP32_TX, MBED_CONF_EASY_CONNECT_WIFI_ESP32_RX);
00026 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00027 #include "EthernetInterface.h"
00028 EthernetInterface eth;
00029 #else
00030 #error "No connectivity method chosen. Please add 'config.network-interfaces.value' to your mbed_app.json (see README.md for more information)."
00031 #endif
00032 
00033 // This is address to mbed Device Connector
00034 #define MBED_SERVER_ADDRESS "coap://api.connector.mbed.com:5684"
00035 
00036 #ifdef MBED_CONF_APP_ESP8266_SSID
00037 #define MBED_CONF_APP_WIFI_SSID MBED_CONF_APP_ESP8266_SSID
00038 #endif
00039 
00040 #ifdef MBED_CONF_APP_ESP8266_PASSWORD
00041 #define MBED_CONF_APP_WIFI_PASSWORD MBED_CONF_APP_ESP8266_PASSWORD
00042 #endif
00043 
00044 #ifndef MBED_CONF_APP_WIFI_SECURITY
00045 #define MBED_CONF_APP_WIFI_SECURITY NSAPI_SECURITY_WPA_WPA2
00046 #endif
00047 
00048 NetworkInterface* easy_connect(bool log_messages = false) {
00049     NetworkInterface* network_interface = 0;
00050     int connect_success = -1;
00051 #if MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP8266
00052     if (log_messages) {
00053         printf("[EasyConnect] Using WiFi (ESP8266) \n");
00054         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00055     }
00056     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
00057     network_interface = &wifi;
00058 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_BP3595
00059     if (log_messages) {
00060         printf("[EasyConnect] Using WiFi (BP3595) \n");
00061         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00062     }
00063     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
00064     network_interface = &wifi;
00065 #elif MBED_CONF_APP_NETWORK_INTERFACE == WIFI_ESP32
00066     if (log_messages) {
00067         printf("[EasyConnect] Using WiFi (ESP32) \n");
00068         printf("[EasyConnect] Connecting to WiFi %s\n", MBED_CONF_APP_WIFI_SSID);
00069     }
00070     connect_success = wifi.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
00071     network_interface = &wifi;
00072 #elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
00073     if (log_messages) {
00074         printf("[EasyConnect] Using Ethernet\n");
00075     }
00076     connect_success = eth.connect();
00077     network_interface = ð
00078 #endif
00079     if(connect_success == 0) {
00080         if (log_messages) {
00081             printf("[EasyConnect] Connected to Network successfully\n");
00082         }
00083     } else {
00084         if (log_messages) {
00085             printf("[EasyConnect] Connection to Network Failed %d!\n", connect_success);
00086         }
00087         return NULL;
00088     }
00089     const char *ip_addr  = network_interface->get_ip_address();
00090     const char *mac_addr = network_interface->get_mac_address();
00091     if (ip_addr == NULL) {
00092         if (log_messages) {
00093             printf("[EasyConnect] ERROR - No IP address\n");
00094         }
00095         return NULL;
00096     }
00097     if (mac_addr == NULL) {
00098         if (log_messages) {
00099             printf("[EasyConnect] ERROR - No MAC address\n");
00100         }
00101         return NULL;
00102     }
00103     if (log_messages) {
00104         printf("[EasyConnect] IP address %s\n", ip_addr);
00105         printf("[EasyConnect] MAC address %s\n", mac_addr);
00106     }
00107     return network_interface;
00108 }
00109 
00110 #endif // __MAGIC_CONNECT_H__
00111