M2X Ethernet demo using Seeed Ethernet W5200 Shield

Dependencies:   LM75B M2XStreamClient jsonlite mbed-rtos mbed Nucleo_Sensor_Shield

Fork of m2x-seeed_ethernet_demo by Sean Newton

Committer:
dangriffin
Date:
Wed Dec 17 20:45:13 2014 +0000
Revision:
11:40d8cfc941ed
Parent:
10:8cb13d51ba2f
Use the public Nucleo Sensor Shield library.

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 #include "mbed.h"
SeanNewton 1:49d4f5ca7d58 4 #include "WIZnetInterface.h"
joe_tijerina 8:3b1e8bfc22df 5 #include "x_cube_mems.h"
SeanNewton 1:49d4f5ca7d58 6
SeanNewton 1:49d4f5ca7d58 7 #define ST_NUCLEO
dangriffin 10:8cb13d51ba2f 8 char feedId[] = "0c76bf26149c9c01d8eec11553d1a6f2"; // Feed you want to post to
dangriffin 10:8cb13d51ba2f 9 char m2xKey[] = "76ee6b8414970d7ade8c75829d0cf879"; // Your M2X access key
joe_tijerina 8:3b1e8bfc22df 10 char tempStreamName[] = "temperature"; // Stream you want to post to M2X
joe_tijerina 8:3b1e8bfc22df 11 char humidityStreamName[] = "humidity"; // Stream you want to post to M2X
SeanNewton 1:49d4f5ca7d58 12
joe_tijerina 8:3b1e8bfc22df 13 char name[] = "Austin"; // Name of current location of datasource
joe_tijerina 8:3b1e8bfc22df 14 double latitude = 30.3748076;
joe_tijerina 8:3b1e8bfc22df 15 double longitude = -97.7386896; // You can also read those values from a GPS
joe_tijerina 8:3b1e8bfc22df 16 double elevation = 300.00;
joe_tijerina 8:3b1e8bfc22df 17 volatile float TEMPERATURE_Value, TEMPERATURE_Value_f;
joe_tijerina 8:3b1e8bfc22df 18 volatile float HUMIDITY_Value;
joe_tijerina 8:3b1e8bfc22df 19 volatile float PRESSURE_Value;
joe_tijerina 8:3b1e8bfc22df 20 volatile AxesRaw_TypeDef MAG_Value;
joe_tijerina 8:3b1e8bfc22df 21 volatile AxesRaw_TypeDef ACC_Value;
joe_tijerina 8:3b1e8bfc22df 22 volatile AxesRaw_TypeDef GYR_Value;
SeanNewton 1:49d4f5ca7d58 23
SeanNewton 4:35a77b9b509c 24 PwmOut mypwm(PWM_OUT);
SeanNewton 1:49d4f5ca7d58 25
SeanNewton 1:49d4f5ca7d58 26 /**
SeanNewton 1:49d4f5ca7d58 27 * Configure the SPI interfac to the ethernet module
SeanNewton 1:49d4f5ca7d58 28 * D11 - MOSI pin
SeanNewton 1:49d4f5ca7d58 29 * D12 - MISO pin
SeanNewton 1:49d4f5ca7d58 30 * D13 - SCK pin
SeanNewton 1:49d4f5ca7d58 31 * D10 - SEL pin
SeanNewton 1:49d4f5ca7d58 32 * NC - Reset pin; use D5 otherwise the shield might get into reset loop
SeanNewton 1:49d4f5ca7d58 33 */
SeanNewton 1:49d4f5ca7d58 34
joe_tijerina 8:3b1e8bfc22df 35
joe_tijerina 8:3b1e8bfc22df 36
SeanNewton 1:49d4f5ca7d58 37 SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk
SeanNewton 1:49d4f5ca7d58 38 WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset
SeanNewton 1:49d4f5ca7d58 39
SeanNewton 1:49d4f5ca7d58 40 /* Instantiate the M2X Stream Client */
SeanNewton 1:49d4f5ca7d58 41 Client client;
SeanNewton 1:49d4f5ca7d58 42 M2XStreamClient m2xClient(&client, m2xKey);
SeanNewton 1:49d4f5ca7d58 43
SeanNewton 1:49d4f5ca7d58 44
SeanNewton 1:49d4f5ca7d58 45 /* Call back function for reading back data point data from M2X */
SeanNewton 1:49d4f5ca7d58 46 void on_data_point_found(const char* at, const char* value, int index, void* context, int type)
SeanNewton 1:49d4f5ca7d58 47 {
SeanNewton 1:49d4f5ca7d58 48 printf("Found a data point, index: %d\r\n", index);
SeanNewton 1:49d4f5ca7d58 49 printf("At: %s Value: %s\r\n", at, value);
SeanNewton 1:49d4f5ca7d58 50 }
SeanNewton 1:49d4f5ca7d58 51
SeanNewton 1:49d4f5ca7d58 52 /* Call back function for reading back location data from M2X */
SeanNewton 1:49d4f5ca7d58 53 void on_location_found(const char* name,
SeanNewton 1:49d4f5ca7d58 54 double latitude,
SeanNewton 1:49d4f5ca7d58 55 double longitude,
SeanNewton 1:49d4f5ca7d58 56 double elevation,
SeanNewton 1:49d4f5ca7d58 57 const char* timestamp,
SeanNewton 1:49d4f5ca7d58 58 int index,
SeanNewton 1:49d4f5ca7d58 59 void* context)
SeanNewton 1:49d4f5ca7d58 60 {
SeanNewton 1:49d4f5ca7d58 61 printf("Found a location, index: %d\r\n", index);
SeanNewton 1:49d4f5ca7d58 62 printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude);
SeanNewton 1:49d4f5ca7d58 63 printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp);
SeanNewton 1:49d4f5ca7d58 64 }
SeanNewton 1:49d4f5ca7d58 65
SeanNewton 1:49d4f5ca7d58 66 int main()
SeanNewton 1:49d4f5ca7d58 67 {
SeanNewton 1:49d4f5ca7d58 68 uint8_t mac[6];
joe_tijerina 8:3b1e8bfc22df 69 int response;
joe_tijerina 8:3b1e8bfc22df 70 uint8_t hts221_id;
joe_tijerina 8:3b1e8bfc22df 71
joe_tijerina 8:3b1e8bfc22df 72 static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
joe_tijerina 8:3b1e8bfc22df 73
joe_tijerina 8:3b1e8bfc22df 74 hts221_id = mems_expansion_board->hts221.ReadID();
joe_tijerina 8:3b1e8bfc22df 75
joe_tijerina 8:3b1e8bfc22df 76 printf("HTS221_ID = 0x%x\n\t\r", hts221_id);
joe_tijerina 8:3b1e8bfc22df 77
SeanNewton 4:35a77b9b509c 78 mypwm.period_ms(500);
SeanNewton 4:35a77b9b509c 79 mypwm.pulsewidth_ms(250);
SeanNewton 1:49d4f5ca7d58 80 /* Have mbed assign the mac address */
SeanNewton 1:49d4f5ca7d58 81 mbed_mac_address((char *)mac);
SeanNewton 1:49d4f5ca7d58 82
SeanNewton 1:49d4f5ca7d58 83 printf("Start...\n");
SeanNewton 1:49d4f5ca7d58 84 wait_ms(3000);
SeanNewton 1:49d4f5ca7d58 85
SeanNewton 1:49d4f5ca7d58 86 /* Initialize ethernet interface */
SeanNewton 1:49d4f5ca7d58 87 int ret = eth.init(mac); //Use DHCP
SeanNewton 1:49d4f5ca7d58 88
SeanNewton 1:49d4f5ca7d58 89
SeanNewton 1:49d4f5ca7d58 90 if (!ret) {
SeanNewton 1:49d4f5ca7d58 91 printf("Initialized, MAC: %s\n", eth.getMACAddress());
SeanNewton 1:49d4f5ca7d58 92 } else {
SeanNewton 1:49d4f5ca7d58 93 printf("Error eth.init() - ret = %d\n", ret);
SeanNewton 1:49d4f5ca7d58 94 return -1;
SeanNewton 1:49d4f5ca7d58 95 }
SeanNewton 1:49d4f5ca7d58 96
SeanNewton 1:49d4f5ca7d58 97
SeanNewton 1:49d4f5ca7d58 98 /* Get IP address */
SeanNewton 1:49d4f5ca7d58 99 while (1) {
SeanNewton 1:49d4f5ca7d58 100 printf(">>> Get IP address...\n");
SeanNewton 1:49d4f5ca7d58 101 ret = eth.connect(); // Connect to network
SeanNewton 1:49d4f5ca7d58 102
SeanNewton 1:49d4f5ca7d58 103 printf("ret: %d\n",ret);
SeanNewton 1:49d4f5ca7d58 104 if (ret < 0) {
SeanNewton 1:49d4f5ca7d58 105 printf(">>> Could not connect to network! Retrying ...\n");
SeanNewton 1:49d4f5ca7d58 106 wait_ms(3000);
SeanNewton 1:49d4f5ca7d58 107 printf("past this point...\n");
SeanNewton 1:49d4f5ca7d58 108 } else {
SeanNewton 1:49d4f5ca7d58 109 break;
SeanNewton 1:49d4f5ca7d58 110 }
SeanNewton 1:49d4f5ca7d58 111 }
SeanNewton 1:49d4f5ca7d58 112
SeanNewton 1:49d4f5ca7d58 113 if (!ret) {
SeanNewton 1:49d4f5ca7d58 114 printf("IP: %s, MASK: %s, GW: %s\n",
SeanNewton 1:49d4f5ca7d58 115 eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway());
SeanNewton 1:49d4f5ca7d58 116 } else {
SeanNewton 1:49d4f5ca7d58 117 printf("Error eth.connect() - ret = %d\n", ret);
SeanNewton 1:49d4f5ca7d58 118 return -1;
SeanNewton 1:49d4f5ca7d58 119 }
SeanNewton 1:49d4f5ca7d58 120
SeanNewton 4:35a77b9b509c 121 mypwm.pulsewidth_ms(500);
SeanNewton 1:49d4f5ca7d58 122
SeanNewton 1:49d4f5ca7d58 123 /* Main loop */
joe_tijerina 8:3b1e8bfc22df 124 while (true) {
SeanNewton 4:35a77b9b509c 125 /* Wait 5 secs and then loop */
SeanNewton 4:35a77b9b509c 126 delay(2500);
SeanNewton 4:35a77b9b509c 127 mypwm.pulsewidth_ms(0);
SeanNewton 4:35a77b9b509c 128
joe_tijerina 8:3b1e8bfc22df 129 /* Read sensors */
joe_tijerina 8:3b1e8bfc22df 130 mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value);
joe_tijerina 8:3b1e8bfc22df 131 mems_expansion_board->hts221.GetHumidity((float *)&HUMIDITY_Value);
joe_tijerina 8:3b1e8bfc22df 132 //mems_expansion_board->lps25h.GetPressure((float *)&PRESSURE_Value);
joe_tijerina 8:3b1e8bfc22df 133 //mems_expansion_board->lis3mdl.GetAxes((AxesRaw_TypeDef *)&MAG_Value);
joe_tijerina 8:3b1e8bfc22df 134 //mems_expansion_board->lsm6ds0.Acc_GetAxes((AxesRaw_TypeDef *)&ACC_Value);
joe_tijerina 8:3b1e8bfc22df 135 //mems_expansion_board->lsm6ds0.Gyro_GetAxes((AxesRaw_TypeDef *)&GYR_Value);
joe_tijerina 8:3b1e8bfc22df 136
SeanNewton 4:35a77b9b509c 137 delay(2500);
SeanNewton 4:35a77b9b509c 138 mypwm.pulsewidth_ms(500);
joe_tijerina 8:3b1e8bfc22df 139
joe_tijerina 8:3b1e8bfc22df 140 /* Post temperature to M2X */
joe_tijerina 8:3b1e8bfc22df 141 TEMPERATURE_Value_f =(1.8 * TEMPERATURE_Value) + 32.0;
joe_tijerina 8:3b1e8bfc22df 142 printf("TEMP: %f C %f F \r\n", TEMPERATURE_Value, TEMPERATURE_Value_f);
joe_tijerina 8:3b1e8bfc22df 143
dangriffin 9:e52869d248ea 144 response = m2xClient.updateStreamValue(feedId, tempStreamName, TEMPERATURE_Value_f);
SeanNewton 1:49d4f5ca7d58 145 printf("Post response code: %d\r\n", response);
SeanNewton 1:49d4f5ca7d58 146 if (response == -1)
SeanNewton 1:49d4f5ca7d58 147 {
SeanNewton 4:35a77b9b509c 148 mypwm.pulsewidth_ms(250);
SeanNewton 1:49d4f5ca7d58 149 printf("Temperature data transmit post error\n");
SeanNewton 1:49d4f5ca7d58 150 }
joe_tijerina 8:3b1e8bfc22df 151
joe_tijerina 8:3b1e8bfc22df 152 /* Post humidity to M2X */
joe_tijerina 8:3b1e8bfc22df 153 printf("HUMIDITY: %f \r\n", HUMIDITY_Value);
dangriffin 9:e52869d248ea 154 response = m2xClient.updateStreamValue(feedId, humidityStreamName, HUMIDITY_Value);
joe_tijerina 8:3b1e8bfc22df 155 printf("Post response code: %d\r\n", response);
joe_tijerina 8:3b1e8bfc22df 156 if (response == -1)
joe_tijerina 8:3b1e8bfc22df 157 {
joe_tijerina 8:3b1e8bfc22df 158 mypwm.pulsewidth_ms(250);
joe_tijerina 8:3b1e8bfc22df 159 printf("Humidity data transmit post error\n");
joe_tijerina 8:3b1e8bfc22df 160 }
joe_tijerina 8:3b1e8bfc22df 161
SeanNewton 1:49d4f5ca7d58 162 /* Update location data */
SeanNewton 1:49d4f5ca7d58 163 response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation);
SeanNewton 1:49d4f5ca7d58 164 printf("updateLocation response code: %d\r\n", response);
SeanNewton 1:49d4f5ca7d58 165 if (response == -1)
SeanNewton 1:49d4f5ca7d58 166 {
SeanNewton 4:35a77b9b509c 167 mypwm.pulsewidth_ms(250);
SeanNewton 1:49d4f5ca7d58 168 printf("Location data transmit post error\n");
SeanNewton 1:49d4f5ca7d58 169 }
SeanNewton 1:49d4f5ca7d58 170
SeanNewton 1:49d4f5ca7d58 171 printf("\r\n");
SeanNewton 4:35a77b9b509c 172
SeanNewton 4:35a77b9b509c 173
SeanNewton 1:49d4f5ca7d58 174 }
SeanNewton 1:49d4f5ca7d58 175 }