Pelion workshop with Grove sensors.

Committer:
Osamu Koizumi
Date:
Thu Mar 07 10:43:02 2019 +0900
Revision:
26:8e49e7ebefa7
Parent:
18:49062a0d117e
Added Grove sensors.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
maclobdell 11:ae1f6fe932dc 1 // ----------------------------------------------------------------------------
maclobdell 11:ae1f6fe932dc 2 // Copyright 2016-2018 ARM Ltd.
maclobdell 11:ae1f6fe932dc 3 //
maclobdell 11:ae1f6fe932dc 4 // SPDX-License-Identifier: Apache-2.0
maclobdell 11:ae1f6fe932dc 5 //
maclobdell 11:ae1f6fe932dc 6 // Licensed under the Apache License, Version 2.0 (the "License");
maclobdell 11:ae1f6fe932dc 7 // you may not use this file except in compliance with the License.
maclobdell 11:ae1f6fe932dc 8 // You may obtain a copy of the License at
maclobdell 11:ae1f6fe932dc 9 //
maclobdell 11:ae1f6fe932dc 10 // http://www.apache.org/licenses/LICENSE-2.0
maclobdell 11:ae1f6fe932dc 11 //
maclobdell 11:ae1f6fe932dc 12 // Unless required by applicable law or agreed to in writing, software
maclobdell 11:ae1f6fe932dc 13 // distributed under the License is distributed on an "AS IS" BASIS,
maclobdell 11:ae1f6fe932dc 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
maclobdell 11:ae1f6fe932dc 15 // See the License for the specific language governing permissions and
maclobdell 11:ae1f6fe932dc 16 // limitations under the License.
maclobdell 11:ae1f6fe932dc 17 // ----------------------------------------------------------------------------
maclobdell 11:ae1f6fe932dc 18
maclobdell 11:ae1f6fe932dc 19 #include "mbed.h"
maclobdell 11:ae1f6fe932dc 20 #include "simple-mbed-cloud-client.h"
maclobdell 11:ae1f6fe932dc 21 #include "FATFileSystem.h"
maclobdell 11:ae1f6fe932dc 22
Osamu Koizumi 26:8e49e7ebefa7 23 // To enable the GROVE sensors, uncomment the next line.
Osamu Koizumi 26:8e49e7ebefa7 24 //#define ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 25
maclobdell 11:ae1f6fe932dc 26 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
maclobdell 11:ae1f6fe932dc 27 // 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
maclobdell 11:ae1f6fe932dc 28 EventQueue eventQueue;
maclobdell 11:ae1f6fe932dc 29 Thread thread1;
maclobdell 11:ae1f6fe932dc 30
maclobdell 18:49062a0d117e 31 // Default block device
maclobdell 18:49062a0d117e 32 BlockDevice* bd = BlockDevice::get_default_instance();
maclobdell 18:49062a0d117e 33 FATFileSystem fs("sd", bd);
maclobdell 18:49062a0d117e 34
maclobdell 18:49062a0d117e 35 // Default network interface object
maclobdell 18:49062a0d117e 36 NetworkInterface *net;
maclobdell 18:49062a0d117e 37
Osamu Koizumi 26:8e49e7ebefa7 38 InterruptIn sw2(SW2); // A button on the board.
Osamu Koizumi 26:8e49e7ebefa7 39 DigitalOut led2(LED2); // LED on the board.
maclobdell 11:ae1f6fe932dc 40
maclobdell 11:ae1f6fe932dc 41 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
maclobdell 11:ae1f6fe932dc 42 MbedCloudClientResource *button_res;
maclobdell 11:ae1f6fe932dc 43 MbedCloudClientResource *pattern_res;
Osamu Koizumi 26:8e49e7ebefa7 44 #ifdef ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 45 MbedCloudClientResource *temperature_res;
Osamu Koizumi 26:8e49e7ebefa7 46 MbedCloudClientResource *illuminance_res;
Osamu Koizumi 26:8e49e7ebefa7 47 MbedCloudClientResource *buzzer_res;
Osamu Koizumi 26:8e49e7ebefa7 48 #endif // ENABLE_GROVE
maclobdell 11:ae1f6fe932dc 49
maclobdell 11:ae1f6fe932dc 50 static bool button_pressed = false;
maclobdell 11:ae1f6fe932dc 51 static int button_count = 0;
maclobdell 11:ae1f6fe932dc 52
maclobdell 11:ae1f6fe932dc 53 void button_press() {
maclobdell 11:ae1f6fe932dc 54 button_pressed = true;
maclobdell 11:ae1f6fe932dc 55 ++button_count;
maclobdell 11:ae1f6fe932dc 56 button_res->set_value(button_count);
maclobdell 11:ae1f6fe932dc 57 }
maclobdell 11:ae1f6fe932dc 58
maclobdell 11:ae1f6fe932dc 59 /**
maclobdell 11:ae1f6fe932dc 60 * PUT handler
maclobdell 11:ae1f6fe932dc 61 * @param resource The resource that triggered the callback
maclobdell 11:ae1f6fe932dc 62 * @param newValue Updated value for the resource
maclobdell 11:ae1f6fe932dc 63 */
maclobdell 11:ae1f6fe932dc 64 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
maclobdell 11:ae1f6fe932dc 65 printf("PUT received, new value: %s\n", newValue.c_str());
maclobdell 11:ae1f6fe932dc 66 }
maclobdell 11:ae1f6fe932dc 67
maclobdell 11:ae1f6fe932dc 68 /**
maclobdell 11:ae1f6fe932dc 69 * POST handler
maclobdell 11:ae1f6fe932dc 70 * @param resource The resource that triggered the callback
maclobdell 11:ae1f6fe932dc 71 * @param buffer If a body was passed to the POST function, this contains the data.
maclobdell 11:ae1f6fe932dc 72 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
maclobdell 11:ae1f6fe932dc 73 * @param size Size of the body
maclobdell 11:ae1f6fe932dc 74 */
maclobdell 11:ae1f6fe932dc 75 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
maclobdell 11:ae1f6fe932dc 76 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
maclobdell 11:ae1f6fe932dc 77
maclobdell 11:ae1f6fe932dc 78 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
maclobdell 11:ae1f6fe932dc 79
maclobdell 11:ae1f6fe932dc 80 // Parse the pattern string, and toggle the LED in that pattern
maclobdell 11:ae1f6fe932dc 81 string s = std::string(pattern_res->get_value().c_str());
maclobdell 11:ae1f6fe932dc 82 size_t i = 0;
maclobdell 11:ae1f6fe932dc 83 size_t pos = s.find(':');
maclobdell 11:ae1f6fe932dc 84 while (pos != string::npos) {
maclobdell 11:ae1f6fe932dc 85 wait_ms(atoi(s.substr(i, pos - i).c_str()));
maclobdell 11:ae1f6fe932dc 86 augmentedLed = !augmentedLed;
maclobdell 11:ae1f6fe932dc 87
maclobdell 11:ae1f6fe932dc 88 i = ++pos;
maclobdell 11:ae1f6fe932dc 89 pos = s.find(':', pos);
maclobdell 11:ae1f6fe932dc 90
maclobdell 11:ae1f6fe932dc 91 if (pos == string::npos) {
maclobdell 11:ae1f6fe932dc 92 wait_ms(atoi(s.substr(i, s.length()).c_str()));
maclobdell 11:ae1f6fe932dc 93 augmentedLed = !augmentedLed;
maclobdell 11:ae1f6fe932dc 94 }
maclobdell 11:ae1f6fe932dc 95 }
maclobdell 11:ae1f6fe932dc 96 }
maclobdell 11:ae1f6fe932dc 97
maclobdell 11:ae1f6fe932dc 98 /**
Osamu Koizumi 26:8e49e7ebefa7 99 * POST handler. Beeps buzzer for a certain time when any data comes.
Osamu Koizumi 26:8e49e7ebefa7 100 */
Osamu Koizumi 26:8e49e7ebefa7 101 #ifdef ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 102 void buzzer_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
Osamu Koizumi 26:8e49e7ebefa7 103 const int BUZZER_ON = 1; // Polarity of the digital pin to beep the buzzer.
Osamu Koizumi 26:8e49e7ebefa7 104 const int BUZZER_OFF = 0; // Polarity of the digital pin to stop the buzzer.
Osamu Koizumi 26:8e49e7ebefa7 105 const int SOUND_DURATION = 500; // Duration
Osamu Koizumi 26:8e49e7ebefa7 106 static DigitalOut buzzer(D3, BUZZER_OFF); // Buzzer must be connected to D3.
Osamu Koizumi 26:8e49e7ebefa7 107 printf("POST received. Going to beep the buzzer for %d ms.\n", SOUND_DURATION);
Osamu Koizumi 26:8e49e7ebefa7 108
Osamu Koizumi 26:8e49e7ebefa7 109 buzzer = BUZZER_ON;
Osamu Koizumi 26:8e49e7ebefa7 110 ThisThread::sleep_for(SOUND_DURATION);
Osamu Koizumi 26:8e49e7ebefa7 111 buzzer = BUZZER_OFF;
Osamu Koizumi 26:8e49e7ebefa7 112 }
Osamu Koizumi 26:8e49e7ebefa7 113 #endif // ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 114
Osamu Koizumi 26:8e49e7ebefa7 115 /**
maclobdell 11:ae1f6fe932dc 116 * Notification callback handler
maclobdell 11:ae1f6fe932dc 117 * @param resource The resource that triggered the callback
maclobdell 11:ae1f6fe932dc 118 * @param status The delivery status of the notification
maclobdell 11:ae1f6fe932dc 119 */
maclobdell 11:ae1f6fe932dc 120 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
maclobdell 11:ae1f6fe932dc 121 printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
maclobdell 11:ae1f6fe932dc 122 }
maclobdell 11:ae1f6fe932dc 123
maclobdell 11:ae1f6fe932dc 124 /**
maclobdell 11:ae1f6fe932dc 125 * Registration callback handler
maclobdell 11:ae1f6fe932dc 126 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
maclobdell 11:ae1f6fe932dc 127 */
maclobdell 11:ae1f6fe932dc 128 void registered(const ConnectorClientEndpointInfo *endpoint) {
maclobdell 11:ae1f6fe932dc 129 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
maclobdell 11:ae1f6fe932dc 130 }
maclobdell 11:ae1f6fe932dc 131
Osamu Koizumi 26:8e49e7ebefa7 132 /**
Osamu Koizumi 26:8e49e7ebefa7 133 * Get the sensor values.
Osamu Koizumi 26:8e49e7ebefa7 134 */
Osamu Koizumi 26:8e49e7ebefa7 135 #ifdef ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 136 void update_sensors() {
Osamu Koizumi 26:8e49e7ebefa7 137 // Get temperature sensor value.
Osamu Koizumi 26:8e49e7ebefa7 138 // See how to covert AD value to temperature:
Osamu Koizumi 26:8e49e7ebefa7 139 // http://wiki.seeedstudio.com/Grove-Temperature_Sensor_V1.2/
Osamu Koizumi 26:8e49e7ebefa7 140 // Conversion formula contains redundancy, but not corrected here.
Osamu Koizumi 26:8e49e7ebefa7 141 const int B = 4275; // B value of the thermistor
Osamu Koizumi 26:8e49e7ebefa7 142 const int R0 = 100000; // R0 = 100k
Osamu Koizumi 26:8e49e7ebefa7 143 static AnalogIn adc_temperature(A0); // temperature sensor must be connected to A0
Osamu Koizumi 26:8e49e7ebefa7 144 float a = adc_temperature.read()*1023.0;
Osamu Koizumi 26:8e49e7ebefa7 145 float R = 1023.0/a-1.0;
Osamu Koizumi 26:8e49e7ebefa7 146 R = R0*R;
Osamu Koizumi 26:8e49e7ebefa7 147
Osamu Koizumi 26:8e49e7ebefa7 148 float temperature = 1.0/(log(R/R0)/B+1/298.15) - 273.15;
Osamu Koizumi 26:8e49e7ebefa7 149
Osamu Koizumi 26:8e49e7ebefa7 150 // Get ambient light luminosity.
Osamu Koizumi 26:8e49e7ebefa7 151 // http://wiki.seeedstudio.com/Grove-Light_Sensor/
Osamu Koizumi 26:8e49e7ebefa7 152 static AnalogIn adc_illuminance(A1);
Osamu Koizumi 26:8e49e7ebefa7 153 uint16_t illuminance = adc_illuminance.read_u16();
Osamu Koizumi 26:8e49e7ebefa7 154
Osamu Koizumi 26:8e49e7ebefa7 155 // Set the obtained sensor values.
Osamu Koizumi 26:8e49e7ebefa7 156 temperature_res->set_value(temperature);
Osamu Koizumi 26:8e49e7ebefa7 157 illuminance_res->set_value(illuminance);
Osamu Koizumi 26:8e49e7ebefa7 158
Osamu Koizumi 26:8e49e7ebefa7 159 printf("Sensor values: temperature %4.1f C, illuminance %d\n", temperature, illuminance);
Osamu Koizumi 26:8e49e7ebefa7 160 }
Osamu Koizumi 26:8e49e7ebefa7 161 #endif // ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 162
Osamu Koizumi 26:8e49e7ebefa7 163
maclobdell 11:ae1f6fe932dc 164 int main(void) {
maclobdell 11:ae1f6fe932dc 165 printf("Starting Simple Mbed Cloud Client example\n");
maclobdell 11:ae1f6fe932dc 166 printf("Connecting to the network using Ethernet...\n");
maclobdell 11:ae1f6fe932dc 167
Osamu Koizumi 26:8e49e7ebefa7 168 // Setup the button
Osamu Koizumi 26:8e49e7ebefa7 169 sw2.mode(PullUp);
Osamu Koizumi 26:8e49e7ebefa7 170 // If the SW2 button on the board is pushed at the init, format the storage.
Osamu Koizumi 26:8e49e7ebefa7 171 // Note that the polarity of SW2 is active low. i.e. 0 is pushed.
Osamu Koizumi 26:8e49e7ebefa7 172 if(sw2 == 0) {
Osamu Koizumi 26:8e49e7ebefa7 173 printf("SW2 is being pushed. Format storage...");
Osamu Koizumi 26:8e49e7ebefa7 174 if(fs.format(bd) != 0) {
Osamu Koizumi 26:8e49e7ebefa7 175 printf("Failed to format the storage.");
Osamu Koizumi 26:8e49e7ebefa7 176 return -1;
Osamu Koizumi 26:8e49e7ebefa7 177 } else {
Osamu Koizumi 26:8e49e7ebefa7 178 printf("The starage was formatted. Program stops here.\n");
Osamu Koizumi 26:8e49e7ebefa7 179 // Turn on the blue LED to inform the storage was formatted successfully.
Osamu Koizumi 26:8e49e7ebefa7 180 DigitalOut tmp(LED_BLUE, 1);
Osamu Koizumi 26:8e49e7ebefa7 181 return 0;
Osamu Koizumi 26:8e49e7ebefa7 182 }
Osamu Koizumi 26:8e49e7ebefa7 183 }
Osamu Koizumi 26:8e49e7ebefa7 184
maclobdell 11:ae1f6fe932dc 185 // Connect to the internet (DHCP is expected to be on)
maclobdell 18:49062a0d117e 186 net = NetworkInterface::get_default_instance();
maclobdell 11:ae1f6fe932dc 187
maclobdell 18:49062a0d117e 188 nsapi_error_t status = net->connect();
maclobdell 18:49062a0d117e 189
maclobdell 18:49062a0d117e 190 if (status != NSAPI_ERROR_OK) {
maclobdell 11:ae1f6fe932dc 191 printf("Connecting to the network failed %d!\n", status);
maclobdell 11:ae1f6fe932dc 192 return -1;
maclobdell 11:ae1f6fe932dc 193 }
maclobdell 11:ae1f6fe932dc 194
maclobdell 18:49062a0d117e 195 printf("Connected to the network successfully. IP address: %s\n", net->get_ip_address());
maclobdell 11:ae1f6fe932dc 196
maclobdell 11:ae1f6fe932dc 197 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
maclobdell 18:49062a0d117e 198 SimpleMbedCloudClient client(net, bd, &fs);
maclobdell 11:ae1f6fe932dc 199 int client_status = client.init();
maclobdell 11:ae1f6fe932dc 200 if (client_status != 0) {
maclobdell 11:ae1f6fe932dc 201 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
maclobdell 11:ae1f6fe932dc 202 return -1;
maclobdell 11:ae1f6fe932dc 203 }
maclobdell 11:ae1f6fe932dc 204
maclobdell 11:ae1f6fe932dc 205 // Creating resources, which can be written or read from the cloud
maclobdell 11:ae1f6fe932dc 206 button_res = client.create_resource("3200/0/5501", "button_count");
maclobdell 11:ae1f6fe932dc 207 button_res->set_value(0);
maclobdell 11:ae1f6fe932dc 208 button_res->methods(M2MMethod::GET);
maclobdell 11:ae1f6fe932dc 209 button_res->observable(true);
maclobdell 11:ae1f6fe932dc 210 button_res->attach_notification_callback(button_callback);
maclobdell 11:ae1f6fe932dc 211
maclobdell 11:ae1f6fe932dc 212 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
maclobdell 11:ae1f6fe932dc 213 pattern_res->set_value("500:500:500:500:500:500:500:500");
maclobdell 11:ae1f6fe932dc 214 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
maclobdell 11:ae1f6fe932dc 215 pattern_res->attach_put_callback(pattern_updated);
maclobdell 11:ae1f6fe932dc 216
maclobdell 11:ae1f6fe932dc 217 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
maclobdell 11:ae1f6fe932dc 218 blink_res->methods(M2MMethod::POST);
maclobdell 11:ae1f6fe932dc 219 blink_res->attach_post_callback(blink_callback);
maclobdell 11:ae1f6fe932dc 220
Osamu Koizumi 26:8e49e7ebefa7 221 #ifdef ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 222 temperature_res = client.create_resource("3303/0/5700", "temperature");
Osamu Koizumi 26:8e49e7ebefa7 223 temperature_res->set_value(0.0f);
Osamu Koizumi 26:8e49e7ebefa7 224 temperature_res->methods(M2MMethod::GET);
Osamu Koizumi 26:8e49e7ebefa7 225 temperature_res->observable(true);
Osamu Koizumi 26:8e49e7ebefa7 226
Osamu Koizumi 26:8e49e7ebefa7 227 illuminance_res = client.create_resource("3301/0/5700", "illuminance");
Osamu Koizumi 26:8e49e7ebefa7 228 illuminance_res->set_value(0.0f);
Osamu Koizumi 26:8e49e7ebefa7 229 illuminance_res->methods(M2MMethod::GET);
Osamu Koizumi 26:8e49e7ebefa7 230 illuminance_res->observable(true);
Osamu Koizumi 26:8e49e7ebefa7 231
Osamu Koizumi 26:8e49e7ebefa7 232 buzzer_res = client.create_resource("3201/1/5550", "buzzer");
Osamu Koizumi 26:8e49e7ebefa7 233 buzzer_res->methods(M2MMethod::POST);
Osamu Koizumi 26:8e49e7ebefa7 234 buzzer_res->attach_post_callback(buzzer_callback);
Osamu Koizumi 26:8e49e7ebefa7 235 #endif // ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 236
maclobdell 11:ae1f6fe932dc 237 printf("Initialized Mbed Cloud Client. Registering...\n");
maclobdell 11:ae1f6fe932dc 238
maclobdell 11:ae1f6fe932dc 239 // Callback that fires when registering is complete
maclobdell 11:ae1f6fe932dc 240 client.on_registered(&registered);
maclobdell 11:ae1f6fe932dc 241
maclobdell 11:ae1f6fe932dc 242 // Register with Mbed Cloud
maclobdell 11:ae1f6fe932dc 243 client.register_and_connect();
maclobdell 11:ae1f6fe932dc 244
maclobdell 11:ae1f6fe932dc 245 // The button fall handler is placed in the event queue so it will run in
maclobdell 11:ae1f6fe932dc 246 // thread context instead of ISR context, which allows safely updating the cloud resource
Osamu Koizumi 26:8e49e7ebefa7 247 sw2.fall(eventQueue.event(&button_press));
Osamu Koizumi 26:8e49e7ebefa7 248 // The button connected to the GROVE shield acts the same as SW2.
Osamu Koizumi 26:8e49e7ebefa7 249 InterruptIn extBtn(D2);
Osamu Koizumi 26:8e49e7ebefa7 250 extBtn.fall(eventQueue.event(&button_press));
Osamu Koizumi 26:8e49e7ebefa7 251 button_count = 0;
Osamu Koizumi 26:8e49e7ebefa7 252
Osamu Koizumi 26:8e49e7ebefa7 253 #ifdef ENABLE_GROVE
Osamu Koizumi 26:8e49e7ebefa7 254 Ticker timer;
Osamu Koizumi 26:8e49e7ebefa7 255 timer.attach(eventQueue.event(update_sensors), 3.0);
Osamu Koizumi 26:8e49e7ebefa7 256 #endif /* ENABLE_SENSORS */
maclobdell 11:ae1f6fe932dc 257
maclobdell 11:ae1f6fe932dc 258 // Start the event queue in a separate thread so the main thread continues
maclobdell 11:ae1f6fe932dc 259 thread1.start(callback(&eventQueue, &EventQueue::dispatch_forever));
maclobdell 11:ae1f6fe932dc 260
maclobdell 11:ae1f6fe932dc 261 while(1)
maclobdell 11:ae1f6fe932dc 262 {
maclobdell 11:ae1f6fe932dc 263 wait_ms(100);
maclobdell 11:ae1f6fe932dc 264
maclobdell 11:ae1f6fe932dc 265 if (button_pressed) {
maclobdell 11:ae1f6fe932dc 266 button_pressed = false;
maclobdell 11:ae1f6fe932dc 267 printf("button clicked %d times\r\n", button_count);
maclobdell 11:ae1f6fe932dc 268 }
maclobdell 11:ae1f6fe932dc 269
maclobdell 11:ae1f6fe932dc 270 }
maclobdell 11:ae1f6fe932dc 271 }