Sample program showing how to connect GR-PEACH into Watson IoT through mbed Connector and Watson's Connector Bridge

Dependencies:   AsciiFont DisplayApp GR-PEACH_video LCD_shield_config LWIPBP3595Interface_STA_for_mbed-os USBDevice

Overview

This sample program shows how to connect GR-PEACH into Watson IoT through mbed Connector and Watson's Connector Bridge. Note that this program is derived from https://github.com/ARMmbed/mbed-ethernet-sample-techcon2016.

In this program, at first, the barcode data input from camera is decoded on GR-PEACH using ZXing which is an open-source, multi-format 1D/2D barcode image processing library. Then, the decoded string is sent to Watson IoT via mbed Device Connector and Watson's Connector Bridge. At last, the delivered string is confirmed via NodeRED flow.

Required hardware

Application setup

  1. Select the connection type. For details, please refer to the following wiki:
    https://developer.mbed.org/teams/Renesas/code/mbed-os_Watson-IoT_ZXing_sample/wiki/Connection-type.
  2. Set the client credentials. For details, please refer to the following wiki:
    https://developer.mbed.org/teams/Renesas/code/mbed-os_Watson-IoT_ZXing_sample/wiki/Client-credentials.
  3. Change Ethernet settings. For details, please refer to the following wiki:
    https://developer.mbed.org/teams/Renesas/code/mbed-os_Watson-IoT_ZXing_sample/wiki/Ethernet-settings.
  4. Change Wifi settings. For details, please refer to the following wiki:
    https://developer.mbed.org/teams/Renesas/code/mbed-os_Watson-IoT_ZXing_sample/wiki/Wifi-settings.

Building this sample program

  1. Import this sample program onto mbed Compiler.
  2. Configure this sample program in accordance with Application setup.
  3. Compile the example on mbed Compiler and download the resultant binary file.
  4. Plug the Ethernet cable into GR-PEACH if you are using Ethernet mode. /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen7.png
  5. Plug the micro-USB cable into the OpenSDA port with lies on the next to the RESET button.
  6. Copy the binary previously downloaded to your PC to GR-PEACH to flash this sample program. When the copy is successfully completed, the board is ready to work.
  7. Press the RESET button on the board to run this sample program.
  8. For verification, please refer to the following wiki:
    https://developer.mbed.org/teams/Renesas/code/mbed-os_Watson-IoT_ZXing_sample/wiki/Monitoring-the-application.

Application resources

This sample program exposes two resources listed below:

  1. /311/0/5850 : Trigger to toggle LED on GR-PEACH (PUT). When "1" is sent to this resource by PUT method, LED should be turned off. Otherwise, LED should be turned on.
  2. /888/0/7700: Decode result of barcode data input from camera (GET)

