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:
Sat Sep 20 01:43:27 2014 +0000
Revision:
1:49d4f5ca7d58
Parent:
0:38a7a8cae773
Child:
3:0fba8849a883
Child:
5:35a77b9b509c
First release with STM32F411 Nucleo board

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 1:49d4f5ca7d58 3
SeanNewton 1:49d4f5ca7d58 4 #include "mbed.h"
SeanNewton 1:49d4f5ca7d58 5 #include "WIZnetInterface.h"
SeanNewton 1:49d4f5ca7d58 6 #include "LM75B.h" //I2C Temperature Sensor
SeanNewton 1:49d4f5ca7d58 7
SeanNewton 1:49d4f5ca7d58 8 #define ST_NUCLEO
SeanNewton 1:49d4f5ca7d58 9 char feedId[] = "fe08906d21a70b05241234077386e041"; // Feed you want to post to
SeanNewton 1:49d4f5ca7d58 10 char m2xKey[] = "ca9c1e4db697886906de09c701879b19"; // Your M2X access key
SeanNewton 1:49d4f5ca7d58 11 char streamName[] = "temperature"; // Stream you want to post to
SeanNewton 1:49d4f5ca7d58 12
SeanNewton 1:49d4f5ca7d58 13 char name[] = "Dallas"; // Name of current location of datasource
SeanNewton 1:49d4f5ca7d58 14 double latitude = 33.007872;
SeanNewton 1:49d4f5ca7d58 15 double longitude = -96.751614; // You can also read those values from a GPS
SeanNewton 1:49d4f5ca7d58 16 double elevation = 697.00;
SeanNewton 1:49d4f5ca7d58 17
SeanNewton 1:49d4f5ca7d58 18 AnalogIn temp_sensor(A0);
SeanNewton 1:49d4f5ca7d58 19
SeanNewton 1:49d4f5ca7d58 20 /**
SeanNewton 1:49d4f5ca7d58 21 * Configure the SPI interfac to the ethernet module
SeanNewton 1:49d4f5ca7d58 22 * D11 - MOSI pin
SeanNewton 1:49d4f5ca7d58 23 * D12 - MISO pin
SeanNewton 1:49d4f5ca7d58 24 * D13 - SCK pin
SeanNewton 1:49d4f5ca7d58 25 * D10 - SEL pin
SeanNewton 1:49d4f5ca7d58 26 * NC - Reset pin; use D5 otherwise the shield might get into reset loop
SeanNewton 1:49d4f5ca7d58 27 */
SeanNewton 1:49d4f5ca7d58 28
SeanNewton 1:49d4f5ca7d58 29 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
SeanNewton 1:49d4f5ca7d58 30 WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset
SeanNewton 1:49d4f5ca7d58 31
SeanNewton 1:49d4f5ca7d58 32 /* Instantiate the M2X Stream Client */
SeanNewton 1:49d4f5ca7d58 33 Client client;
SeanNewton 1:49d4f5ca7d58 34 M2XStreamClient m2xClient(&client, m2xKey);
SeanNewton 1:49d4f5ca7d58 35
SeanNewton 1:49d4f5ca7d58 36
SeanNewton 1:49d4f5ca7d58 37 /* Call back function for reading back data point data from M2X */
SeanNewton 1:49d4f5ca7d58 38 void on_data_point_found(const char* at, const char* value, int index, void* context, int type)
SeanNewton 1:49d4f5ca7d58 39 {
SeanNewton 1:49d4f5ca7d58 40 printf("Found a data point, index: %d\r\n", index);
SeanNewton 1:49d4f5ca7d58 41 printf("At: %s Value: %s\r\n", at, value);
SeanNewton 1:49d4f5ca7d58 42 }
SeanNewton 1:49d4f5ca7d58 43
SeanNewton 1:49d4f5ca7d58 44 /* Call back function for reading back location data from M2X */
SeanNewton 1:49d4f5ca7d58 45 void on_location_found(const char* name,
SeanNewton 1:49d4f5ca7d58 46 double latitude,
SeanNewton 1:49d4f5ca7d58 47 double longitude,
SeanNewton 1:49d4f5ca7d58 48 double elevation,
SeanNewton 1:49d4f5ca7d58 49 const char* timestamp,
SeanNewton 1:49d4f5ca7d58 50 int index,
SeanNewton 1:49d4f5ca7d58 51 void* context)
SeanNewton 1:49d4f5ca7d58 52 {
SeanNewton 1:49d4f5ca7d58 53 printf("Found a location, index: %d\r\n", index);
SeanNewton 1:49d4f5ca7d58 54 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
SeanNewton 1:49d4f5ca7d58 55 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
SeanNewton 1:49d4f5ca7d58 56 }
SeanNewton 1:49d4f5ca7d58 57
SeanNewton 1:49d4f5ca7d58 58 int main()
SeanNewton 1:49d4f5ca7d58 59 {
SeanNewton 1:49d4f5ca7d58 60 uint8_t mac[6];
SeanNewton 1:49d4f5ca7d58 61 int adc_scale = 65536; //4096;
SeanNewton 1:49d4f5ca7d58 62 int B = 3975;
SeanNewton 1:49d4f5ca7d58 63 float resistance;
SeanNewton 1:49d4f5ca7d58 64 float temperature;
SeanNewton 1:49d4f5ca7d58 65 float temperature_f;
SeanNewton 1:49d4f5ca7d58 66 char amb_temp[6];
SeanNewton 1:49d4f5ca7d58 67
SeanNewton 1:49d4f5ca7d58 68 /* Have mbed assign the mac address */
SeanNewton 1:49d4f5ca7d58 69 mbed_mac_address((char *)mac);
SeanNewton 1:49d4f5ca7d58 70
SeanNewton 1:49d4f5ca7d58 71 printf("Start...\n");
SeanNewton 1:49d4f5ca7d58 72 wait_ms(3000);
SeanNewton 1:49d4f5ca7d58 73
SeanNewton 1:49d4f5ca7d58 74 /* Initialize ethernet interface */
SeanNewton 1:49d4f5ca7d58 75 int ret = eth.init(mac); //Use DHCP
SeanNewton 1:49d4f5ca7d58 76
SeanNewton 1:49d4f5ca7d58 77
SeanNewton 1:49d4f5ca7d58 78 if (!ret) {
SeanNewton 1:49d4f5ca7d58 79 printf("Initialized, MAC: %s\n", eth.getMACAddress());
SeanNewton 1:49d4f5ca7d58 80 } else {
SeanNewton 1:49d4f5ca7d58 81 printf("Error eth.init() - ret = %d\n", ret);
SeanNewton 1:49d4f5ca7d58 82 return -1;
SeanNewton 1:49d4f5ca7d58 83 }
SeanNewton 1:49d4f5ca7d58 84
SeanNewton 1:49d4f5ca7d58 85
SeanNewton 1:49d4f5ca7d58 86 /* Get IP address */
SeanNewton 1:49d4f5ca7d58 87 while (1) {
SeanNewton 1:49d4f5ca7d58 88 printf(">>> Get IP address...\n");
SeanNewton 1:49d4f5ca7d58 89 ret = eth.connect(); // Connect to network
SeanNewton 1:49d4f5ca7d58 90
SeanNewton 1:49d4f5ca7d58 91 printf("ret: %d\n",ret);
SeanNewton 1:49d4f5ca7d58 92 if (ret < 0) {
SeanNewton 1:49d4f5ca7d58 93 printf(">>> Could not connect to network! Retrying ...\n");
SeanNewton 1:49d4f5ca7d58 94 wait_ms(3000);
SeanNewton 1:49d4f5ca7d58 95 printf("past this point...\n");
SeanNewton 1:49d4f5ca7d58 96 } else {
SeanNewton 1:49d4f5ca7d58 97 break;
SeanNewton 1:49d4f5ca7d58 98 }
SeanNewton 1:49d4f5ca7d58 99 }
SeanNewton 1:49d4f5ca7d58 100
SeanNewton 1:49d4f5ca7d58 101 if (!ret) {
SeanNewton 1:49d4f5ca7d58 102 printf("IP: %s, MASK: %s, GW: %s\n",
SeanNewton 1:49d4f5ca7d58 103 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
SeanNewton 1:49d4f5ca7d58 104 } else {
SeanNewton 1:49d4f5ca7d58 105 printf("Error eth.connect() - ret = %d\n", ret);
SeanNewton 1:49d4f5ca7d58 106 return -1;
SeanNewton 1:49d4f5ca7d58 107 }
SeanNewton 1:49d4f5ca7d58 108
SeanNewton 1:49d4f5ca7d58 109
SeanNewton 1:49d4f5ca7d58 110
SeanNewton 1:49d4f5ca7d58 111 /* Main loop */
SeanNewton 1:49d4f5ca7d58 112 while (true) {
SeanNewton 1:49d4f5ca7d58 113
SeanNewton 1:49d4f5ca7d58 114 /* Read ADC value from analog sensor */
SeanNewton 1:49d4f5ca7d58 115 uint16_t a = temp_sensor.read_u16();
SeanNewton 1:49d4f5ca7d58 116
SeanNewton 1:49d4f5ca7d58 117 /* Calculate the temperature in Fareheight and Celsius */
SeanNewton 1:49d4f5ca7d58 118 resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor;
SeanNewton 1:49d4f5ca7d58 119 temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet ;
SeanNewton 1:49d4f5ca7d58 120 temperature_f =(1.8 * temperature) + 32.0;
SeanNewton 1:49d4f5ca7d58 121 sprintf(amb_temp, "%0.2f", temperature_f);
SeanNewton 1:49d4f5ca7d58 122
SeanNewton 1:49d4f5ca7d58 123 printf("Temp Sensor Analog Reading is 0x%X = %d \r\n", a, a);
SeanNewton 1:49d4f5ca7d58 124 printf("Current Temperature: %f C %f F \r\n", temperature, temperature_f);
SeanNewton 1:49d4f5ca7d58 125
SeanNewton 1:49d4f5ca7d58 126 /* Post temperature to M2X site */
SeanNewton 1:49d4f5ca7d58 127 int response = m2xClient.put(feedId, streamName, amb_temp);
SeanNewton 1:49d4f5ca7d58 128 printf("Post response code: %d\r\n", response);
SeanNewton 1:49d4f5ca7d58 129 if (response == -1)
SeanNewton 1:49d4f5ca7d58 130 {
SeanNewton 1:49d4f5ca7d58 131 printf("Temperature data transmit post error\n");
SeanNewton 1:49d4f5ca7d58 132 }
SeanNewton 1:49d4f5ca7d58 133
SeanNewton 1:49d4f5ca7d58 134 /* Update location data */
SeanNewton 1:49d4f5ca7d58 135 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
SeanNewton 1:49d4f5ca7d58 136 printf("updateLocation response code: %d\r\n", response);
SeanNewton 1:49d4f5ca7d58 137 if (response == -1)
SeanNewton 1:49d4f5ca7d58 138 {
SeanNewton 1:49d4f5ca7d58 139 printf("Location data transmit post error\n");
SeanNewton 1:49d4f5ca7d58 140 }
SeanNewton 1:49d4f5ca7d58 141
SeanNewton 1:49d4f5ca7d58 142 printf("\r\n");
SeanNewton 1:49d4f5ca7d58 143 /* Wait 5 secs and then loop */
SeanNewton 1:49d4f5ca7d58 144 delay(5000);
SeanNewton 1:49d4f5ca7d58 145 }
SeanNewton 1:49d4f5ca7d58 146 }