M2X with Seeed Ethernet using Seeed Accelerometer demo
Dependencies: LM75B M2XStreamClient jsonlite mbed-rtos mbed
Fork of m2x-seeed_ethernet_demo by
main.cpp
- Committer:
- SeanNewton
- Date:
- 2014-09-30
- Revision:
- 3:0fba8849a883
- Parent:
- 1:49d4f5ca7d58
File content as of revision 3:0fba8849a883:
#include <jsonlite.h> #include "M2XStreamClient.h" #include "ADXL345_I2C.h" #include "mbed.h" #include "WIZnetInterface.h" #define ST_NUCLEO 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; PwmOut mypwm(PWM_OUT); ADXL345_I2C accelerometer(D14, D15); /** * Configure the SPI interfac to the ethernet module * D11 - MOSI pin * D12 - MISO pin * D13 - SCK pin * D10 - SEL pin * NC - Reset pin; use D5 otherwise the shield might get into reset loop */ SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset /* Instantiate the M2X Stream Client */ Client client; M2XStreamClient m2xClient(&client, m2xKey); /* Call back function for reading back data point data from M2X */ void on_data_point_found(const char* at, const char* value, int index, void* context, int type) { printf("Found a data point, index: %d\r\n", index); printf("At: %s Value: %s\r\n", at, value); } /* Call back function for reading back location data from M2X */ void on_location_found(const char* name, double latitude, double longitude, double elevation, const char* timestamp, int index, void* context) { printf("Found a location, index: %d\r\n", index); printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); } int main() { uint8_t mac[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); printf("Start...\n"); wait_ms(3000); /* Initialize ethernet interface */ int ret = eth.init(mac); //Use DHCP if (!ret) { printf("Initialized, MAC: %s\n", eth.getMACAddress()); } else { printf("Error eth.init() - ret = %d\n", ret); return -1; } /* Get IP address */ while (1) { printf(">>> Get IP address...\n"); ret = eth.connect(); // Connect to network printf("ret: %d\n",ret); if (ret < 0) { printf(">>> Could not connect to network! Retrying ...\n"); wait_ms(3000); printf("past this point...\n"); } else { break; } } if (!ret) { printf("IP: %s, MASK: %s, GW: %s\n", 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) { /* Wait 2.5 secs and then loop */ delay(2500); mypwm.pulsewidth_ms(0); /* 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]); 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) { 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) { mypwm.pulsewidth_ms(250); printf("Location data transmit post error\n"); } printf("\r\n"); } }