Taking photo using GR-LYCHEE through Pelion Device Management.

This firmware project works for GR-LYCHEE and GR-PEACH.

Steps to build this firmware with Mbed CLI

Import project

$ mbed import http://os.mbed.com/users/coisme/code/Pelion-GR-LYCHEE-camera-firmware/

Setting

  • Ethernet (GR-PEACH only)
    • Change the file name mbed_app.RZ_A1H_Ethernet.json to mbed_app.json
  • Wi-Fi
    • Change the Wi-Fi setting in mbed_app.json, i.e. nsapi.default-wifi-ssid and nsapi.default-wifi-password

If you haven't set your Pelion API key, run the following command.

$ mbed config -G CLOUD_SDK_API_KEY <API_KEY>

Setting for Pelion Device Management

$ mbed dm init -d "example.com" --model-name "PELION_DEMO" -q --force

Compile

For GR-LYCHEE:

$ mbed compile -t GCC_ARM -m GR_LYCHEE

For GR-PEACH:

$ mbed compile -t GCC_ARM -m RZ_A1H

Write the created .bin file to your GR-LYCHEE/PEACH.

That's it! :-)

LED Status Indicator

LEDs next to UB0 show the status of your GR-LYCHEE/PEACH.

LED#GR-LYCHEEGR-PEACHStatusDescription
LED1greenredNormalTurned on after the device is registered to Pelion Device Management successfully.
LED2yellowgreenErrorTurned on when the network initialization failed. Check your Wi-Fi setting.
LED3orangeblueErrorTurned on when the Pelion Device Management Client initialization failed. Check your SD card.
LED4redredErrorTurned on when the network is disconnected. Check your Wi-Fi network status.

Clear device identity

If you want to clear the device's identity, connect to the device via serial terminal. Then input r command. The device flushes the identity storage, then reboot.

Known Issues

  • client_error(6) -> Client in reconnection mode NetworkError appears when connecting to network, but eventually connection will be established.
  • Warning message "MBEDTLS_TEST_NULL_ENTROPY has been enabled. This configuration is not secure and is not suitable for production use" appears when compiling the project for GR-PEACH. This can be ignored in development stage.
