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
- GR-PEACH ( https://developer.mbed.org/platforms/Renesas-GR-PEACH/ )
- Audio Camera Shield ( https://developer.mbed.org/teams/Renesas/wiki/Audio_Camera-shield )
- LCD Shield ( https://developer.mbed.org/teams/Renesas/wiki/LCD-shield )
- BP3595 ( https://developer.mbed.org/components/BP3595-for-GR-PEACH/ )
- Ethernet cable and connection to the internet
Application setup
- 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. - 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. - 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. - 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
- Import this sample program onto mbed Compiler.
- Configure this sample program in accordance with Application setup.
- Compile the example on mbed Compiler and download the resultant binary file.
- Plug the Ethernet cable into GR-PEACH if you are using Ethernet mode.
- Plug the micro-USB cable into the OpenSDA port with lies on the next to the RESET button.
- 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.
- Press the RESET button on the board to run this sample program.
- 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:
- /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.
- /888/0/7700: Decode result of barcode data input from camera (GET)
Setup of Watson IoT
- 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:
- Select Internet of Things" as follows:
- Press Get Started as follows:
- Configure Watson IoT Instance
- Leave unbound
- Select the Free plan, then click Create
- You should create Watson IoT Application Credentials
Once you successfully create Watson IoT Instance, you should see the following Watson IoT dashboard:
- On dashboard, launch the Connect your devices dashboard
- Click APPS
- Click Generate API Key
- Record the API Key and Authenticatoin Token. Then click Generate after adding a comment Note that these two values will be needed in the later step.
- 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
- Select Node-RED Starter
- Enter a unique App name and Click Create
- Application will Stage after a few minutes
- Application will report app is running when the staging has completed
- Watson NodeRED Application should be binded to Watson IoT instance
- You can now see the dashboard below:
- Click Connections. Then, press Connect Existing
- Click on Watson IoT Instance badge. Then, press Connect
- NodeRED application will restage. This will take a while. Please wait for Your app is running state
- You should acquire mbed Device Connector DOMAIN value
- Go to the mbed Device Connector Dashboard: https://connector.mbed.com and log in
- Select Access Key in order to create a new token
- Click CREATE NEW ACCESS KEY and enter a unique name. Then, push ADD bution
- an API token is generated. Need to save the generated string for later steps
- You should configure Watson IoT ARM mbed Connector Bridge
- Go back to the Watson IoT Dashboard and select Extensions
- Press Add Extension
- Add ARM mbed Connector
- Setup the bridge
- Enter your Connector API Token to Access Key and MBED_DOMAIN to Domain Id, respectively. Then, click Check Connection
- If the connection is successfully established, you can see Connection Established. Then, press Done
- 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
- Select menu, Import and Clipboard
- Paste the entire contents of JSON code described in NODEFLOW_GR-PEACH.txt and click Import
- Watson IoT Application Node Flow should be configured
- Double-click input node and link them to your Watson IoT instance.
- Click on the edit button
- First enter arbitrary name. Then, Watson API Key and Watson Auth Token previously acquired. Finally, press Add to save
- Enter MBED_ENDPOINT_NAME in security.h as Device ID. Then, press Done to save
- Double-click output node
- 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:
- Click Deproy to deploy NodeRED application
- Invoke sample program
- Press the reset button on GR-PEACH
- The decode result of barcode data should be shown in debug window
- Also, you can toggle LED by clicking Turn LED OFF and Turn LED ON injection node
mbedConnectorInterface/source/Utils.cpp@0:ad834d403a8c, 2016-11-24 (annotated)
- Committer:
- Osamu Nakamura
- Date:
- Thu Nov 24 13:59:16 2016 +0900
- Revision:
- 0:ad834d403a8c
Initial Commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Osamu Nakamura |
0:ad834d403a8c | 1 | /** |
Osamu Nakamura |
0:ad834d403a8c | 2 | * @file Utils.cpp |
Osamu Nakamura |
0:ad834d403a8c | 3 | * @brief mbed CoAP Endpoint misc utils collection |
Osamu Nakamura |
0:ad834d403a8c | 4 | * @author Doug Anson |
Osamu Nakamura |
0:ad834d403a8c | 5 | * @version 1.0 |
Osamu Nakamura |
0:ad834d403a8c | 6 | * @see |
Osamu Nakamura |
0:ad834d403a8c | 7 | * |
Osamu Nakamura |
0:ad834d403a8c | 8 | * Copyright (c) 2014 |
Osamu Nakamura |
0:ad834d403a8c | 9 | * |
Osamu Nakamura |
0:ad834d403a8c | 10 | * Licensed under the Apache License, Version 2.0 (the "License"); |
Osamu Nakamura |
0:ad834d403a8c | 11 | * you may not use this file except in compliance with the License. |
Osamu Nakamura |
0:ad834d403a8c | 12 | * You may obtain a copy of the License at |
Osamu Nakamura |
0:ad834d403a8c | 13 | * |
Osamu Nakamura |
0:ad834d403a8c | 14 | * http://www.apache.org/licenses/LICENSE-2.0 |
Osamu Nakamura |
0:ad834d403a8c | 15 | * |
Osamu Nakamura |
0:ad834d403a8c | 16 | * Unless required by applicable law or agreed to in writing, software |
Osamu Nakamura |
0:ad834d403a8c | 17 | * distributed under the License is distributed on an "AS IS" BASIS, |
Osamu Nakamura |
0:ad834d403a8c | 18 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
Osamu Nakamura |
0:ad834d403a8c | 19 | * See the License for the specific language governing permissions and |
Osamu Nakamura |
0:ad834d403a8c | 20 | * limitations under the License. |
Osamu Nakamura |
0:ad834d403a8c | 21 | */ |
Osamu Nakamura |
0:ad834d403a8c | 22 | |
Osamu Nakamura |
0:ad834d403a8c | 23 | // mbed Endpoint includes |
Osamu Nakamura |
0:ad834d403a8c | 24 | #include "mbed-connector-interface/ConnectorEndpoint.h" |
Osamu Nakamura |
0:ad834d403a8c | 25 | #include "mbed-connector-interface/OptionsBuilder.h" |
Osamu Nakamura |
0:ad834d403a8c | 26 | #include "mbed-connector-interface/mbedEndpointNetwork.h" |
Osamu Nakamura |
0:ad834d403a8c | 27 | #include "mbed-connector-interface/DeviceManager.h" |
Osamu Nakamura |
0:ad834d403a8c | 28 | #include "mbed-connector-interface/ObjectInstanceManager.h" |
Osamu Nakamura |
0:ad834d403a8c | 29 | |
Osamu Nakamura |
0:ad834d403a8c | 30 | // External references (defined in main.cpp) |
Osamu Nakamura |
0:ad834d403a8c | 31 | Connector::Options *configure_endpoint(Connector::OptionsBuilder &builder); |
Osamu Nakamura |
0:ad834d403a8c | 32 | extern Logger logger; |
Osamu Nakamura |
0:ad834d403a8c | 33 | |
Osamu Nakamura |
0:ad834d403a8c | 34 | // Our Endpoint |
Osamu Nakamura |
0:ad834d403a8c | 35 | Connector::Endpoint *__endpoint = NULL; |
Osamu Nakamura |
0:ad834d403a8c | 36 | |
Osamu Nakamura |
0:ad834d403a8c | 37 | // Our Endpoint configured Options |
Osamu Nakamura |
0:ad834d403a8c | 38 | Connector::OptionsBuilder config; |
Osamu Nakamura |
0:ad834d403a8c | 39 | Connector::Options *options = NULL; |
Osamu Nakamura |
0:ad834d403a8c | 40 | |
Osamu Nakamura |
0:ad834d403a8c | 41 | // initialize the Connector::Endpoint instance |
Osamu Nakamura |
0:ad834d403a8c | 42 | void *utils_init_endpoint(bool canActAsRouterNode) { |
Osamu Nakamura |
0:ad834d403a8c | 43 | // alloc Endpoint |
Osamu Nakamura |
0:ad834d403a8c | 44 | logger.logging("Endpoint: allocating endpoint instance..."); |
Osamu Nakamura |
0:ad834d403a8c | 45 | Connector::Endpoint *ep = new Connector::Endpoint(&logger,options); |
Osamu Nakamura |
0:ad834d403a8c | 46 | if (ep != NULL) { |
Osamu Nakamura |
0:ad834d403a8c | 47 | // link to config object |
Osamu Nakamura |
0:ad834d403a8c | 48 | config.setEndpoint((void *)ep); |
Osamu Nakamura |
0:ad834d403a8c | 49 | |
Osamu Nakamura |
0:ad834d403a8c | 50 | // not sure if we need this anymore... |
Osamu Nakamura |
0:ad834d403a8c | 51 | ep->asRouterNode(canActAsRouterNode); |
Osamu Nakamura |
0:ad834d403a8c | 52 | |
Osamu Nakamura |
0:ad834d403a8c | 53 | // Add ObjectInstanceManager |
Osamu Nakamura |
0:ad834d403a8c | 54 | ObjectInstanceManager *oim = new ObjectInstanceManager(&logger,(void *)ep); |
Osamu Nakamura |
0:ad834d403a8c | 55 | ep->setObjectInstanceManager(oim); |
Osamu Nakamura |
0:ad834d403a8c | 56 | } |
Osamu Nakamura |
0:ad834d403a8c | 57 | return (void *)ep; |
Osamu Nakamura |
0:ad834d403a8c | 58 | } |
Osamu Nakamura |
0:ad834d403a8c | 59 | |
Osamu Nakamura |
0:ad834d403a8c | 60 | // further simplifies the endpoint main() configuration by removing the final initialization details of the endpoint... |
Osamu Nakamura |
0:ad834d403a8c | 61 | void utils_configure_endpoint(void *p) |
Osamu Nakamura |
0:ad834d403a8c | 62 | { |
Osamu Nakamura |
0:ad834d403a8c | 63 | // our Endpoint |
Osamu Nakamura |
0:ad834d403a8c | 64 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
Osamu Nakamura |
0:ad834d403a8c | 65 | |
Osamu Nakamura |
0:ad834d403a8c | 66 | // default configuration - see mbedConnectorInterface.h for definitions... |
Osamu Nakamura |
0:ad834d403a8c | 67 | logger.logging("Endpoint: setting defaults..."); |
Osamu Nakamura |
0:ad834d403a8c | 68 | config.setEndpointNodename(NODE_NAME); |
Osamu Nakamura |
0:ad834d403a8c | 69 | config.setEndpointType(DEFAULT_ENDPOINT_TYPE); |
Osamu Nakamura |
0:ad834d403a8c | 70 | config.setLifetime(REG_LIFETIME_SEC); |
Osamu Nakamura |
0:ad834d403a8c | 71 | |
Osamu Nakamura |
0:ad834d403a8c | 72 | // WiFi defaults |
Osamu Nakamura |
0:ad834d403a8c | 73 | config.setWiFiSSID((char *)WIFI_DEFAULT_SSID); // default: changeme |
Osamu Nakamura |
0:ad834d403a8c | 74 | config.setWiFiAuthType(WIFI_NONE); // default: none |
Osamu Nakamura |
0:ad834d403a8c | 75 | config.setWiFiAuthKey((char *)WIFI_DEFAULT_AUTH_KEY); // default: changeme |
Osamu Nakamura |
0:ad834d403a8c | 76 | |
Osamu Nakamura |
0:ad834d403a8c | 77 | // Default CoAP Connection Type |
Osamu Nakamura |
0:ad834d403a8c | 78 | config.setCoAPConnectionType(COAP_UDP); // default CoAP Connection Type |
Osamu Nakamura |
0:ad834d403a8c | 79 | |
Osamu Nakamura |
0:ad834d403a8c | 80 | // Set the default IP Address Type |
Osamu Nakamura |
0:ad834d403a8c | 81 | #if MBED_CONF_APP_NETWORK_INTERFACE == MESH_LOWPAN_ND || MBED_CONF_APP_NETWORK_INTERFACE == MESH_THREAD |
Osamu Nakamura |
0:ad834d403a8c | 82 | config.setIPAddressType(IP_ADDRESS_TYPE_IPV6); // IPv6 is the default for mesh |
Osamu Nakamura |
0:ad834d403a8c | 83 | #else |
Osamu Nakamura |
0:ad834d403a8c | 84 | config.setIPAddressType(IP_ADDRESS_TYPE_IPV4); // IPv4 is the default for all but mesh |
Osamu Nakamura |
0:ad834d403a8c | 85 | #endif |
Osamu Nakamura |
0:ad834d403a8c | 86 | |
Osamu Nakamura |
0:ad834d403a8c | 87 | // Establish default CoAP observation behavior |
Osamu Nakamura |
0:ad834d403a8c | 88 | config.setImmedateObservationEnabled(true); |
Osamu Nakamura |
0:ad834d403a8c | 89 | |
Osamu Nakamura |
0:ad834d403a8c | 90 | // Establish default CoAP GET-based observation control behavior |
Osamu Nakamura |
0:ad834d403a8c | 91 | config.setEnableGETObservationControl(false); |
Osamu Nakamura |
0:ad834d403a8c | 92 | |
Osamu Nakamura |
0:ad834d403a8c | 93 | // Device Manager installation |
Osamu Nakamura |
0:ad834d403a8c | 94 | DeviceManager *device_manager = (DeviceManager *)ep->getDeviceManager(); |
Osamu Nakamura |
0:ad834d403a8c | 95 | if (device_manager != NULL) { |
Osamu Nakamura |
0:ad834d403a8c | 96 | logger.logging("Endpoint: installing and setting up device manager and its resources..."); |
Osamu Nakamura |
0:ad834d403a8c | 97 | device_manager->install((void *)ep,(void *)&config); |
Osamu Nakamura |
0:ad834d403a8c | 98 | } |
Osamu Nakamura |
0:ad834d403a8c | 99 | else { |
Osamu Nakamura |
0:ad834d403a8c | 100 | logger.logging("Endpoint: no device manager installed..."); |
Osamu Nakamura |
0:ad834d403a8c | 101 | } |
Osamu Nakamura |
0:ad834d403a8c | 102 | |
Osamu Nakamura |
0:ad834d403a8c | 103 | // main.cpp can override or change any of the above defaults... |
Osamu Nakamura |
0:ad834d403a8c | 104 | logger.logging("Endpoint: gathering configuration overrides..."); |
Osamu Nakamura |
0:ad834d403a8c | 105 | options = configure_endpoint(config); |
Osamu Nakamura |
0:ad834d403a8c | 106 | |
Osamu Nakamura |
0:ad834d403a8c | 107 | // set our options |
Osamu Nakamura |
0:ad834d403a8c | 108 | ep->setOptions(options); |
Osamu Nakamura |
0:ad834d403a8c | 109 | |
Osamu Nakamura |
0:ad834d403a8c | 110 | // DONE |
Osamu Nakamura |
0:ad834d403a8c | 111 | logger.logging("Endpoint: endpoint configuration completed."); |
Osamu Nakamura |
0:ad834d403a8c | 112 | } |
Osamu Nakamura |
0:ad834d403a8c | 113 | |
Osamu Nakamura |
0:ad834d403a8c | 114 | // build out the endpoint and its resources |
Osamu Nakamura |
0:ad834d403a8c | 115 | void utils_build_endpoint(void *p) |
Osamu Nakamura |
0:ad834d403a8c | 116 | { |
Osamu Nakamura |
0:ad834d403a8c | 117 | if (p != NULL) { |
Osamu Nakamura |
0:ad834d403a8c | 118 | // Build the Endpoint |
Osamu Nakamura |
0:ad834d403a8c | 119 | logger.logging("Endpoint: building endpoint and its resources..."); |
Osamu Nakamura |
0:ad834d403a8c | 120 | Connector::Endpoint *ep = (Connector::Endpoint *)p; |
Osamu Nakamura |
0:ad834d403a8c | 121 | ep->buildEndpoint(); |
Osamu Nakamura |
0:ad834d403a8c | 122 | } |
Osamu Nakamura |
0:ad834d403a8c | 123 | } |
Osamu Nakamura |
0:ad834d403a8c | 124 | |
Osamu Nakamura |
0:ad834d403a8c | 125 | // parse out the CoAP port number from the connection URL |
Osamu Nakamura |
0:ad834d403a8c | 126 | uint16_t extract_port_from_url(char *url,uint16_t default_port) |
Osamu Nakamura |
0:ad834d403a8c | 127 | { |
Osamu Nakamura |
0:ad834d403a8c | 128 | uint16_t port = default_port; |
Osamu Nakamura |
0:ad834d403a8c | 129 | |
Osamu Nakamura |
0:ad834d403a8c | 130 | if (url != NULL && strlen(url) > 0) { |
Osamu Nakamura |
0:ad834d403a8c | 131 | char buffer[MAX_CONN_URL_LENGTH+1]; |
Osamu Nakamura |
0:ad834d403a8c | 132 | char uri[MAX_CONN_URL_LENGTH+1]; |
Osamu Nakamura |
0:ad834d403a8c | 133 | char path[MAX_CONN_URL_LENGTH+1]; |
Osamu Nakamura |
0:ad834d403a8c | 134 | |
Osamu Nakamura |
0:ad834d403a8c | 135 | // initialize the buffer |
Osamu Nakamura |
0:ad834d403a8c | 136 | memset(buffer,0,MAX_CONN_URL_LENGTH+1); |
Osamu Nakamura |
0:ad834d403a8c | 137 | memset(uri,0,MAX_CONN_URL_LENGTH+1); |
Osamu Nakamura |
0:ad834d403a8c | 138 | memset(path,0,MAX_CONN_URL_LENGTH+1); |
Osamu Nakamura |
0:ad834d403a8c | 139 | int length = strlen(url); |
Osamu Nakamura |
0:ad834d403a8c | 140 | |
Osamu Nakamura |
0:ad834d403a8c | 141 | // truncate if needed |
Osamu Nakamura |
0:ad834d403a8c | 142 | if (length >MAX_CONN_URL_LENGTH) length = MAX_CONN_URL_LENGTH; |
Osamu Nakamura |
0:ad834d403a8c | 143 | |
Osamu Nakamura |
0:ad834d403a8c | 144 | // make a copy... |
Osamu Nakamura |
0:ad834d403a8c | 145 | memcpy(buffer,url,length); |
Osamu Nakamura |
0:ad834d403a8c | 146 | |
Osamu Nakamura |
0:ad834d403a8c | 147 | // remove the forward slashes and colons |
Osamu Nakamura |
0:ad834d403a8c | 148 | for(int i=0;i<length;++i) { |
Osamu Nakamura |
0:ad834d403a8c | 149 | if (buffer[i] == ':') buffer[i] = ' '; |
Osamu Nakamura |
0:ad834d403a8c | 150 | if (buffer[i] == '/') buffer[i] = ' '; |
Osamu Nakamura |
0:ad834d403a8c | 151 | if (buffer[i] == '[') buffer[i] = ' '; |
Osamu Nakamura |
0:ad834d403a8c | 152 | if (buffer[i] == ']') buffer[i] = ' '; |
Osamu Nakamura |
0:ad834d403a8c | 153 | } |
Osamu Nakamura |
0:ad834d403a8c | 154 | |
Osamu Nakamura |
0:ad834d403a8c | 155 | // parse |
Osamu Nakamura |
0:ad834d403a8c | 156 | sscanf(buffer,"%s%s%d",uri,path,(int *)&port); |
Osamu Nakamura |
0:ad834d403a8c | 157 | |
Osamu Nakamura |
0:ad834d403a8c | 158 | // IPv6 kludge until we parse it properly... |
Osamu Nakamura |
0:ad834d403a8c | 159 | if (strcmp(uri,"coaps") == 0 && strcmp(path,"2607") == 0) { |
Osamu Nakamura |
0:ad834d403a8c | 160 | // IPv6 in use - so nail to Connector's IPv6 address |
Osamu Nakamura |
0:ad834d403a8c | 161 | strcpy(uri,"coap"); |
Osamu Nakamura |
0:ad834d403a8c | 162 | strcpy(path,"2607:f0d0:2601:52::20"); |
Osamu Nakamura |
0:ad834d403a8c | 163 | port = default_port; |
Osamu Nakamura |
0:ad834d403a8c | 164 | |
Osamu Nakamura |
0:ad834d403a8c | 165 | // DEBUG |
Osamu Nakamura |
0:ad834d403a8c | 166 | logger.logging("Endpoint: Connector IPV6 uri: %s path: %s port: %d",uri,path,port); |
Osamu Nakamura |
0:ad834d403a8c | 167 | } |
Osamu Nakamura |
0:ad834d403a8c | 168 | } |
Osamu Nakamura |
0:ad834d403a8c | 169 | |
Osamu Nakamura |
0:ad834d403a8c | 170 | // DEBUG |
Osamu Nakamura |
0:ad834d403a8c | 171 | logger.logging("Endpoint: Connector URL: %s CoAP port: %u",url,port); |
Osamu Nakamura |
0:ad834d403a8c | 172 | |
Osamu Nakamura |
0:ad834d403a8c | 173 | // return the port |
Osamu Nakamura |
0:ad834d403a8c | 174 | return port; |
Osamu Nakamura |
0:ad834d403a8c | 175 | } |