Simulated product dispenser

Dependencies:   HTS221

Fork of mbed-cloud-workshop-connect-HTS221 by Jim Carver

Committer:
JimCarver
Date:
Mon Oct 22 23:01:04 2018 +0000
Revision:
3:0d3492ebb1a5
Parent:
2:d5f5207e8b2a
Child:
4:e518dde96e59
Improved reliability

Who changed what in which revision?

UserRevisionLine numberNew contents of line
JimCarver 0:6b753f761943 1 // ----------------------------------------------------------------------------
JimCarver 0:6b753f761943 2 // Copyright 2016-2018 ARM Ltd.
JimCarver 0:6b753f761943 3 //
JimCarver 0:6b753f761943 4 // SPDX-License-Identifier: Apache-2.0
JimCarver 0:6b753f761943 5 //
JimCarver 0:6b753f761943 6 // Licensed under the Apache License, Version 2.0 (the "License");
JimCarver 0:6b753f761943 7 // you may not use this file except in compliance with the License.
JimCarver 0:6b753f761943 8 // You may obtain a copy of the License at
JimCarver 0:6b753f761943 9 //
JimCarver 0:6b753f761943 10 // http://www.apache.org/licenses/LICENSE-2.0
JimCarver 0:6b753f761943 11 //
JimCarver 0:6b753f761943 12 // Unless required by applicable law or agreed to in writing, software
JimCarver 0:6b753f761943 13 // distributed under the License is distributed on an "AS IS" BASIS,
JimCarver 0:6b753f761943 14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
JimCarver 0:6b753f761943 15 // See the License for the specific language governing permissions and
JimCarver 0:6b753f761943 16 // limitations under the License.
JimCarver 0:6b753f761943 17 // ----------------------------------------------------------------------------
JimCarver 0:6b753f761943 18
JimCarver 0:6b753f761943 19 #include "mbed.h"
JimCarver 0:6b753f761943 20 #include <stdio.h>
JimCarver 0:6b753f761943 21 #include <errno.h>
JimCarver 0:6b753f761943 22
JimCarver 0:6b753f761943 23 #include "qspi_api.h"
JimCarver 0:6b753f761943 24 #include "QSPI.h"
JimCarver 0:6b753f761943 25 #include "qspi-blockdevice/QSPIFBlockDevice.h"
JimCarver 0:6b753f761943 26 #define DEVICE_QSPI
JimCarver 0:6b753f761943 27
JimCarver 0:6b753f761943 28 #include "FATFileSystem.h"
JimCarver 0:6b753f761943 29 #include "LittleFileSystem.h"
JimCarver 0:6b753f761943 30 #include "simple-mbed-cloud-client.h"
JimCarver 0:6b753f761943 31 // #include "SDBlockDevice.h"
JimCarver 0:6b753f761943 32 #include "ISM43362Interface.h"
JimCarver 0:6b753f761943 33 // #include "SPIFBlockDevice.h" // to use onboard spi flash
JimCarver 0:6b753f761943 34
JimCarver 0:6b753f761943 35 #define WIFI_SSID "mbed"
JimCarver 0:6b753f761943 36 #define WIFI_PASSWORD "workshop-password"
JimCarver 0:6b753f761943 37
JimCarver 0:6b753f761943 38
JimCarver 0:6b753f761943 39
JimCarver 1:521604503e81 40 #include "HTS221/HTS221Sensor.h"
JimCarver 0:6b753f761943 41
JimCarver 1:521604503e81 42
JimCarver 0:6b753f761943 43 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
JimCarver 0:6b753f761943 44 // 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
JimCarver 0:6b753f761943 45 EventQueue eventQueue;
JimCarver 0:6b753f761943 46
JimCarver 0:6b753f761943 47 // Declaring net interface as a global variable instead of local to avoid stack overflow
JimCarver 0:6b753f761943 48 ISM43362Interface net;
JimCarver 0:6b753f761943 49
JimCarver 0:6b753f761943 50 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
JimCarver 0:6b753f761943 51 // SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
JimCarver 0:6b753f761943 52 QSPIFBlockDevice sd(PE_12, PE_13, PE_14, PE_15,PE_10,PE_11,0,8000000);
JimCarver 0:6b753f761943 53
JimCarver 0:6b753f761943 54 // FATFileSystem fs("sd", &bd);
JimCarver 0:6b753f761943 55
JimCarver 0:6b753f761943 56 LittleFileSystem fs("sd");
JimCarver 0:6b753f761943 57
JimCarver 0:6b753f761943 58 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
JimCarver 0:6b753f761943 59 MbedCloudClientResource *button_res;
JimCarver 0:6b753f761943 60 MbedCloudClientResource *pattern_res;
JimCarver 1:521604503e81 61 MbedCloudClientResource *temperature_res;
JimCarver 1:521604503e81 62 MbedCloudClientResource *humidity_res;
JimCarver 1:521604503e81 63 static int v = 0;
JimCarver 0:6b753f761943 64
JimCarver 0:6b753f761943 65 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button
JimCarver 0:6b753f761943 66 void fake_button_press() {
JimCarver 1:521604503e81 67
JimCarver 1:521604503e81 68 button_res->set_value(v++);
JimCarver 1:521604503e81 69 if(v > 1000) v = 0;
JimCarver 1:521604503e81 70 //printf("Simulated button clicked %d times\n", v);
JimCarver 1:521604503e81 71
JimCarver 1:521604503e81 72 }
JimCarver 1:521604503e81 73
JimCarver 1:521604503e81 74 // Manage the HST221 in an independent thread
JimCarver 1:521604503e81 75 Thread HTS221Thread;
JimCarver 0:6b753f761943 76
JimCarver 1:521604503e81 77 void HTS221Handler(void)
JimCarver 1:521604503e81 78 {
JimCarver 1:521604503e81 79 uint8_t id;
JimCarver 1:521604503e81 80 float value1, value2;
JimCarver 1:521604503e81 81 static DevI2C devI2c(PB_11,PB_10); // This defines the processor port pins attached to the I2C bus
JimCarver 1:521604503e81 82 static HTS221Sensor hum_temp(&devI2c);
JimCarver 1:521604503e81 83 hum_temp.init(NULL);
JimCarver 1:521604503e81 84 hum_temp.read_id(&id); // Read the device ID
JimCarver 1:521604503e81 85 printf("\r\n\n\nHTS221 humidity & temperature = 0x%X\r\n", id);
JimCarver 1:521604503e81 86 hum_temp.enable();
JimCarver 1:521604503e81 87 while(1) {
JimCarver 1:521604503e81 88 wait(5); // Update every 5 seconds
JimCarver 1:521604503e81 89 // Update temperature and humidity resources
JimCarver 1:521604503e81 90 hum_temp.get_temperature(&value1);
JimCarver 1:521604503e81 91 hum_temp.get_humidity(&value2);
JimCarver 1:521604503e81 92 temperature_res->set_value(value1);
JimCarver 1:521604503e81 93 humidity_res->set_value(value2);
JimCarver 1:521604503e81 94 }
JimCarver 0:6b753f761943 95 }
JimCarver 0:6b753f761943 96
JimCarver 0:6b753f761943 97 /**
JimCarver 0:6b753f761943 98 * PUT handler
JimCarver 0:6b753f761943 99 * @param resource The resource that triggered the callback
JimCarver 0:6b753f761943 100 * @param newValue Updated value for the resource
JimCarver 0:6b753f761943 101 */
JimCarver 0:6b753f761943 102 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
JimCarver 0:6b753f761943 103 printf("PUT received, new value: %s\n", newValue.c_str());
JimCarver 0:6b753f761943 104 }
JimCarver 0:6b753f761943 105
JimCarver 0:6b753f761943 106 /**
JimCarver 0:6b753f761943 107 * POST handler
JimCarver 0:6b753f761943 108 * @param resource The resource that triggered the callback
JimCarver 0:6b753f761943 109 * @param buffer If a body was passed to the POST function, this contains the data.
JimCarver 0:6b753f761943 110 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
JimCarver 0:6b753f761943 111 * @param size Size of the body
JimCarver 0:6b753f761943 112 */
JimCarver 1:521604503e81 113
JimCarver 1:521604503e81 114
JimCarver 1:521604503e81 115
JimCarver 0:6b753f761943 116 void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
JimCarver 0:6b753f761943 117 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
JimCarver 0:6b753f761943 118
JimCarver 0:6b753f761943 119 static DigitalOut augmentedLed(LED1); // LED that is used for blinking the pattern
JimCarver 0:6b753f761943 120
JimCarver 0:6b753f761943 121 // Parse the pattern string, and toggle the LED in that pattern
JimCarver 0:6b753f761943 122 string s = std::string(pattern_res->get_value().c_str());
JimCarver 0:6b753f761943 123 size_t i = 0;
JimCarver 0:6b753f761943 124 size_t pos = s.find(':');
JimCarver 0:6b753f761943 125 while (pos != string::npos) {
JimCarver 0:6b753f761943 126 wait_ms(atoi(s.substr(i, pos - i).c_str()));
JimCarver 0:6b753f761943 127 augmentedLed = !augmentedLed;
JimCarver 0:6b753f761943 128
JimCarver 0:6b753f761943 129 i = ++pos;
JimCarver 0:6b753f761943 130 pos = s.find(':', pos);
JimCarver 0:6b753f761943 131
JimCarver 0:6b753f761943 132 if (pos == string::npos) {
JimCarver 0:6b753f761943 133 wait_ms(atoi(s.substr(i, s.length()).c_str()));
JimCarver 0:6b753f761943 134 augmentedLed = !augmentedLed;
JimCarver 0:6b753f761943 135 }
JimCarver 0:6b753f761943 136 }
JimCarver 0:6b753f761943 137 }
JimCarver 0:6b753f761943 138
JimCarver 0:6b753f761943 139 /**
JimCarver 0:6b753f761943 140 * Notification callback handler
JimCarver 0:6b753f761943 141 * @param resource The resource that triggered the callback
JimCarver 0:6b753f761943 142 * @param status The delivery status of the notification
JimCarver 0:6b753f761943 143 */
JimCarver 0:6b753f761943 144 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
JimCarver 1:521604503e81 145 printf("Button %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
JimCarver 1:521604503e81 146 }
JimCarver 1:521604503e81 147
JimCarver 1:521604503e81 148 void temperature_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
JimCarver 1:521604503e81 149 printf("Temperature %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
JimCarver 1:521604503e81 150 }
JimCarver 1:521604503e81 151
JimCarver 1:521604503e81 152 void humidity_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
JimCarver 1:521604503e81 153 printf("Humidity %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
JimCarver 0:6b753f761943 154 }
JimCarver 0:6b753f761943 155
JimCarver 0:6b753f761943 156 /**
JimCarver 0:6b753f761943 157 * Registration callback handler
JimCarver 0:6b753f761943 158 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
JimCarver 0:6b753f761943 159 */
JimCarver 0:6b753f761943 160 void registered(const ConnectorClientEndpointInfo *endpoint) {
JimCarver 0:6b753f761943 161 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
JimCarver 2:d5f5207e8b2a 162 Ticker timer;
JimCarver 2:d5f5207e8b2a 163 timer.attach(eventQueue.event(&fake_button_press), 5.0);
JimCarver 2:d5f5207e8b2a 164 HTS221Thread.start(HTS221Handler);
JimCarver 0:6b753f761943 165 }
JimCarver 0:6b753f761943 166
JimCarver 3:0d3492ebb1a5 167
JimCarver 0:6b753f761943 168 int main(void) {
JimCarver 1:521604503e81 169
JimCarver 0:6b753f761943 170 printf("Starting Simple Mbed Cloud Client example\n");
JimCarver 0:6b753f761943 171
JimCarver 0:6b753f761943 172 printf("Checking SDCard is Formatted\r\n");
JimCarver 0:6b753f761943 173 int err = fs.mount(&sd);
JimCarver 0:6b753f761943 174 printf("%s\n", (err ? "Fail :(" : "OK"));
JimCarver 0:6b753f761943 175 if (err) {
JimCarver 0:6b753f761943 176 // Reformat if we can't mount the filesystem
JimCarver 0:6b753f761943 177 // this should only happen on the first boot
JimCarver 0:6b753f761943 178 printf("No filesystem found, formatting... ");
JimCarver 0:6b753f761943 179 fflush(stdout);
JimCarver 0:6b753f761943 180 err = fs.reformat(&sd);
JimCarver 0:6b753f761943 181 printf("%s\n", (err ? "Fail :(" : "OK"));
JimCarver 0:6b753f761943 182 if (err) {
JimCarver 0:6b753f761943 183 error("error: %s (%d)\n", strerror(-err), err);
JimCarver 0:6b753f761943 184 }
JimCarver 0:6b753f761943 185 }
JimCarver 0:6b753f761943 186 // err = fs.unmount();
JimCarver 0:6b753f761943 187 // printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
JimCarver 0:6b753f761943 188 // if (err < 0) {
JimCarver 0:6b753f761943 189 // error("error: %s (%d)\n", strerror(-err), err);
JimCarver 0:6b753f761943 190 // }
JimCarver 1:521604503e81 191
JimCarver 1:521604503e81 192 //
JimCarver 1:521604503e81 193 // Initalize temperature and humidity sensor
JimCarver 1:521604503e81 194 //
JimCarver 0:6b753f761943 195
JimCarver 1:521604503e81 196
JimCarver 0:6b753f761943 197 printf("Connecting to the network using Wifi...\n");
JimCarver 0:6b753f761943 198
JimCarver 0:6b753f761943 199 // Connect to the internet (DHCP is expected to be on)
JimCarver 0:6b753f761943 200 nsapi_error_t status = net.connect(WIFI_SSID, WIFI_PASSWORD, (strlen(WIFI_PASSWORD) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE);
JimCarver 0:6b753f761943 201
JimCarver 0:6b753f761943 202 if (status != 0) {
JimCarver 0:6b753f761943 203 printf("Connecting to the network failed %d!\n", status);
JimCarver 0:6b753f761943 204 return -1;
JimCarver 0:6b753f761943 205 }
JimCarver 0:6b753f761943 206
JimCarver 0:6b753f761943 207 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
JimCarver 0:6b753f761943 208
JimCarver 0:6b753f761943 209 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
JimCarver 0:6b753f761943 210 SimpleMbedCloudClient client(&net, &sd, &fs);
JimCarver 0:6b753f761943 211 int client_status = client.init();
JimCarver 0:6b753f761943 212 if (client_status != 0) {
JimCarver 0:6b753f761943 213 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
JimCarver 0:6b753f761943 214 return -1;
JimCarver 0:6b753f761943 215 }
JimCarver 0:6b753f761943 216
JimCarver 0:6b753f761943 217 // Creating resources, which can be written or read from the cloud
JimCarver 0:6b753f761943 218 button_res = client.create_resource("3200/0/5501", "button_count");
JimCarver 0:6b753f761943 219 button_res->set_value(0);
JimCarver 0:6b753f761943 220 button_res->methods(M2MMethod::GET);
JimCarver 0:6b753f761943 221 button_res->observable(true);
JimCarver 0:6b753f761943 222 button_res->attach_notification_callback(button_callback);
JimCarver 0:6b753f761943 223
JimCarver 0:6b753f761943 224 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
JimCarver 0:6b753f761943 225 pattern_res->set_value("500:500:500:500:500:500:500:500");
JimCarver 0:6b753f761943 226 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
JimCarver 0:6b753f761943 227 pattern_res->attach_put_callback(pattern_updated);
JimCarver 0:6b753f761943 228
JimCarver 0:6b753f761943 229 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
JimCarver 0:6b753f761943 230 blink_res->methods(M2MMethod::POST);
JimCarver 0:6b753f761943 231 blink_res->attach_post_callback(blink_callback);
JimCarver 1:521604503e81 232
JimCarver 1:521604503e81 233 temperature_res = client.create_resource("3303/0/5700", "temperature");
JimCarver 1:521604503e81 234 temperature_res->set_value(0);
JimCarver 1:521604503e81 235 temperature_res->methods(M2MMethod::GET);
JimCarver 1:521604503e81 236 temperature_res->observable(true);
JimCarver 1:521604503e81 237 temperature_res->attach_notification_callback(temperature_callback);
JimCarver 1:521604503e81 238
JimCarver 1:521604503e81 239 humidity_res = client.create_resource("3304/0/5700", "humidity");
JimCarver 1:521604503e81 240 humidity_res->set_value(0);
JimCarver 1:521604503e81 241 humidity_res->methods(M2MMethod::GET);
JimCarver 1:521604503e81 242 humidity_res->observable(true);
JimCarver 1:521604503e81 243 humidity_res->attach_notification_callback(humidity_callback);
JimCarver 1:521604503e81 244
JimCarver 0:6b753f761943 245 printf("Initialized Mbed Cloud Client. Registering...\n");
JimCarver 0:6b753f761943 246
JimCarver 0:6b753f761943 247 // Callback that fires when registering is complete
JimCarver 0:6b753f761943 248 client.on_registered(&registered);
JimCarver 0:6b753f761943 249
JimCarver 0:6b753f761943 250 // Register with Mbed Cloud
JimCarver 0:6b753f761943 251 client.register_and_connect();
JimCarver 0:6b753f761943 252
JimCarver 0:6b753f761943 253 // Placeholder for callback to update local resource when GET comes.
JimCarver 0:6b753f761943 254 // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations
JimCarver 2:d5f5207e8b2a 255
JimCarver 1:521604503e81 256
JimCarver 0:6b753f761943 257 // You can easily run the eventQueue in a separate thread if required
JimCarver 0:6b753f761943 258 eventQueue.dispatch_forever();
JimCarver 0:6b753f761943 259 }