Committer:
Osamu Koizumi
Date:
Fri Feb 15 12:53:56 2019 +0900
Revision:
4:7c58c47eca55
Parent:
0:6d2053b84a92
Child:
7:826cdcc3c8ec
Added camera resource, currently sending static photo data.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
MACRUM 0:6d2053b84a92 1 // ----------------------------------------------------------------------------
MACRUM 0:6d2053b84a92 2 // Copyright 2016-2018 ARM Ltd.
MACRUM 0:6d2053b84a92 3 //
MACRUM 0:6d2053b84a92 4 // SPDX-License-Identifier: Apache-2.0
MACRUM 0:6d2053b84a92 5 //
MACRUM 0:6d2053b84a92 6 // Licensed under the Apache License, Version 2.0 (the "License");
MACRUM 0:6d2053b84a92 7 // you may not use this file except in compliance with the License.
MACRUM 0:6d2053b84a92 8 // You may obtain a copy of the License at
MACRUM 0:6d2053b84a92 9 //
MACRUM 0:6d2053b84a92 10 // http://www.apache.org/licenses/LICENSE-2.0
MACRUM 0:6d2053b84a92 11 //
MACRUM 0:6d2053b84a92 12 // Unless required by applicable law or agreed to in writing, software
MACRUM 0:6d2053b84a92 13 // distributed under the License is distributed on an "AS IS" BASIS,
MACRUM 0:6d2053b84a92 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
MACRUM 0:6d2053b84a92 15 // See the License for the specific language governing permissions and
MACRUM 0:6d2053b84a92 16 // limitations under the License.
MACRUM 0:6d2053b84a92 17 // ----------------------------------------------------------------------------
MACRUM 0:6d2053b84a92 18 #ifndef MBED_TEST_MODE
MACRUM 0:6d2053b84a92 19
MACRUM 0:6d2053b84a92 20 #include "mbed.h"
MACRUM 0:6d2053b84a92 21 #include "simple-mbed-cloud-client.h"
MACRUM 0:6d2053b84a92 22 #include "FATFileSystem.h"
MACRUM 0:6d2053b84a92 23
Osamu Koizumi 4:7c58c47eca55 24 // Include photo and icon jpeg data
Osamu Koizumi 4:7c58c47eca55 25 #include "icon.h"
Osamu Koizumi 4:7c58c47eca55 26 #include "photo.h"
Osamu Koizumi 4:7c58c47eca55 27
MACRUM 0:6d2053b84a92 28 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
MACRUM 0:6d2053b84a92 29 // This is great because things such as network operations are illegal in ISR, so updating a resource in a button's fall() function is not allowed
MACRUM 0:6d2053b84a92 30 EventQueue eventQueue;
MACRUM 0:6d2053b84a92 31
MACRUM 0:6d2053b84a92 32 // Default block device
MACRUM 0:6d2053b84a92 33 BlockDevice *bd = BlockDevice::get_default_instance();
MACRUM 0:6d2053b84a92 34 FATFileSystem fs("fs");
MACRUM 0:6d2053b84a92 35
MACRUM 0:6d2053b84a92 36 // Default network interface object
MACRUM 0:6d2053b84a92 37 NetworkInterface *net = NetworkInterface::get_default_instance();
MACRUM 0:6d2053b84a92 38
MACRUM 0:6d2053b84a92 39 InterruptIn btn(USER_BUTTON0);
MACRUM 0:6d2053b84a92 40 // Declaring pointers for access to Pelion Device Management Client resources outside of main()
MACRUM 0:6d2053b84a92 41 MbedCloudClientResource *button_res;
MACRUM 0:6d2053b84a92 42 MbedCloudClientResource *pattern_res;
Osamu Koizumi 4:7c58c47eca55 43 MbedCloudClientResource *res_camera_capture;
MACRUM 0:6d2053b84a92 44
MACRUM 0:6d2053b84a92 45 void button_press() {
MACRUM 0:6d2053b84a92 46 int v = button_res->get_value_int() + 1;
MACRUM 0:6d2053b84a92 47
MACRUM 0:6d2053b84a92 48 button_res->set_value(v);
MACRUM 0:6d2053b84a92 49
MACRUM 0:6d2053b84a92 50 printf("User button clicked %d times\n", v);
Osamu Koizumi 4:7c58c47eca55 51 M2MResource* m2m_res = res_camera_capture->get_m2m_resource();
Osamu Koizumi 4:7c58c47eca55 52 if(v % 2 == 0) {
Osamu Koizumi 4:7c58c47eca55 53 m2m_res->set_value((const uint8_t *)data_icon, data_icon_len);
Osamu Koizumi 4:7c58c47eca55 54 } else {
Osamu Koizumi 4:7c58c47eca55 55 m2m_res->set_value((const uint8_t *)data_photo, data_photo_len);
Osamu Koizumi 4:7c58c47eca55 56 }
Osamu Koizumi 4:7c58c47eca55 57 }
Osamu Koizumi 4:7c58c47eca55 58
Osamu Koizumi 4:7c58c47eca55 59 void res_camera_capture_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status)
Osamu Koizumi 4:7c58c47eca55 60 {
Osamu Koizumi 4:7c58c47eca55 61 printf("camera notification, status: %s (%d)\n",
Osamu Koizumi 4:7c58c47eca55 62 MbedCloudClientResource::delivery_status_to_string(status), status);
MACRUM 0:6d2053b84a92 63 }
MACRUM 0:6d2053b84a92 64
MACRUM 0:6d2053b84a92 65 /**
MACRUM 0:6d2053b84a92 66 * PUT handler
MACRUM 0:6d2053b84a92 67 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 68 * @param newValue Updated value for the resource
MACRUM 0:6d2053b84a92 69 */
MACRUM 0:6d2053b84a92 70 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
MACRUM 0:6d2053b84a92 71 printf("PUT received, new value: %s\n", newValue.c_str());
MACRUM 0:6d2053b84a92 72 }
MACRUM 0:6d2053b84a92 73
MACRUM 0:6d2053b84a92 74 /**
MACRUM 0:6d2053b84a92 75 * POST handler
MACRUM 0:6d2053b84a92 76 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 77 * @param buffer If a body was passed to the POST function, this contains the data.
MACRUM 0:6d2053b84a92 78 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
MACRUM 0:6d2053b84a92 79 * @param size Size of the body
MACRUM 0:6d2053b84a92 80 */
MACRUM 0:6d2053b84a92 81 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
MACRUM 0:6d2053b84a92 82 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
MACRUM 0:6d2053b84a92 83
MACRUM 0:6d2053b84a92 84 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
MACRUM 0:6d2053b84a92 85
MACRUM 0:6d2053b84a92 86 // Parse the pattern string, and toggle the LED in that pattern
MACRUM 0:6d2053b84a92 87 string s = std::string(pattern_res->get_value().c_str());
MACRUM 0:6d2053b84a92 88 size_t i = 0;
MACRUM 0:6d2053b84a92 89 size_t pos = s.find(':');
MACRUM 0:6d2053b84a92 90 while (pos != string::npos) {
MACRUM 0:6d2053b84a92 91 wait_ms(atoi(s.substr(i, pos - i).c_str()));
MACRUM 0:6d2053b84a92 92 augmentedLed = !augmentedLed;
MACRUM 0:6d2053b84a92 93
MACRUM 0:6d2053b84a92 94 i = ++pos;
MACRUM 0:6d2053b84a92 95 pos = s.find(':', pos);
MACRUM 0:6d2053b84a92 96
MACRUM 0:6d2053b84a92 97 if (pos == string::npos) {
MACRUM 0:6d2053b84a92 98 wait_ms(atoi(s.substr(i, s.length()).c_str()));
MACRUM 0:6d2053b84a92 99 augmentedLed = !augmentedLed;
MACRUM 0:6d2053b84a92 100 }
MACRUM 0:6d2053b84a92 101 }
MACRUM 0:6d2053b84a92 102 }
MACRUM 0:6d2053b84a92 103
MACRUM 0:6d2053b84a92 104 /**
MACRUM 0:6d2053b84a92 105 * Notification callback handler
MACRUM 0:6d2053b84a92 106 * @param resource The resource that triggered the callback
MACRUM 0:6d2053b84a92 107 * @param status The delivery status of the notification
MACRUM 0:6d2053b84a92 108 */
MACRUM 0:6d2053b84a92 109 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
MACRUM 0:6d2053b84a92 110 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
MACRUM 0:6d2053b84a92 111 }
MACRUM 0:6d2053b84a92 112
MACRUM 0:6d2053b84a92 113 /**
MACRUM 0:6d2053b84a92 114 * Registration callback handler
MACRUM 0:6d2053b84a92 115 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
MACRUM 0:6d2053b84a92 116 */
MACRUM 0:6d2053b84a92 117 void registered(const ConnectorClientEndpointInfo *endpoint) {
MACRUM 0:6d2053b84a92 118 printf("Connected to Pelion Device Management. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
MACRUM 0:6d2053b84a92 119 }
MACRUM 0:6d2053b84a92 120
MACRUM 0:6d2053b84a92 121 int main(void) {
MACRUM 0:6d2053b84a92 122 printf("Starting Simple Pelion Device Management Client example\n");
MACRUM 0:6d2053b84a92 123 printf("Connecting to the network...\n");
MACRUM 0:6d2053b84a92 124
MACRUM 0:6d2053b84a92 125 // Connect to the internet (DHCP is expected to be on)
MACRUM 0:6d2053b84a92 126 nsapi_error_t status = net->connect();
MACRUM 0:6d2053b84a92 127
MACRUM 0:6d2053b84a92 128 if (status != NSAPI_ERROR_OK) {
MACRUM 0:6d2053b84a92 129 printf("Connecting to the network failed %d!\n", status);
MACRUM 0:6d2053b84a92 130 return -1;
MACRUM 0:6d2053b84a92 131 }
MACRUM 0:6d2053b84a92 132
MACRUM 0:6d2053b84a92 133 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
MACRUM 0:6d2053b84a92 134
MACRUM 0:6d2053b84a92 135 // SimpleMbedCloudClient handles registering over LwM2M to Pelion Device Management
MACRUM 0:6d2053b84a92 136 SimpleMbedCloudClient client(net, bd, &fs);
MACRUM 0:6d2053b84a92 137 int client_status = client.init();
MACRUM 0:6d2053b84a92 138 if (client_status != 0) {
MACRUM 0:6d2053b84a92 139 printf("Pelion Client initialization failed (%d)\n", client_status);
MACRUM 0:6d2053b84a92 140 return -1;
MACRUM 0:6d2053b84a92 141 }
MACRUM 0:6d2053b84a92 142
MACRUM 0:6d2053b84a92 143 // Creating resources, which can be written or read from the cloud
MACRUM 0:6d2053b84a92 144 button_res = client.create_resource("3200/0/5501", "button_count");
MACRUM 0:6d2053b84a92 145 button_res->set_value(0);
MACRUM 0:6d2053b84a92 146 button_res->methods(M2MMethod::GET);
MACRUM 0:6d2053b84a92 147 button_res->observable(true);
MACRUM 0:6d2053b84a92 148 button_res->attach_notification_callback(button_callback);
MACRUM 0:6d2053b84a92 149
MACRUM 0:6d2053b84a92 150 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
MACRUM 0:6d2053b84a92 151 pattern_res->set_value("500:500:500:500:500:500:500:500");
MACRUM 0:6d2053b84a92 152 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
MACRUM 0:6d2053b84a92 153 pattern_res->attach_put_callback(pattern_updated);
MACRUM 0:6d2053b84a92 154
Osamu Koizumi 4:7c58c47eca55 155 res_camera_capture = client.create_resource("3200/0/4014", "CameraCapture");
Osamu Koizumi 4:7c58c47eca55 156 res_camera_capture->set_value(0);
Osamu Koizumi 4:7c58c47eca55 157 res_camera_capture->methods(M2MMethod::GET);
Osamu Koizumi 4:7c58c47eca55 158 res_camera_capture->observable(true);
Osamu Koizumi 4:7c58c47eca55 159 res_camera_capture->attach_notification_callback(res_camera_capture_callback);
Osamu Koizumi 4:7c58c47eca55 160
MACRUM 0:6d2053b84a92 161 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
MACRUM 0:6d2053b84a92 162 blink_res->methods(M2MMethod::POST);
MACRUM 0:6d2053b84a92 163 blink_res->attach_post_callback(blink_callback);
MACRUM 0:6d2053b84a92 164
MACRUM 0:6d2053b84a92 165 printf("Initialized Pelion Client. Registering...\n");
MACRUM 0:6d2053b84a92 166
MACRUM 0:6d2053b84a92 167 // Callback that fires when registering is complete
MACRUM 0:6d2053b84a92 168 client.on_registered(&registered);
MACRUM 0:6d2053b84a92 169
MACRUM 0:6d2053b84a92 170 // Register with Pelion Device Management
MACRUM 0:6d2053b84a92 171 client.register_and_connect();
MACRUM 0:6d2053b84a92 172
MACRUM 0:6d2053b84a92 173 // Setup the button
MACRUM 0:6d2053b84a92 174 btn.mode(PullUp);
MACRUM 0:6d2053b84a92 175
MACRUM 0:6d2053b84a92 176 // The button fall handler is placed in the event queue so it will run in
MACRUM 0:6d2053b84a92 177 // thread context instead of ISR context, which allows safely updating the cloud resource
MACRUM 0:6d2053b84a92 178 btn.fall(eventQueue.event(&button_press));
MACRUM 0:6d2053b84a92 179
MACRUM 0:6d2053b84a92 180 // You can easily run the eventQueue in a separate thread if required
MACRUM 0:6d2053b84a92 181 eventQueue.dispatch_forever();
MACRUM 0:6d2053b84a92 182 }
MACRUM 0:6d2053b84a92 183 #endif