Toyomasa Watarai / Mbed OS Cloud-connect-example-wifi-app-shield

Dependencies:   simple-mbed-cloud-client LM75B MMA7660

Fork of mbed-cloud-connect-example-wifi by Toyomasa Watarai

Revision:
13:428ae6d8349a
Parent:
11:4cd5fd7c7821
Child:
14:30fde4120a4d
--- a/main.cpp	Sat Jun 30 05:31:19 2018 +0000
+++ b/main.cpp	Sat Jun 30 05:42:47 2018 +0000
@@ -25,6 +25,8 @@
 #include "SDBlockDevice.h"
 #include "FATFileSystem.h"
 #include "ESP8266Interface.h"
+#include "MMA7660.h"
+#include "LM75B.h"
 
 #define WIFI_SSID "SSID"
 #define WIFI_PSWD "PASSWORD"
@@ -101,6 +103,18 @@
     // Placeholder for GET
 }
 
+void accel_callback(const M2MBase& object, const NoticationDeliveryStatus status)
+{
+    // Do nothing.
+}
+
+void temp_callback(const M2MBase& object, const NoticationDeliveryStatus status)
+{
+    // Do nothing.
+}
+
+
+
 
 int main(void)
 {
@@ -175,19 +189,55 @@
     blink->methods(M2MMethod::POST);
     blink->attach_post_callback(blink_callback);
 
+    /* Accelerometer */
+    const int NUM_AXIS = 3;
+    MbedCloudClientResource* accel[NUM_AXIS];
+    accel[0] = mbedClient.create_resource("3313/0/5702", "accel_x");
+    accel[1] = mbedClient.create_resource("3313/0/5703", "accel_y");
+    accel[2] = mbedClient.create_resource("3313/0/5704", "accel_z");
+    for (int i=0; i < NUM_AXIS; i++) {
+        accel[i]->set_value(0);
+        accel[i]->methods(M2MMethod::GET);
+        accel[i]->attach_notification_callback(accel_callback);
+        accel[i]->observable(true);
+    }
+    MbedCloudClientResource* acc_unit = mbedClient.create_resource("3313/0/5701", "unit");
+    acc_unit->set_value("G");
+
+    /* Temperature */
+    MbedCloudClientResource *temp = mbedClient.create_resource("3303/0/5700", "temperature");
+    temp->set_value("0");
+    temp->methods(M2MMethod::GET);
+    temp->attach_notification_callback(temp_callback);
+    temp->observable(true);
+    MbedCloudClientResource *temp_unit = mbedClient.create_resource("3303/0/5701", "unit");
+    temp_unit->set_value("Cel");
+
     mbedClient.register_and_connect();
 
+    printf("Waiting for register and connect ");
     // Wait for client to finish registering
     while (!mbedClient.is_client_registered()) {
-        wait_ms(100);
+        printf(".");
+        wait_ms(500);
     }
+    printf("\n\n");
 
     // Placeholder for callback to update local resource when GET comes.
     //timer.attach(&button_press, 5.0);
       sw2.mode(PullUp);
       sw2.fall(button_press);
       button_count = 0;
-      
+
+    // For sensors
+    LM75B lm75b(I2C_SDA, I2C_SCL);     // temperature
+    MMA7660 mma7660(I2C_SDA, I2C_SCL); // accel
+
+    const long measurementPeriod = 5;  // sec
+    Timer timer;
+    timer.start();
+
+
     // Check if client is registering or registered, if true sleep and repeat.
     while (mbedClient.is_register_called()) {
         //static int button_count = 0;
@@ -200,9 +250,27 @@
             //button->set_value(button_count);
             printf("button clicked %d times\r\n", button_count);            
         }
-        
+        if( timer.read() > measurementPeriod) {
+            timer.reset();
+            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[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->set_value(buf);
+            printf("temp: %s\n", buf);
+        }
+
     }
-
     // Client unregistered, exit program.
     return 0;
 }