M2X with Seeed Ethernet using Seeed Accelerometer demo

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed

Fork of m2x-seeed_ethernet_demo by Sean Newton

Revision:
3:0fba8849a883
Parent:
1:49d4f5ca7d58
--- a/main.cpp	Tue Sep 23 01:15:19 2014 +0000
+++ b/main.cpp	Tue Sep 30 00:32:20 2014 +0000
@@ -1,21 +1,24 @@
 #include <jsonlite.h>
 #include "M2XStreamClient.h"
-
+#include "ADXL345_I2C.h"
 #include "mbed.h"
 #include "WIZnetInterface.h"
-#include "LM75B.h"  //I2C Temperature Sensor
+
 
 #define ST_NUCLEO
-char feedId[] = "fe08906d21a70b05241234077386e041"; // Feed you want to post to
-char m2xKey[] = "ca9c1e4db697886906de09c701879b19"; // Your M2X access key
-char streamName[] = "temperature"; // Stream you want to post to
+char feedId[] = "02f8358827a2414ab683c4397620ee91"; // Feed you want to post to
+char m2xKey[] = "894ab3426a376a7a18e8f439cad8f8cc"; // Your M2X access key
+char x_streamName[] = "x_accel"; // Stream you want to post to
+char y_streamName[] = "y_accel"; // Stream you want to post to
+char z_streamName[] = "z_accel"; // Stream you want to post to
 
 char name[] = "Dallas"; // Name of current location of datasource
 double latitude = 33.007872;
 double longitude = -96.751614; // You can also read those values from a GPS
 double elevation = 697.00;
 
-AnalogIn temp_sensor(A0);  
+PwmOut mypwm(PWM_OUT);
+ADXL345_I2C accelerometer(D14, D15);
 
 /**
  * Configure the SPI interfac to the ethernet module
@@ -58,18 +61,51 @@
 int main()
 {
     uint8_t mac[6];
-    int adc_scale = 65536; //4096; 
-    int B = 3975;    
-    float resistance; 
-    float temperature;
-    float temperature_f;  
-    char amb_temp[6];    
+    int readings[3] = {0, 0, 0};
+    double xyz[3];
+    int sensorValue;    const double gain = 0.00390625;
+    
+    mypwm.period_ms(500);
+    mypwm.pulsewidth_ms(250);
+    
+    printf("Starting ADXL345 test...\n\r");
+    wait(.001);
+    printf("Device ID is: 0x%02x\n\r", accelerometer.getDeviceID());
+    wait(.001);
+
+    // These are here to test whether any of the initialization fails. It will print the failure
+    if (accelerometer.setPowerControl(0x00)) {
+        printf("didn't intitialize power control\n");
+        return 0;
+    }
+
+    //Full resolution, +/-16g, 4mg/LSB.
+    wait(.001);
+
+    if(accelerometer.setDataFormatControl(0x0B)) {
+        printf("didn't set data format\n");
+        return 0;
+    }
+    wait(.001);
+
+    //3.2kHz data rate.
+    if(accelerometer.setDataRate(ADXL345_3200HZ)) {
+        printf("didn't set data rate\n");
+        return 0;
+    }
+    wait(.001);
+
+    //Measurement mode.
+    if(accelerometer.setPowerControl(MeasurementMode)) {
+        printf("didn't set the power control to measurement\n");
+        return 0;
+    }
 
     /* Have mbed assign the mac address */
-    mbed_mac_address((char *)mac);     
+    mbed_mac_address((char *)mac);
 
     printf("Start...\n");
-    wait_ms(3000);    
+    wait_ms(3000);
 
     /* Initialize ethernet interface */
     int ret = eth.init(mac); //Use DHCP
@@ -82,7 +118,7 @@
         return -1;
     }
 
-
+ 
     /* Get IP address */
     while (1) {
         printf(">>> Get IP address...\n");
@@ -100,47 +136,65 @@
 
     if (!ret) {
         printf("IP: %s, MASK: %s, GW: %s\n",
-                  eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
+               eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
     } else {
         printf("Error eth.connect() - ret = %d\n", ret);
         return -1;
     }
 
-
- 
+    mypwm.pulsewidth_ms(500);
+    
     /* Main loop */
     while (true) {
 
-        /* Read ADC value from analog sensor */
-        uint16_t a = temp_sensor.read_u16();
-               
-        /* Calculate the temperature in Fareheight and Celsius */
-        resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;              
-        temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15;  //convert to temperature via datasheet ;        
-        temperature_f =(1.8 * temperature) +  32.0;
-        sprintf(amb_temp, "%0.2f", temperature_f);  
+        /* Wait 2.5 secs and then loop */
+        delay(2500);
+        mypwm.pulsewidth_ms(0);  
         
-        printf("Temp Sensor Analog Reading is 0x%X = %d  \r\n", a, a);         
-        printf("Current Temperature: %f C  %f F \r\n", temperature, temperature_f); 
+        /* Read accelerometer data */
+        accelerometer.getOutput(readings);
+        for(int i=0; i<3; i++) {
+            xyz[i] = (int16_t) readings[i] * gain;
+        }
+
+        /* x-axis, y-axis and z-axis */
+        printf("%0.3f, %0.3f, %0.3f\n\r", xyz[0], xyz[1], xyz[2]);
 
-        /* Post temperature to M2X site */
-        int response = m2xClient.put(feedId, streamName, amb_temp);
+        delay(2500);
+        mypwm.pulsewidth_ms(500);  
+
+        /* Post acclerometer to M2X site */
+        int response = m2xClient.put(feedId, x_streamName, xyz[0]);
         printf("Post response code: %d\r\n", response);
-        if (response == -1) 
-        {
-            printf("Temperature data transmit post error\n");
+        if (response == -1) {
+            mypwm.pulsewidth_ms(250);   
+            printf("x_accel data transmit post error\n");
         }
         
+        /* Post acclerometer to M2X site */
+        response = m2xClient.put(feedId, y_streamName, xyz[1]);
+        printf("Post response code: %d\r\n", response);
+        if (response == -1) {
+            mypwm.pulsewidth_ms(250);   
+            printf("y_accel data transmit post error\n");
+        }
+        
+        /* Post acclerometer to M2X site */
+        response = m2xClient.put(feedId, z_streamName, xyz[2]);
+        printf("Post response code: %d\r\n", response);
+        if (response == -1) {
+            mypwm.pulsewidth_ms(250);   
+            printf("z_accel data transmit post error\n");
+        }                
+
         /* Update location data */
         response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
         printf("updateLocation response code: %d\r\n", response);
-        if (response == -1) 
-        {
+        if (response == -1) {
+            mypwm.pulsewidth_ms(250);   
             printf("Location data transmit post error\n");
         }
 
         printf("\r\n");
-        /* Wait 5 secs and then loop */
-        delay(5000);
     }
 }