Setup of Watson IoT

  1. You should create Watson IoT Instance by the following procedure:
    • Login IBM Bluemix from https://console.ng.bluemix.net/ and login it.
    • Select Services as follows: /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen6.png
    • Select Internet of Things" as follows: /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen7_1.png
    • Press Get Started as follows: /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen8.png
    • Configure Watson IoT Instance
      • Leave unbound /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen9.png
      • Select the Free plan, then click Create /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen10.png

  2. You should create Watson IoT Application Credentials Once you successfully create Watson IoT Instance, you should see the following Watson IoT dashboard: /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen11.png
    • On dashboard, launch the Connect your devices dashboard /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen12.png
    • Click APPS /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen13.png
    • Click Generate API Key /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen14_1.png
    • Record the API Key and Authenticatoin Token. Then click Generate after adding a comment /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen15.png Note that these two values will be needed in the later step.
  3. Watson IoT NodeRED Application should be created in accordance with the following procedure:
    • Go back to your Bluemix Dashboard: https://console.ng.bluemix.net
    • Click Create Application /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen16.png
    • Select Node-RED Starter /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen17.png
    • Enter a unique App name and Click Create /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen18.png
    • Application will Stage after a few minutes
    • Application will report app is running when the staging has completed
  4. Watson NodeRED Application should be binded to Watson IoT instance
    • You can now see the dashboard below: /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen19.png
    • Click Connections. Then, press Connect Existing /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen20.png
    • Click on Watson IoT Instance badge. Then, press Connect /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen21.png
    • NodeRED application will restage. This will take a while. Please wait for Your app is running state /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen22.png /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen23.png
  5. You should acquire mbed Device Connector DOMAIN value
    • Go to the mbed Device Connector Dashboard: https://connector.mbed.com and log in /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen41.png
    • Select Access Key in order to create a new token /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen42.png
    • Click CREATE NEW ACCESS KEY and enter a unique name. Then, push ADD bution /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen43.png
    • an API token is generated. Need to save the generated string for later steps /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen44.png
  1. You should configure Watson IoT ARM mbed Connector Bridge
    • Go back to the Watson IoT Dashboard and select Extensions /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen24.png
    • Press Add Extension /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen25.png
    • Add ARM mbed Connector /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen26_1.png
    • Setup the bridge /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen27.png
    • Enter your Connector API Token to Access Key and MBED_DOMAIN to Domain Id, respectively. Then, click Check Connection /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen28.png
    • If the connection is successfully established, you can see Connection Established. Then, press Done /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen29.png
  2. NodeRED Flow Example should be imported
    • Open NODEFLOW_GR-PEACH.txt
    • Navigate to the URL that you recorded earlier for your Watson IoT NodeRED Application (i.e. http://<app name>.mybluemix.net) and Select Go to your NodeRED flow editor /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen30.png
    • Select menu, Import and Clipboard /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen31.png
    • Paste the entire contents of JSON code described in NODEFLOW_GR-PEACH.txt and click Import /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen32.png
  3. Watson IoT Application Node Flow should be configured
    • Double-click input node and link them to your Watson IoT instance. /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen33.png
    • Click on the edit button /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen34.png
    • First enter arbitrary name. Then, Watson API Key and Watson Auth Token previously acquired. Finally, press Add to save /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen35.png
    • Enter MBED_ENDPOINT_NAME in security.h as Device ID. Then, press Done to save /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen36.png
    • Double-click output node /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen37.png
    • Edit API Key and Device Id in the same manner as that for input node
    • Click LED OFF and LED ON node and replace MBED_ENDPOINT_NAME_GOES_HERE with MBED_ENDPOINT_NAME as shown below: /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen38.png
    • Click Deproy to deploy NodeRED application /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen39.png
  4. Invoke sample program
    • Press the reset button on GR-PEACH
    • The decode result of barcode data should be shown in debug window /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen40_1.png
    • Also, you can toggle LED by clicking Turn LED OFF and Turn LED ON injection node /media/uploads/HinoNaka/mbed-os_watson-iot_zxing_sample_screen40_2.png

mbedConnectorInterface/source/mbedEndpointNetwork.cpp

Committer:
Osamu Nakamura
Date:
2016-11-24
Revision:
0:ad834d403a8c

File content as of revision 0:ad834d403a8c:

/**
 * @file    mbedEndpointNetwork.cpp
 * @brief   mbed Connector Interface network low level functions and support (Ethernet, WiFi, Mesh (6LowPAN,Thread))
 * @author  Doug Anson
 * @version 1.0
 * @see     
 *
 * Copyright (c) 2014
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

// Connector Endpoint
#include "mbed-connector-interface/ConnectorEndpoint.h"

// OptionsBuilder
#include "mbed-connector-interface/OptionsBuilder.h"

// Forward declarations of public functions in mbedEndpointNetwork
#include "mbed-connector-interface/mbedEndpointNetworkImpl.h"

// Network Selection
#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
	#define NETWORK_TYPE			(char *)"WiFi"
#if 1 //bp3595
    #include "LWIPBP3595Interface.h"
    LWIPBP3595Interface network;
    DigitalOut usb1en(P3_8);
#else //#if 1   
	#include "ESP8266Interface.h"
	ESP8266Interface network(MBED_CONF_APP_WIFI_TX,MBED_CONF_APP_WIFI_RX);
#endif
#elif MBED_CONF_APP_NETWORK_INTERFACE == ETHERNET
	#define NETWORK_TYPE			(char *)"Ethernet"
	#include "EthernetInterface.h"
	EthernetInterface network;
#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND
	#define NETWORK_TYPE			(char *)"6LowPAN"
	#include "NanostackInterface.h"
	LoWPANNDInterface network;
#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
	#define NETWORK_TYPE			(char *)"Thread"
	#include "NanostackInterface.h"
	ThreadInterface network;
#endif

// Logger instance
extern Logger logger;

// endpoint instance 
static void *_endpoint_instance = NULL;

// LWIP network instance forward reference
extern NetworkInterface *__network_interface;

// main loop cycle period
static int _main_loop_iteration_wait_ms = MAIN_LOOP_WAIT_TIME_MS;

// endpoint shutdown indicator
static volatile bool _shutdown_endpoint = false;			

extern "C" {

/*********************** START LOCAL FUNCTIONS **************************/

// start shutting downt the endpoint
void start_endpoint_shutdown(void) {
	if (_shutdown_endpoint == true) {
		Connector::Endpoint *ep = (Connector::Endpoint *)_endpoint_instance;
		if (ep != NULL && ep->isRegistered() == true) {
			logger.logging("mbedEndpointNetwork(%s): shutdown requested. De-registering the endpoint...",NETWORK_TYPE);
			ep->de_register_endpoint();
		}
		
		// Clean up
		if (ep != NULL) {
			delete ep;
			_endpoint_instance = NULL;
		}
	}
	
	// ready to shutdown...
	logger.logging("mbedEndpointNetwork(%s): endpoint shutdown. Bye!",NETWORK_TYPE);
}
	
// setup shutdown button
#if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
InterruptIn shutdown_button(MBED_CONF_APP_SHUTDOWN_PIN);
void configure_deregistration_button(void) {
	logger.logging("mbedEndpointNetwork(%s): configuring de-registration button...",NETWORK_TYPE); 
	shutdown_button.fall(&net_shutdown_endpoint);
}	
#endif

// setup shutdown button
void setup_deregistration_button(void) {
#if MBED_CONF_APP_SHUTDOWN_BUTTON_ENABLE == true
	configure_deregistration_button();
#endif
}

// configure main loop parameters
void configure_main_loop_params(Connector::Endpoint *endpoint) {
	// set the initial shutdown state
	_shutdown_endpoint = false;
}

// perform an actvity in the main loop
void peform_main_loop_activity(void) {
	// empty for now...
	;
}

// begin the main loop for processing endpoint events
void begin_main_loop(void) 
{
	// DEBUG
	logger.logging("mbedEndpointNetwork(%s): endpoint main loop beginning...",NETWORK_TYPE);
	
	// enter our main loop (until the shutdown condition flags it...)
	while(_shutdown_endpoint == false) {
		Thread::wait(_main_loop_iteration_wait_ms);
		peform_main_loop_activity();
	}
	
	// main loop has exited... start the endpoint shutdown...
	logger.logging("mbedEndpointNetwork(%s): endpoint main loop exited. Starting endpoint shutdown...",NETWORK_TYPE);
	start_endpoint_shutdown();
}

/************************ END LOCAL FUNCTIONS ***************************/

/*********************** START PUBLIC FUNCTIONS *************************/

// get the network type
char *net_get_type() {
	return NETWORK_TYPE;
}

// shutdown the endpoint
void net_shutdown_endpoint() {
    _shutdown_endpoint = true;
}
	
// called after the endpoint is configured...
void net_plumb_network(void *p) 
{
    int connected = 0;
    Connector::Endpoint *ep = NULL;
    Connector::Options *options = NULL;
    
    // save 
    _endpoint_instance = p;
    
    // connected
    if (p != NULL) {
		ep = (Connector::Endpoint *)p;
		options = ep->getOptions();
	}
    
#if MBED_CONF_APP_NETWORK_INTERFACE == WIFI
#if 1 //BP3595
    usb1en = 1;
    Thread::wait(5);
    usb1en = 0;
    Thread::wait(5);
    connected = network.connect(MBED_CONF_APP_WIFI_SSID, MBED_CONF_APP_WIFI_PASSWORD, MBED_CONF_APP_WIFI_SECURITY);
#else //#if 1
	// map security types
    nsapi_security_t security_opt = NSAPI_SECURITY_NONE;
    if  (options->getWiFiAuthType() == WIFI_WPA_PERSONAL) {
    	security_opt = NSAPI_SECURITY_WPA;
    }
    if  (options->getWiFiAuthType() == WIFI_WPA2_PERSONAL) {
    	security_opt = NSAPI_SECURITY_WPA2;
    }
    if  (options->getWiFiAuthType() == WIFI_WEP) {
    	security_opt = NSAPI_SECURITY_WEP;
    }
    
	// Network Init (WIFI)...
    connected = network.connect(options->getWiFiSSID().c_str(),options->getWiFiAuthKey().c_str(),security_opt);
#endif
#elif MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD
	// Set the IP Address type to IPV6
	((Connector::OptionsBuilder *)options)->setIPAddressType(IP_ADDRESS_TYPE_IPV6);
	
	// Network Init (Mesh)
	connected = network.connect();
#else
	// not used... just removes a compiler warning...
    options->getConnectorURL();
    
    // Network Init (Ethernet)
    connected = network.connect();
#endif

	// check the connection status..
	if (connected == 0) {
		// success
    	__network_interface = (NetworkInterface *)&network;
    	if (ep != NULL) {
    		ep->isConnected(true);
    	
    		// Debug
   			logger.logging("mbedEndpointNetwork(%s): IP Address: %s",NETWORK_TYPE,network.get_ip_address());
   		}
   	}
   	else {
   		__network_interface = NULL;
    	if (ep != NULL) {
    		ep->isConnected(false);
    	}
    	
    	// Debug
   		logger.logging("mbedEndpointNetwork(%s): CONNECTION FAILED",NETWORK_TYPE);
   	}
}

// finalize and run the endpoint main loop
void net_finalize_and_run_endpoint_main_loop(void *p) 
{
	// cast
	Connector::Endpoint *ep = (Connector::Endpoint *)p;
	
	// Initialize our main loop... 
    configure_main_loop_params(ep);
    
    // setup the shutdown button (if enabled for a given platform...)
    setup_deregistration_button();

	// register the endpoint
	logger.logging("mbedEndpointNetwork(%s): registering endpoint...",NETWORK_TYPE); 
	ep->register_endpoint(ep->getEndpointSecurity(),ep->getEndpointObjectList());
	       
    // Begin the endpoint's main loop
    begin_main_loop();
}

/************************ END PUBLIC FUNCTIONS **************************/

}