Simulated product dispenser

Dependencies:   HTS221

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

Committer:
JimCarver
Date:
Thu Oct 25 14:00:12 2018 +0000
Revision:
4:e518dde96e59
Parent:
3:0d3492ebb1a5
Simulated dispenser

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 4:e518dde96e59 42 float rtime = 5.0;
JimCarver 4:e518dde96e59 43
JimCarver 0:6b753f761943 44 // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads)
JimCarver 0:6b753f761943 45 // 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 46 EventQueue eventQueue;
JimCarver 0:6b753f761943 47
JimCarver 0:6b753f761943 48 // Declaring net interface as a global variable instead of local to avoid stack overflow
JimCarver 0:6b753f761943 49 ISM43362Interface net;
JimCarver 0:6b753f761943 50
JimCarver 0:6b753f761943 51 // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available)
JimCarver 0:6b753f761943 52 // SDBlockDevice sd(SPI_MOSI, SPI_MISO, SPI_SCK, SPI_CS);
JimCarver 0:6b753f761943 53 QSPIFBlockDevice sd(PE_12, PE_13, PE_14, PE_15,PE_10,PE_11,0,8000000);
JimCarver 0:6b753f761943 54
JimCarver 0:6b753f761943 55 // FATFileSystem fs("sd", &bd);
JimCarver 0:6b753f761943 56
JimCarver 0:6b753f761943 57 LittleFileSystem fs("sd");
JimCarver 0:6b753f761943 58
JimCarver 0:6b753f761943 59 // Declaring pointers for access to Mbed Cloud Client resources outside of main()
JimCarver 0:6b753f761943 60 MbedCloudClientResource *button_res;
JimCarver 0:6b753f761943 61 MbedCloudClientResource *pattern_res;
JimCarver 1:521604503e81 62 MbedCloudClientResource *temperature_res;
JimCarver 1:521604503e81 63 MbedCloudClientResource *humidity_res;
JimCarver 4:e518dde96e59 64
JimCarver 0:6b753f761943 65
JimCarver 4:e518dde96e59 66 int SwipeCount = 0;
JimCarver 4:e518dde96e59 67 InterruptIn PIR(D5);
JimCarver 4:e518dde96e59 68 DigitalOut motorOnLed(LED4);
JimCarver 4:e518dde96e59 69 DigitalOut PIRtrigger(LED2);
JimCarver 4:e518dde96e59 70 DigitalOut connectLED(LED1);
JimCarver 4:e518dde96e59 71
JimCarver 4:e518dde96e59 72 Thread motorThread;
JimCarver 4:e518dde96e59 73 Semaphore motorHold;
JimCarver 1:521604503e81 74
JimCarver 4:e518dde96e59 75 void motorAction(void)
JimCarver 4:e518dde96e59 76 {
JimCarver 4:e518dde96e59 77 while(1) {
JimCarver 4:e518dde96e59 78 motorHold.wait();
JimCarver 4:e518dde96e59 79 SwipeCount++;
JimCarver 4:e518dde96e59 80 motorOnLed = 0;
JimCarver 4:e518dde96e59 81 PIRtrigger = 1;
JimCarver 4:e518dde96e59 82 button_res->set_value(SwipeCount);
JimCarver 4:e518dde96e59 83 wait(rtime);
JimCarver 4:e518dde96e59 84 motorOnLed = 1;
JimCarver 4:e518dde96e59 85 }
JimCarver 4:e518dde96e59 86 }
JimCarver 4:e518dde96e59 87
JimCarver 4:e518dde96e59 88 void PIRhandler(void)
JimCarver 4:e518dde96e59 89 {
JimCarver 4:e518dde96e59 90 motorHold.release();
JimCarver 4:e518dde96e59 91 }
JimCarver 4:e518dde96e59 92
JimCarver 4:e518dde96e59 93 void PIRreset(void)
JimCarver 4:e518dde96e59 94 {
JimCarver 4:e518dde96e59 95 PIRtrigger = 0;
JimCarver 4:e518dde96e59 96 }
JimCarver 1:521604503e81 97
JimCarver 1:521604503e81 98
JimCarver 1:521604503e81 99 // Manage the HST221 in an independent thread
JimCarver 1:521604503e81 100 Thread HTS221Thread;
JimCarver 0:6b753f761943 101
JimCarver 1:521604503e81 102 void HTS221Handler(void)
JimCarver 1:521604503e81 103 {
JimCarver 1:521604503e81 104 uint8_t id;
JimCarver 1:521604503e81 105 float value1, value2;
JimCarver 1:521604503e81 106 static DevI2C devI2c(PB_11,PB_10); // This defines the processor port pins attached to the I2C bus
JimCarver 1:521604503e81 107 static HTS221Sensor hum_temp(&devI2c);
JimCarver 4:e518dde96e59 108
JimCarver 1:521604503e81 109 hum_temp.init(NULL);
JimCarver 1:521604503e81 110 hum_temp.read_id(&id); // Read the device ID
JimCarver 4:e518dde96e59 111 //printf("\r\n\n\nHTS221 humidity & temperature = 0x%X\r\n", id);
JimCarver 1:521604503e81 112 hum_temp.enable();
JimCarver 4:e518dde96e59 113 while(5) {
JimCarver 4:e518dde96e59 114 //printf("PIR=%x\r\n", PIR.read());
JimCarver 1:521604503e81 115 wait(5); // Update every 5 seconds
JimCarver 1:521604503e81 116 // Update temperature and humidity resources
JimCarver 1:521604503e81 117 hum_temp.get_temperature(&value1);
JimCarver 1:521604503e81 118 hum_temp.get_humidity(&value2);
JimCarver 1:521604503e81 119 temperature_res->set_value(value1);
JimCarver 1:521604503e81 120 humidity_res->set_value(value2);
JimCarver 1:521604503e81 121 }
JimCarver 0:6b753f761943 122 }
JimCarver 0:6b753f761943 123
JimCarver 4:e518dde96e59 124
JimCarver 4:e518dde96e59 125 // This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button
JimCarver 4:e518dde96e59 126 void fake_button_press() {
JimCarver 4:e518dde96e59 127 // int v = button_res->get_value_int() + 1;
JimCarver 4:e518dde96e59 128
JimCarver 4:e518dde96e59 129
JimCarver 4:e518dde96e59 130 printf("Dispenser Swiped %d times\n", SwipeCount);
JimCarver 4:e518dde96e59 131 }
JimCarver 4:e518dde96e59 132
JimCarver 0:6b753f761943 133 /**
JimCarver 0:6b753f761943 134 * PUT handler
JimCarver 0:6b753f761943 135 * @param resource The resource that triggered the callback
JimCarver 0:6b753f761943 136 * @param newValue Updated value for the resource
JimCarver 0:6b753f761943 137 */
JimCarver 0:6b753f761943 138 void pattern_updated(MbedCloudClientResource *resource, m2m::String newValue) {
JimCarver 4:e518dde96e59 139 //printf("PUT received, new value: %s\n", newValue.c_str());
JimCarver 4:e518dde96e59 140 rtime = atof(newValue.c_str());
JimCarver 4:e518dde96e59 141 printf("New Rtime= %f\r\n", rtime);
JimCarver 0:6b753f761943 142 }
JimCarver 0:6b753f761943 143
JimCarver 0:6b753f761943 144 /**
JimCarver 0:6b753f761943 145 * POST handler
JimCarver 0:6b753f761943 146 * @param resource The resource that triggered the callback
JimCarver 0:6b753f761943 147 * @param buffer If a body was passed to the POST function, this contains the data.
JimCarver 0:6b753f761943 148 * Note that the buffer is deallocated after leaving this function, so copy it if you need it longer.
JimCarver 0:6b753f761943 149 * @param size Size of the body
JimCarver 0:6b753f761943 150 */
JimCarver 4:e518dde96e59 151 /*void blink_callback(MbedCloudClientResource *resource, const uint8_t *buffer, uint16_t size) {
JimCarver 0:6b753f761943 152 printf("POST received. Going to blink LED pattern: %s\n", pattern_res->get_value().c_str());
JimCarver 0:6b753f761943 153
JimCarver 4:e518dde96e59 154 static DigitalOut augmentedLed(LED2); // LED that is used for blinking the pattern
JimCarver 0:6b753f761943 155
JimCarver 0:6b753f761943 156 // Parse the pattern string, and toggle the LED in that pattern
JimCarver 0:6b753f761943 157 string s = std::string(pattern_res->get_value().c_str());
JimCarver 4:e518dde96e59 158 //printf("Pattern = %s\r\n", s);
JimCarver 0:6b753f761943 159 size_t i = 0;
JimCarver 0:6b753f761943 160 size_t pos = s.find(':');
JimCarver 0:6b753f761943 161 while (pos != string::npos) {
JimCarver 0:6b753f761943 162 wait_ms(atoi(s.substr(i, pos - i).c_str()));
JimCarver 0:6b753f761943 163 augmentedLed = !augmentedLed;
JimCarver 0:6b753f761943 164
JimCarver 0:6b753f761943 165 i = ++pos;
JimCarver 0:6b753f761943 166 pos = s.find(':', pos);
JimCarver 0:6b753f761943 167
JimCarver 0:6b753f761943 168 if (pos == string::npos) {
JimCarver 0:6b753f761943 169 wait_ms(atoi(s.substr(i, s.length()).c_str()));
JimCarver 0:6b753f761943 170 augmentedLed = !augmentedLed;
JimCarver 0:6b753f761943 171 }
JimCarver 0:6b753f761943 172 }
JimCarver 0:6b753f761943 173 }
JimCarver 4:e518dde96e59 174 */
JimCarver 0:6b753f761943 175 /**
JimCarver 0:6b753f761943 176 * Notification callback handler
JimCarver 0:6b753f761943 177 * @param resource The resource that triggered the callback
JimCarver 0:6b753f761943 178 * @param status The delivery status of the notification
JimCarver 0:6b753f761943 179 */
JimCarver 0:6b753f761943 180 void button_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
JimCarver 4:e518dde96e59 181 printf("callback %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
JimCarver 1:521604503e81 182 }
JimCarver 4:e518dde96e59 183 /*
JimCarver 1:521604503e81 184 void temperature_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
JimCarver 1:521604503e81 185 printf("Temperature %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
JimCarver 1:521604503e81 186 }
JimCarver 1:521604503e81 187
JimCarver 1:521604503e81 188 void humidity_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) {
JimCarver 1:521604503e81 189 printf("Humidity %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status);
JimCarver 0:6b753f761943 190 }
JimCarver 4:e518dde96e59 191 */
JimCarver 4:e518dde96e59 192
JimCarver 4:e518dde96e59 193 void startData(void)
JimCarver 4:e518dde96e59 194 {
JimCarver 4:e518dde96e59 195 connectLED = 1;
JimCarver 4:e518dde96e59 196 PIR.rise(PIRhandler);
JimCarver 4:e518dde96e59 197 PIR.fall(PIRreset);
JimCarver 4:e518dde96e59 198 motorThread.start(motorAction);
JimCarver 4:e518dde96e59 199 HTS221Thread.start(HTS221Handler);
JimCarver 4:e518dde96e59 200 }
JimCarver 0:6b753f761943 201
JimCarver 0:6b753f761943 202 /**
JimCarver 0:6b753f761943 203 * Registration callback handler
JimCarver 0:6b753f761943 204 * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal)
JimCarver 0:6b753f761943 205 */
JimCarver 0:6b753f761943 206 void registered(const ConnectorClientEndpointInfo *endpoint) {
JimCarver 0:6b753f761943 207 printf("Connected to Mbed Cloud. Endpoint Name: %s\n", endpoint->internal_endpoint_name.c_str());
JimCarver 4:e518dde96e59 208 startData();
JimCarver 0:6b753f761943 209 }
JimCarver 0:6b753f761943 210
JimCarver 0:6b753f761943 211 int main(void) {
JimCarver 4:e518dde96e59 212 connectLED = 0;
JimCarver 4:e518dde96e59 213 motorOnLed = 1;
JimCarver 0:6b753f761943 214 printf("Starting Simple Mbed Cloud Client example\n");
JimCarver 0:6b753f761943 215
JimCarver 0:6b753f761943 216 printf("Checking SDCard is Formatted\r\n");
JimCarver 0:6b753f761943 217 int err = fs.mount(&sd);
JimCarver 0:6b753f761943 218 printf("%s\n", (err ? "Fail :(" : "OK"));
JimCarver 0:6b753f761943 219 if (err) {
JimCarver 0:6b753f761943 220 // Reformat if we can't mount the filesystem
JimCarver 0:6b753f761943 221 // this should only happen on the first boot
JimCarver 0:6b753f761943 222 printf("No filesystem found, formatting... ");
JimCarver 0:6b753f761943 223 fflush(stdout);
JimCarver 0:6b753f761943 224 err = fs.reformat(&sd);
JimCarver 0:6b753f761943 225 printf("%s\n", (err ? "Fail :(" : "OK"));
JimCarver 0:6b753f761943 226 if (err) {
JimCarver 0:6b753f761943 227 error("error: %s (%d)\n", strerror(-err), err);
JimCarver 0:6b753f761943 228 }
JimCarver 0:6b753f761943 229 }
JimCarver 0:6b753f761943 230 // err = fs.unmount();
JimCarver 0:6b753f761943 231 // printf("%s\n", (err < 0 ? "Fail :(" : "OK"));
JimCarver 0:6b753f761943 232 // if (err < 0) {
JimCarver 0:6b753f761943 233 // error("error: %s (%d)\n", strerror(-err), err);
JimCarver 0:6b753f761943 234 // }
JimCarver 0:6b753f761943 235
JimCarver 0:6b753f761943 236 printf("Connecting to the network using Wifi...\n");
JimCarver 0:6b753f761943 237
JimCarver 0:6b753f761943 238 // Connect to the internet (DHCP is expected to be on)
JimCarver 0:6b753f761943 239 nsapi_error_t status = net.connect(WIFI_SSID, WIFI_PASSWORD, (strlen(WIFI_PASSWORD) > 1) ? NSAPI_SECURITY_WPA_WPA2 : NSAPI_SECURITY_NONE);
JimCarver 0:6b753f761943 240
JimCarver 0:6b753f761943 241 if (status != 0) {
JimCarver 0:6b753f761943 242 printf("Connecting to the network failed %d!\n", status);
JimCarver 0:6b753f761943 243 return -1;
JimCarver 0:6b753f761943 244 }
JimCarver 0:6b753f761943 245
JimCarver 0:6b753f761943 246 printf("Connected to the network successfully. IP address: %s\n", net.get_ip_address());
JimCarver 0:6b753f761943 247
JimCarver 0:6b753f761943 248 // SimpleMbedCloudClient handles registering over LwM2M to Mbed Cloud
JimCarver 0:6b753f761943 249 SimpleMbedCloudClient client(&net, &sd, &fs);
JimCarver 0:6b753f761943 250 int client_status = client.init();
JimCarver 0:6b753f761943 251 if (client_status != 0) {
JimCarver 0:6b753f761943 252 printf("Initializing Mbed Cloud Client failed (%d)\n", client_status);
JimCarver 0:6b753f761943 253 return -1;
JimCarver 0:6b753f761943 254 }
JimCarver 0:6b753f761943 255
JimCarver 0:6b753f761943 256 // Creating resources, which can be written or read from the cloud
JimCarver 0:6b753f761943 257 button_res = client.create_resource("3200/0/5501", "button_count");
JimCarver 0:6b753f761943 258 button_res->set_value(0);
JimCarver 0:6b753f761943 259 button_res->methods(M2MMethod::GET);
JimCarver 0:6b753f761943 260 button_res->observable(true);
JimCarver 0:6b753f761943 261 button_res->attach_notification_callback(button_callback);
JimCarver 0:6b753f761943 262
JimCarver 0:6b753f761943 263 pattern_res = client.create_resource("3201/0/5853", "blink_pattern");
JimCarver 4:e518dde96e59 264 pattern_res->set_value("5.0");
JimCarver 0:6b753f761943 265 pattern_res->methods(M2MMethod::GET | M2MMethod::PUT);
JimCarver 0:6b753f761943 266 pattern_res->attach_put_callback(pattern_updated);
JimCarver 0:6b753f761943 267
JimCarver 4:e518dde96e59 268 /*
JimCarver 0:6b753f761943 269 MbedCloudClientResource *blink_res = client.create_resource("3201/0/5850", "blink_action");
JimCarver 0:6b753f761943 270 blink_res->methods(M2MMethod::POST);
JimCarver 0:6b753f761943 271 blink_res->attach_post_callback(blink_callback);
JimCarver 4:e518dde96e59 272 */
JimCarver 1:521604503e81 273 temperature_res = client.create_resource("3303/0/5700", "temperature");
JimCarver 1:521604503e81 274 temperature_res->set_value(0);
JimCarver 1:521604503e81 275 temperature_res->methods(M2MMethod::GET);
JimCarver 1:521604503e81 276 temperature_res->observable(true);
JimCarver 4:e518dde96e59 277 temperature_res->attach_notification_callback(button_callback);
JimCarver 1:521604503e81 278
JimCarver 1:521604503e81 279 humidity_res = client.create_resource("3304/0/5700", "humidity");
JimCarver 1:521604503e81 280 humidity_res->set_value(0);
JimCarver 1:521604503e81 281 humidity_res->methods(M2MMethod::GET);
JimCarver 1:521604503e81 282 humidity_res->observable(true);
JimCarver 4:e518dde96e59 283 humidity_res->attach_notification_callback(button_callback);
JimCarver 1:521604503e81 284
JimCarver 0:6b753f761943 285 printf("Initialized Mbed Cloud Client. Registering...\n");
JimCarver 0:6b753f761943 286
JimCarver 0:6b753f761943 287 // Callback that fires when registering is complete
JimCarver 0:6b753f761943 288 client.on_registered(&registered);
JimCarver 0:6b753f761943 289
JimCarver 0:6b753f761943 290 // Register with Mbed Cloud
JimCarver 0:6b753f761943 291 client.register_and_connect();
JimCarver 0:6b753f761943 292
JimCarver 0:6b753f761943 293 // You can easily run the eventQueue in a separate thread if required
JimCarver 0:6b753f761943 294 eventQueue.dispatch_forever();
JimCarver 4:e518dde96e59 295 }