M2X with Seeed Ethernet using Seeed Accelerometer demo

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed

Fork of m2x-seeed_ethernet_demo by Sean Newton

Committer:
SeanNewton
Date:
Tue Sep 30 00:32:20 2014 +0000
Revision:
3:0fba8849a883
Parent:
1:49d4f5ca7d58
Initial Release

Who changed what in which revision?

UserRevisionLine numberNew contents of line
SeanNewton 1:49d4f5ca7d58 1 #include <jsonlite.h>
SeanNewton 1:49d4f5ca7d58 2 #include "M2XStreamClient.h"
SeanNewton 3:0fba8849a883 3 #include "ADXL345_I2C.h"
SeanNewton 1:49d4f5ca7d58 4 #include "mbed.h"
SeanNewton 1:49d4f5ca7d58 5 #include "WIZnetInterface.h"
SeanNewton 3:0fba8849a883 6
SeanNewton 1:49d4f5ca7d58 7
SeanNewton 1:49d4f5ca7d58 8 #define ST_NUCLEO
SeanNewton 3:0fba8849a883 9 char feedId[] = "02f8358827a2414ab683c4397620ee91"; // Feed you want to post to
SeanNewton 3:0fba8849a883 10 char m2xKey[] = "894ab3426a376a7a18e8f439cad8f8cc"; // Your M2X access key
SeanNewton 3:0fba8849a883 11 char x_streamName[] = "x_accel"; // Stream you want to post to
SeanNewton 3:0fba8849a883 12 char y_streamName[] = "y_accel"; // Stream you want to post to
SeanNewton 3:0fba8849a883 13 char z_streamName[] = "z_accel"; // Stream you want to post to
SeanNewton 1:49d4f5ca7d58 14
SeanNewton 1:49d4f5ca7d58 15 char name[] = "Dallas"; // Name of current location of datasource
SeanNewton 1:49d4f5ca7d58 16 double latitude = 33.007872;
SeanNewton 1:49d4f5ca7d58 17 double longitude = -96.751614; // You can also read those values from a GPS
SeanNewton 1:49d4f5ca7d58 18 double elevation = 697.00;
SeanNewton 1:49d4f5ca7d58 19
SeanNewton 3:0fba8849a883 20 PwmOut mypwm(PWM_OUT);
SeanNewton 3:0fba8849a883 21 ADXL345_I2C accelerometer(D14, D15);
SeanNewton 1:49d4f5ca7d58 22
SeanNewton 1:49d4f5ca7d58 23 /**
SeanNewton 1:49d4f5ca7d58 24 * Configure the SPI interfac to the ethernet module
SeanNewton 1:49d4f5ca7d58 25 * D11 - MOSI pin
SeanNewton 1:49d4f5ca7d58 26 * D12 - MISO pin
SeanNewton 1:49d4f5ca7d58 27 * D13 - SCK pin
SeanNewton 1:49d4f5ca7d58 28 * D10 - SEL pin
SeanNewton 1:49d4f5ca7d58 29 * NC - Reset pin; use D5 otherwise the shield might get into reset loop
SeanNewton 1:49d4f5ca7d58 30 */
SeanNewton 1:49d4f5ca7d58 31
SeanNewton 1:49d4f5ca7d58 32 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
SeanNewton 1:49d4f5ca7d58 33 WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset
SeanNewton 1:49d4f5ca7d58 34
SeanNewton 1:49d4f5ca7d58 35 /* Instantiate the M2X Stream Client */
SeanNewton 1:49d4f5ca7d58 36 Client client;
SeanNewton 1:49d4f5ca7d58 37 M2XStreamClient m2xClient(&client, m2xKey);
SeanNewton 1:49d4f5ca7d58 38
SeanNewton 1:49d4f5ca7d58 39
SeanNewton 1:49d4f5ca7d58 40 /* Call back function for reading back data point data from M2X */
SeanNewton 1:49d4f5ca7d58 41 void on_data_point_found(const char* at, const char* value, int index, void* context, int type)
SeanNewton 1:49d4f5ca7d58 42 {
SeanNewton 1:49d4f5ca7d58 43 printf("Found a data point, index: %d\r\n", index);
SeanNewton 1:49d4f5ca7d58 44 printf("At: %s Value: %s\r\n", at, value);
SeanNewton 1:49d4f5ca7d58 45 }
SeanNewton 1:49d4f5ca7d58 46
SeanNewton 1:49d4f5ca7d58 47 /* Call back function for reading back location data from M2X */
SeanNewton 1:49d4f5ca7d58 48 void on_location_found(const char* name,
SeanNewton 1:49d4f5ca7d58 49 double latitude,
SeanNewton 1:49d4f5ca7d58 50 double longitude,
SeanNewton 1:49d4f5ca7d58 51 double elevation,
SeanNewton 1:49d4f5ca7d58 52 const char* timestamp,
SeanNewton 1:49d4f5ca7d58 53 int index,
SeanNewton 1:49d4f5ca7d58 54 void* context)
SeanNewton 1:49d4f5ca7d58 55 {
SeanNewton 1:49d4f5ca7d58 56 printf("Found a location, index: %d\r\n", index);
SeanNewton 1:49d4f5ca7d58 57 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
SeanNewton 1:49d4f5ca7d58 58 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
SeanNewton 1:49d4f5ca7d58 59 }
SeanNewton 1:49d4f5ca7d58 60
SeanNewton 1:49d4f5ca7d58 61 int main()
SeanNewton 1:49d4f5ca7d58 62 {
SeanNewton 1:49d4f5ca7d58 63 uint8_t mac[6];
SeanNewton 3:0fba8849a883 64 int readings[3] = {0, 0, 0};
SeanNewton 3:0fba8849a883 65 double xyz[3];
SeanNewton 3:0fba8849a883 66 int sensorValue; const double gain = 0.00390625;
SeanNewton 3:0fba8849a883 67
SeanNewton 3:0fba8849a883 68 mypwm.period_ms(500);
SeanNewton 3:0fba8849a883 69 mypwm.pulsewidth_ms(250);
SeanNewton 3:0fba8849a883 70
SeanNewton 3:0fba8849a883 71 printf("Starting ADXL345 test...\n\r");
SeanNewton 3:0fba8849a883 72 wait(.001);
SeanNewton 3:0fba8849a883 73 printf("Device ID is: 0x%02x\n\r", accelerometer.getDeviceID());
SeanNewton 3:0fba8849a883 74 wait(.001);
SeanNewton 3:0fba8849a883 75
SeanNewton 3:0fba8849a883 76 // These are here to test whether any of the initialization fails. It will print the failure
SeanNewton 3:0fba8849a883 77 if (accelerometer.setPowerControl(0x00)) {
SeanNewton 3:0fba8849a883 78 printf("didn't intitialize power control\n");
SeanNewton 3:0fba8849a883 79 return 0;
SeanNewton 3:0fba8849a883 80 }
SeanNewton 3:0fba8849a883 81
SeanNewton 3:0fba8849a883 82 //Full resolution, +/-16g, 4mg/LSB.
SeanNewton 3:0fba8849a883 83 wait(.001);
SeanNewton 3:0fba8849a883 84
SeanNewton 3:0fba8849a883 85 if(accelerometer.setDataFormatControl(0x0B)) {
SeanNewton 3:0fba8849a883 86 printf("didn't set data format\n");
SeanNewton 3:0fba8849a883 87 return 0;
SeanNewton 3:0fba8849a883 88 }
SeanNewton 3:0fba8849a883 89 wait(.001);
SeanNewton 3:0fba8849a883 90
SeanNewton 3:0fba8849a883 91 //3.2kHz data rate.
SeanNewton 3:0fba8849a883 92 if(accelerometer.setDataRate(ADXL345_3200HZ)) {
SeanNewton 3:0fba8849a883 93 printf("didn't set data rate\n");
SeanNewton 3:0fba8849a883 94 return 0;
SeanNewton 3:0fba8849a883 95 }
SeanNewton 3:0fba8849a883 96 wait(.001);
SeanNewton 3:0fba8849a883 97
SeanNewton 3:0fba8849a883 98 //Measurement mode.
SeanNewton 3:0fba8849a883 99 if(accelerometer.setPowerControl(MeasurementMode)) {
SeanNewton 3:0fba8849a883 100 printf("didn't set the power control to measurement\n");
SeanNewton 3:0fba8849a883 101 return 0;
SeanNewton 3:0fba8849a883 102 }
SeanNewton 1:49d4f5ca7d58 103
SeanNewton 1:49d4f5ca7d58 104 /* Have mbed assign the mac address */
SeanNewton 3:0fba8849a883 105 mbed_mac_address((char *)mac);
SeanNewton 1:49d4f5ca7d58 106
SeanNewton 1:49d4f5ca7d58 107 printf("Start...\n");
SeanNewton 3:0fba8849a883 108 wait_ms(3000);
SeanNewton 1:49d4f5ca7d58 109
SeanNewton 1:49d4f5ca7d58 110 /* Initialize ethernet interface */
SeanNewton 1:49d4f5ca7d58 111 int ret = eth.init(mac); //Use DHCP
SeanNewton 1:49d4f5ca7d58 112
SeanNewton 1:49d4f5ca7d58 113
SeanNewton 1:49d4f5ca7d58 114 if (!ret) {
SeanNewton 1:49d4f5ca7d58 115 printf("Initialized, MAC: %s\n", eth.getMACAddress());
SeanNewton 1:49d4f5ca7d58 116 } else {
SeanNewton 1:49d4f5ca7d58 117 printf("Error eth.init() - ret = %d\n", ret);
SeanNewton 1:49d4f5ca7d58 118 return -1;
SeanNewton 1:49d4f5ca7d58 119 }
SeanNewton 1:49d4f5ca7d58 120
SeanNewton 3:0fba8849a883 121
SeanNewton 1:49d4f5ca7d58 122 /* Get IP address */
SeanNewton 1:49d4f5ca7d58 123 while (1) {
SeanNewton 1:49d4f5ca7d58 124 printf(">>> Get IP address...\n");
SeanNewton 1:49d4f5ca7d58 125 ret = eth.connect(); // Connect to network
SeanNewton 1:49d4f5ca7d58 126
SeanNewton 1:49d4f5ca7d58 127 printf("ret: %d\n",ret);
SeanNewton 1:49d4f5ca7d58 128 if (ret < 0) {
SeanNewton 1:49d4f5ca7d58 129 printf(">>> Could not connect to network! Retrying ...\n");
SeanNewton 1:49d4f5ca7d58 130 wait_ms(3000);
SeanNewton 1:49d4f5ca7d58 131 printf("past this point...\n");
SeanNewton 1:49d4f5ca7d58 132 } else {
SeanNewton 1:49d4f5ca7d58 133 break;
SeanNewton 1:49d4f5ca7d58 134 }
SeanNewton 1:49d4f5ca7d58 135 }
SeanNewton 1:49d4f5ca7d58 136
SeanNewton 1:49d4f5ca7d58 137 if (!ret) {
SeanNewton 1:49d4f5ca7d58 138 printf("IP: %s, MASK: %s, GW: %s\n",
SeanNewton 3:0fba8849a883 139 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
SeanNewton 1:49d4f5ca7d58 140 } else {
SeanNewton 1:49d4f5ca7d58 141 printf("Error eth.connect() - ret = %d\n", ret);
SeanNewton 1:49d4f5ca7d58 142 return -1;
SeanNewton 1:49d4f5ca7d58 143 }
SeanNewton 1:49d4f5ca7d58 144
SeanNewton 3:0fba8849a883 145 mypwm.pulsewidth_ms(500);
SeanNewton 3:0fba8849a883 146
SeanNewton 1:49d4f5ca7d58 147 /* Main loop */
SeanNewton 1:49d4f5ca7d58 148 while (true) {
SeanNewton 1:49d4f5ca7d58 149
SeanNewton 3:0fba8849a883 150 /* Wait 2.5 secs and then loop */
SeanNewton 3:0fba8849a883 151 delay(2500);
SeanNewton 3:0fba8849a883 152 mypwm.pulsewidth_ms(0);
SeanNewton 1:49d4f5ca7d58 153
SeanNewton 3:0fba8849a883 154 /* Read accelerometer data */
SeanNewton 3:0fba8849a883 155 accelerometer.getOutput(readings);
SeanNewton 3:0fba8849a883 156 for(int i=0; i<3; i++) {
SeanNewton 3:0fba8849a883 157 xyz[i] = (int16_t) readings[i] * gain;
SeanNewton 3:0fba8849a883 158 }
SeanNewton 3:0fba8849a883 159
SeanNewton 3:0fba8849a883 160 /* x-axis, y-axis and z-axis */
SeanNewton 3:0fba8849a883 161 printf("%0.3f, %0.3f, %0.3f\n\r", xyz[0], xyz[1], xyz[2]);
SeanNewton 1:49d4f5ca7d58 162
SeanNewton 3:0fba8849a883 163 delay(2500);
SeanNewton 3:0fba8849a883 164 mypwm.pulsewidth_ms(500);
SeanNewton 3:0fba8849a883 165
SeanNewton 3:0fba8849a883 166 /* Post acclerometer to M2X site */
SeanNewton 3:0fba8849a883 167 int response = m2xClient.put(feedId, x_streamName, xyz[0]);
SeanNewton 1:49d4f5ca7d58 168 printf("Post response code: %d\r\n", response);
SeanNewton 3:0fba8849a883 169 if (response == -1) {
SeanNewton 3:0fba8849a883 170 mypwm.pulsewidth_ms(250);
SeanNewton 3:0fba8849a883 171 printf("x_accel data transmit post error\n");
SeanNewton 1:49d4f5ca7d58 172 }
SeanNewton 1:49d4f5ca7d58 173
SeanNewton 3:0fba8849a883 174 /* Post acclerometer to M2X site */
SeanNewton 3:0fba8849a883 175 response = m2xClient.put(feedId, y_streamName, xyz[1]);
SeanNewton 3:0fba8849a883 176 printf("Post response code: %d\r\n", response);
SeanNewton 3:0fba8849a883 177 if (response == -1) {
SeanNewton 3:0fba8849a883 178 mypwm.pulsewidth_ms(250);
SeanNewton 3:0fba8849a883 179 printf("y_accel data transmit post error\n");
SeanNewton 3:0fba8849a883 180 }
SeanNewton 3:0fba8849a883 181
SeanNewton 3:0fba8849a883 182 /* Post acclerometer to M2X site */
SeanNewton 3:0fba8849a883 183 response = m2xClient.put(feedId, z_streamName, xyz[2]);
SeanNewton 3:0fba8849a883 184 printf("Post response code: %d\r\n", response);
SeanNewton 3:0fba8849a883 185 if (response == -1) {
SeanNewton 3:0fba8849a883 186 mypwm.pulsewidth_ms(250);
SeanNewton 3:0fba8849a883 187 printf("z_accel data transmit post error\n");
SeanNewton 3:0fba8849a883 188 }
SeanNewton 3:0fba8849a883 189
SeanNewton 1:49d4f5ca7d58 190 /* Update location data */
SeanNewton 1:49d4f5ca7d58 191 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
SeanNewton 1:49d4f5ca7d58 192 printf("updateLocation response code: %d\r\n", response);
SeanNewton 3:0fba8849a883 193 if (response == -1) {
SeanNewton 3:0fba8849a883 194 mypwm.pulsewidth_ms(250);
SeanNewton 1:49d4f5ca7d58 195 printf("Location data transmit post error\n");
SeanNewton 1:49d4f5ca7d58 196 }
SeanNewton 1:49d4f5ca7d58 197
SeanNewton 1:49d4f5ca7d58 198 printf("\r\n");
SeanNewton 1:49d4f5ca7d58 199 }
SeanNewton 1:49d4f5ca7d58 200 }