Toyomasa Watarai
/
mbed-cloud-example-ESP8266
Simple Mbed Cloud client application using features of K64F including Wi-Fi and SD Card
Revision 11:354983151a8e, committed 2018-07-04
- Comitter:
- MACRUM
- Date:
- Wed Jul 04 06:04:43 2018 +0000
- Parent:
- 10:d88cd97a42fa
- Commit message:
- Initial commit
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/LM75B.lib Wed Jul 04 06:04:43 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/chris/code/LM75B/#6a70c9303bbe
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/MMA7660.lib Wed Jul 04 06:04:43 2018 +0000 @@ -0,0 +1,1 @@ +https://os.mbed.com/users/Sissors/code/MMA7660/#36a163511e34
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/esp8266-driver.lib Wed Jul 04 06:04:43 2018 +0000 @@ -0,0 +1,1 @@ +https://github.com/ARMmbed/esp8266-driver/#5a88ff9c75e34d65e359d7f239bacc456824ed0e
--- a/main.cpp Fri Jun 15 18:57:20 2018 +0100 +++ b/main.cpp Wed Jul 04 06:04:43 2018 +0000 @@ -20,27 +20,46 @@ #include "simple-mbed-cloud-client.h" #include "SDBlockDevice.h" #include "FATFileSystem.h" -#include "EthernetInterface.h" +#include "ESP8266Interface.h" +#include "MMA7660.h" +#include "LM75B.h" + +#define WIFI_SSID "SSID" +#define WIFI_PASSWORD "PASSWORD" // An event queue is a very useful structure to debounce information between contexts (e.g. ISR and normal threads) // 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 EventQueue eventQueue; +Thread thread1; + +InterruptIn sw2(SW2); +DigitalOut led2(LED2); // Storage implementation definition, currently using SDBlockDevice (SPI flash, DataFlash, and internal flash are also available) SDBlockDevice sd(PTE3, PTE1, PTE2, PTE4); FATFileSystem fs("sd", &sd); +ESP8266Interface net(D1, D0); +LM75B lm75b(I2C_SDA, I2C_SCL); // temperature +MMA7660 mma7660(I2C_SDA, I2C_SCL); // accel + +const int NUM_AXIS = 3; // Declaring pointers for access to Mbed Cloud Client resources outside of main() MbedCloudClientResource *button_res; MbedCloudClientResource *pattern_res; +MbedCloudClientResource *temp_res; +MbedCloudClientResource *temp_unit_res; +MbedCloudClientResource *accel_res[NUM_AXIS]; +MbedCloudClientResource *acc_unit_res; -// This function gets triggered by the timer. It's easy to replace it by an InterruptIn and fall() mode on a real button -void fake_button_press() { - int v = button_res->get_value_int() + 1; +static bool button_pressed = false; +static int button_count = 0; - button_res->set_value(v); - - printf("Simulated button clicked %d times\n", v); +void button_press() +{ + button_pressed = true; + ++button_count; + button_res->set_value(button_count); } /** @@ -91,6 +110,35 @@ printf("Button notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); } +void temp_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) +{ + printf("Temperature notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); +} + +void accel_callback(MbedCloudClientResource *resource, const NoticationDeliveryStatus status) +{ + printf("Accelerometer notification, status %s (%d)\n", MbedCloudClientResource::delivery_status_to_string(status), status); +} + +void measure_sensors() +{ + float temperature, acc[3]; + const unsigned int buf_size = 20; + char buf[buf_size]; + + mma7660.readData(acc); + for(int i=0; i < NUM_AXIS; i++) { + snprintf(buf, buf_size, "%f", acc[i]); + accel_res[i]->set_value(buf); + } + printf("acc: %f,%f,%f\n", acc[0], acc[1], acc[2]); + + temperature = lm75b.read(); + snprintf(buf, buf_size, "%f", temperature); + temp_res->set_value(buf); + printf("temp: %s\n", buf); +} + /** * Registration callback handler * @param endpoint Information about the registered endpoint such as the name (so you can find it back in portal) @@ -101,11 +149,10 @@ int main(void) { printf("Starting Simple Mbed Cloud Client example\n"); - printf("Connecting to the network using Ethernet...\n"); + printf("Connecting to the network using Wi-Fi...\n"); // Connect to the internet (DHCP is expected to be on) - EthernetInterface net; - nsapi_error_t status = net.connect(); + nsapi_error_t status = net.connect(WIFI_SSID, WIFI_PASSWORD, NSAPI_SECURITY_WPA2); if (status != 0) { printf("Connecting to the network failed %d!\n", status); @@ -138,6 +185,29 @@ blink_res->methods(M2MMethod::POST); blink_res->attach_post_callback(blink_callback); + temp_res = client.create_resource("3303/0/5700", "temperature"); + temp_res->set_value("0"); + temp_res->methods(M2MMethod::GET); + temp_res->attach_notification_callback(temp_callback); + temp_res->observable(true); + + temp_unit_res = client.create_resource("3303/0/5701", "unit"); + temp_unit_res->set_value("Cel"); + + accel_res[0] = client.create_resource("3313/0/5702", "accel_x"); + accel_res[1] = client.create_resource("3313/0/5703", "accel_y"); + accel_res[2] = client.create_resource("3313/0/5704", "accel_z"); + + for (int i=0; i < NUM_AXIS; i++) { + accel_res[i]->set_value(0); + accel_res[i]->methods(M2MMethod::GET); + accel_res[i]->attach_notification_callback(accel_callback); + accel_res[i]->observable(true); + } + + acc_unit_res = client.create_resource("3313/0/5701", "unit"); + acc_unit_res->set_value("G"); + printf("Initialized Mbed Cloud Client. Registering...\n"); // Callback that fires when registering is complete @@ -146,11 +216,30 @@ // Register with Mbed Cloud client.register_and_connect(); + // Setup the button + sw2.mode(PullUp); + + // The button fall handler is placed in the event queue so it will run in + // thread context instead of ISR context, which allows safely updating the cloud resource + sw2.fall(eventQueue.event(&button_press)); + button_count = 0; + // Placeholder for callback to update local resource when GET comes. // The timer fires on an interrupt context, but debounces it to the eventqueue, so it's safe to do network operations Ticker timer; - timer.attach(eventQueue.event(&fake_button_press), 5.0); + timer.attach(eventQueue.event(&measure_sensors), 5.0); + + // Start the event queue in a separate thread so the main thread continues + thread1.start(callback(&eventQueue, &EventQueue::dispatch_forever)); + + while(1) { + wait_ms(100); - // You can easily run the eventQueue in a separate thread if required - eventQueue.dispatch_forever(); + if (button_pressed) { + button_pressed = false; + printf("button clicked %d times\r\n", button_count); + } + + } + }
--- a/mbed_app.json Fri Jun 15 18:57:20 2018 +0100 +++ b/mbed_app.json Wed Jul 04 06:04:43 2018 +0000 @@ -5,7 +5,7 @@ "MBED_CLIENT_USER_CONFIG_FILE=\"mbed_cloud_client_user_config.h\"", "MBED_CLOUD_CLIENT_USER_CONFIG_FILE=\"mbed_cloud_client_user_config.h\"", "PAL_DTLS_PEER_MIN_TIMEOUT=5000", - "MBED_CONF_APP_MAIN_STACK_SIZE=4608", + "MBED_CONF_APP_MAIN_STACK_SIZE=8000", "ARM_UC_USE_PAL_BLOCKDEVICE=1", "MBED_CLOUD_CLIENT_UPDATE_STORAGE=ARM_UCP_FLASHIAP_BLOCKDEVICE" ], @@ -24,15 +24,7 @@ "sotp-section-2-address" : "0xFF000", "sotp-section-2-size" : "0x1000", "sotp-num-sections" : 2 - }, - "K66F": { - "sotp-section-1-address" : "0x1FE000", - "sotp-section-1-size" : "0x1000", - "sotp-section-2-address" : "0x1FF000", - "sotp-section-2-size" : "0x1000", - "sotp-num-sections" : 2 } - }, "config": { "format-storage-layer-on-error": {
--- a/mbed_cloud_dev_credentials.c Fri Jun 15 18:57:20 2018 +0100 +++ b/mbed_cloud_dev_credentials.c Wed Jul 04 06:04:43 2018 +0000 @@ -15,39 +15,39 @@ */ #ifndef __MBED_CLOUD_DEV_CREDENTIALS_H__ #define __MBED_CLOUD_DEV_CREDENTIALS_H__ - + #if MBED_CONF_APP_DEVELOPER_MODE == 1 #error "Replace mbed_cloud_dev_credentials.c with your own developer cert." #endif - + #include <inttypes.h> - + const char MBED_CLOUD_DEV_BOOTSTRAP_ENDPOINT_NAME[] = ""; const char MBED_CLOUD_DEV_ACCOUNT_ID[] = ""; const char MBED_CLOUD_DEV_BOOTSTRAP_SERVER_URI[] = ""; - + const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE[] = { 0x0 }; - + const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE[] = { 0x0 }; - + const uint8_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY[] = { 0x0 }; - + const char MBED_CLOUD_DEV_MANUFACTURER[] = "dev_manufacturer"; - + const char MBED_CLOUD_DEV_MODEL_NUMBER[] = "dev_model_num"; - + const char MBED_CLOUD_DEV_SERIAL_NUMBER[] = "0"; - + const char MBED_CLOUD_DEV_DEVICE_TYPE[] = "dev_device_type"; - + const char MBED_CLOUD_DEV_HARDWARE_VERSION[] = "dev_hardware_version"; - + const uint32_t MBED_CLOUD_DEV_MEMORY_TOTAL_KB = 0; const uint32_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE_SIZE = sizeof(MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_CERTIFICATE); const uint32_t MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE_SIZE = sizeof(MBED_CLOUD_DEV_BOOTSTRAP_SERVER_ROOT_CA_CERTIFICATE); const uint32_t MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY_SIZE = sizeof(MBED_CLOUD_DEV_BOOTSTRAP_DEVICE_PRIVATE_KEY); - + #endif //__MBED_CLOUD_DEV_CREDENTIALS_H__