M2X with Seeed Ethernet using Seeed Accelerometer demo
Dependencies: LM75B M2XStreamClient jsonlite mbed-rtos mbed
Fork of m2x-seeed_ethernet_demo by
main.cpp@9:facd3d0242b0, 2014-09-30 (annotated)
- Committer:
- SeanNewton
- Date:
- Tue Sep 30 00:34:03 2014 +0000
- Revision:
- 9:facd3d0242b0
- Parent:
- 5:35a77b9b509c
Update M2XStreamClient
;
Who changed what in which revision?
User | Revision | Line number | New 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 | 5:35a77b9b509c | 18 | PwmOut mypwm(PWM_OUT); |
SeanNewton | 1:49d4f5ca7d58 | 19 | AnalogIn temp_sensor(A0); |
SeanNewton | 1:49d4f5ca7d58 | 20 | |
SeanNewton | 1:49d4f5ca7d58 | 21 | /** |
SeanNewton | 1:49d4f5ca7d58 | 22 | * Configure the SPI interfac to the ethernet module |
SeanNewton | 1:49d4f5ca7d58 | 23 | * D11 - MOSI pin |
SeanNewton | 1:49d4f5ca7d58 | 24 | * D12 - MISO pin |
SeanNewton | 1:49d4f5ca7d58 | 25 | * D13 - SCK pin |
SeanNewton | 1:49d4f5ca7d58 | 26 | * D10 - SEL pin |
SeanNewton | 1:49d4f5ca7d58 | 27 | * NC - Reset pin; use D5 otherwise the shield might get into reset loop |
SeanNewton | 1:49d4f5ca7d58 | 28 | */ |
SeanNewton | 1:49d4f5ca7d58 | 29 | |
SeanNewton | 1:49d4f5ca7d58 | 30 | SPI spi(PA_7, PA_6, PA_5); // mosi, miso, sclk |
SeanNewton | 1:49d4f5ca7d58 | 31 | WIZnetInterface eth(&spi, PB_6, PA_10); // spi, cs, reset |
SeanNewton | 1:49d4f5ca7d58 | 32 | |
SeanNewton | 1:49d4f5ca7d58 | 33 | /* Instantiate the M2X Stream Client */ |
SeanNewton | 1:49d4f5ca7d58 | 34 | Client client; |
SeanNewton | 1:49d4f5ca7d58 | 35 | M2XStreamClient m2xClient(&client, m2xKey); |
SeanNewton | 1:49d4f5ca7d58 | 36 | |
SeanNewton | 1:49d4f5ca7d58 | 37 | |
SeanNewton | 1:49d4f5ca7d58 | 38 | /* Call back function for reading back data point data from M2X */ |
SeanNewton | 1:49d4f5ca7d58 | 39 | void on_data_point_found(const char* at, const char* value, int index, void* context, int type) |
SeanNewton | 1:49d4f5ca7d58 | 40 | { |
SeanNewton | 1:49d4f5ca7d58 | 41 | printf("Found a data point, index: %d\r\n", index); |
SeanNewton | 1:49d4f5ca7d58 | 42 | printf("At: %s Value: %s\r\n", at, value); |
SeanNewton | 1:49d4f5ca7d58 | 43 | } |
SeanNewton | 1:49d4f5ca7d58 | 44 | |
SeanNewton | 1:49d4f5ca7d58 | 45 | /* Call back function for reading back location data from M2X */ |
SeanNewton | 1:49d4f5ca7d58 | 46 | void on_location_found(const char* name, |
SeanNewton | 1:49d4f5ca7d58 | 47 | double latitude, |
SeanNewton | 1:49d4f5ca7d58 | 48 | double longitude, |
SeanNewton | 1:49d4f5ca7d58 | 49 | double elevation, |
SeanNewton | 1:49d4f5ca7d58 | 50 | const char* timestamp, |
SeanNewton | 1:49d4f5ca7d58 | 51 | int index, |
SeanNewton | 1:49d4f5ca7d58 | 52 | void* context) |
SeanNewton | 1:49d4f5ca7d58 | 53 | { |
SeanNewton | 1:49d4f5ca7d58 | 54 | printf("Found a location, index: %d\r\n", index); |
SeanNewton | 1:49d4f5ca7d58 | 55 | printf("Name: %s Latitude: %lf Longitude: %lf\r\n", name, latitude, longitude); |
SeanNewton | 1:49d4f5ca7d58 | 56 | printf("Elevation: %lf Timestamp: %s\r\n", elevation, timestamp); |
SeanNewton | 1:49d4f5ca7d58 | 57 | } |
SeanNewton | 1:49d4f5ca7d58 | 58 | |
SeanNewton | 1:49d4f5ca7d58 | 59 | int main() |
SeanNewton | 1:49d4f5ca7d58 | 60 | { |
SeanNewton | 1:49d4f5ca7d58 | 61 | uint8_t mac[6]; |
SeanNewton | 1:49d4f5ca7d58 | 62 | int adc_scale = 65536; //4096; |
SeanNewton | 1:49d4f5ca7d58 | 63 | int B = 3975; |
SeanNewton | 1:49d4f5ca7d58 | 64 | float resistance; |
SeanNewton | 1:49d4f5ca7d58 | 65 | float temperature; |
SeanNewton | 1:49d4f5ca7d58 | 66 | float temperature_f; |
SeanNewton | 1:49d4f5ca7d58 | 67 | char amb_temp[6]; |
SeanNewton | 1:49d4f5ca7d58 | 68 | |
SeanNewton | 5:35a77b9b509c | 69 | mypwm.period_ms(500); |
SeanNewton | 5:35a77b9b509c | 70 | mypwm.pulsewidth_ms(250); |
SeanNewton | 1:49d4f5ca7d58 | 71 | /* Have mbed assign the mac address */ |
SeanNewton | 1:49d4f5ca7d58 | 72 | mbed_mac_address((char *)mac); |
SeanNewton | 1:49d4f5ca7d58 | 73 | |
SeanNewton | 1:49d4f5ca7d58 | 74 | printf("Start...\n"); |
SeanNewton | 1:49d4f5ca7d58 | 75 | wait_ms(3000); |
SeanNewton | 1:49d4f5ca7d58 | 76 | |
SeanNewton | 1:49d4f5ca7d58 | 77 | /* Initialize ethernet interface */ |
SeanNewton | 1:49d4f5ca7d58 | 78 | int ret = eth.init(mac); //Use DHCP |
SeanNewton | 1:49d4f5ca7d58 | 79 | |
SeanNewton | 1:49d4f5ca7d58 | 80 | |
SeanNewton | 1:49d4f5ca7d58 | 81 | if (!ret) { |
SeanNewton | 1:49d4f5ca7d58 | 82 | printf("Initialized, MAC: %s\n", eth.getMACAddress()); |
SeanNewton | 1:49d4f5ca7d58 | 83 | } else { |
SeanNewton | 1:49d4f5ca7d58 | 84 | printf("Error eth.init() - ret = %d\n", ret); |
SeanNewton | 1:49d4f5ca7d58 | 85 | return -1; |
SeanNewton | 1:49d4f5ca7d58 | 86 | } |
SeanNewton | 1:49d4f5ca7d58 | 87 | |
SeanNewton | 1:49d4f5ca7d58 | 88 | |
SeanNewton | 1:49d4f5ca7d58 | 89 | /* Get IP address */ |
SeanNewton | 1:49d4f5ca7d58 | 90 | while (1) { |
SeanNewton | 1:49d4f5ca7d58 | 91 | printf(">>> Get IP address...\n"); |
SeanNewton | 1:49d4f5ca7d58 | 92 | ret = eth.connect(); // Connect to network |
SeanNewton | 1:49d4f5ca7d58 | 93 | |
SeanNewton | 1:49d4f5ca7d58 | 94 | printf("ret: %d\n",ret); |
SeanNewton | 1:49d4f5ca7d58 | 95 | if (ret < 0) { |
SeanNewton | 1:49d4f5ca7d58 | 96 | printf(">>> Could not connect to network! Retrying ...\n"); |
SeanNewton | 1:49d4f5ca7d58 | 97 | wait_ms(3000); |
SeanNewton | 1:49d4f5ca7d58 | 98 | printf("past this point...\n"); |
SeanNewton | 1:49d4f5ca7d58 | 99 | } else { |
SeanNewton | 1:49d4f5ca7d58 | 100 | break; |
SeanNewton | 1:49d4f5ca7d58 | 101 | } |
SeanNewton | 1:49d4f5ca7d58 | 102 | } |
SeanNewton | 1:49d4f5ca7d58 | 103 | |
SeanNewton | 1:49d4f5ca7d58 | 104 | if (!ret) { |
SeanNewton | 1:49d4f5ca7d58 | 105 | printf("IP: %s, MASK: %s, GW: %s\n", |
SeanNewton | 1:49d4f5ca7d58 | 106 | eth.getIPAddress(), eth.getNetworkMask(), eth.getGateway()); |
SeanNewton | 1:49d4f5ca7d58 | 107 | } else { |
SeanNewton | 1:49d4f5ca7d58 | 108 | printf("Error eth.connect() - ret = %d\n", ret); |
SeanNewton | 1:49d4f5ca7d58 | 109 | return -1; |
SeanNewton | 1:49d4f5ca7d58 | 110 | } |
SeanNewton | 1:49d4f5ca7d58 | 111 | |
SeanNewton | 1:49d4f5ca7d58 | 112 | |
SeanNewton | 5:35a77b9b509c | 113 | |
SeanNewton | 5:35a77b9b509c | 114 | mypwm.pulsewidth_ms(500); |
SeanNewton | 1:49d4f5ca7d58 | 115 | |
SeanNewton | 1:49d4f5ca7d58 | 116 | /* Main loop */ |
SeanNewton | 1:49d4f5ca7d58 | 117 | while (true) { |
SeanNewton | 1:49d4f5ca7d58 | 118 | |
SeanNewton | 5:35a77b9b509c | 119 | |
SeanNewton | 5:35a77b9b509c | 120 | |
SeanNewton | 5:35a77b9b509c | 121 | /* Wait 5 secs and then loop */ |
SeanNewton | 5:35a77b9b509c | 122 | delay(2500); |
SeanNewton | 5:35a77b9b509c | 123 | mypwm.pulsewidth_ms(0); |
SeanNewton | 5:35a77b9b509c | 124 | |
SeanNewton | 1:49d4f5ca7d58 | 125 | /* Read ADC value from analog sensor */ |
SeanNewton | 1:49d4f5ca7d58 | 126 | uint16_t a = temp_sensor.read_u16(); |
SeanNewton | 1:49d4f5ca7d58 | 127 | |
SeanNewton | 1:49d4f5ca7d58 | 128 | /* Calculate the temperature in Fareheight and Celsius */ |
SeanNewton | 1:49d4f5ca7d58 | 129 | resistance = (float)(adc_scale-a)*10000/a; //get the resistance of the sensor; |
SeanNewton | 1:49d4f5ca7d58 | 130 | temperature = 1/(log(resistance/10000)/B+1/298.15)-273.15; //convert to temperature via datasheet ; |
SeanNewton | 1:49d4f5ca7d58 | 131 | temperature_f =(1.8 * temperature) + 32.0; |
SeanNewton | 1:49d4f5ca7d58 | 132 | sprintf(amb_temp, "%0.2f", temperature_f); |
SeanNewton | 1:49d4f5ca7d58 | 133 | |
SeanNewton | 1:49d4f5ca7d58 | 134 | printf("Temp Sensor Analog Reading is 0x%X = %d \r\n", a, a); |
SeanNewton | 1:49d4f5ca7d58 | 135 | printf("Current Temperature: %f C %f F \r\n", temperature, temperature_f); |
SeanNewton | 1:49d4f5ca7d58 | 136 | |
SeanNewton | 5:35a77b9b509c | 137 | delay(2500); |
SeanNewton | 5:35a77b9b509c | 138 | mypwm.pulsewidth_ms(500); |
SeanNewton | 5:35a77b9b509c | 139 | |
SeanNewton | 1:49d4f5ca7d58 | 140 | /* Post temperature to M2X site */ |
SeanNewton | 1:49d4f5ca7d58 | 141 | int response = m2xClient.put(feedId, streamName, amb_temp); |
SeanNewton | 1:49d4f5ca7d58 | 142 | printf("Post response code: %d\r\n", response); |
SeanNewton | 1:49d4f5ca7d58 | 143 | if (response == -1) |
SeanNewton | 1:49d4f5ca7d58 | 144 | { |
SeanNewton | 5:35a77b9b509c | 145 | mypwm.pulsewidth_ms(250); |
SeanNewton | 1:49d4f5ca7d58 | 146 | printf("Temperature data transmit post error\n"); |
SeanNewton | 1:49d4f5ca7d58 | 147 | } |
SeanNewton | 1:49d4f5ca7d58 | 148 | /* Update location data */ |
SeanNewton | 1:49d4f5ca7d58 | 149 | response = m2xClient.updateLocation(feedId, name, latitude, longitude, elevation); |
SeanNewton | 1:49d4f5ca7d58 | 150 | printf("updateLocation response code: %d\r\n", response); |
SeanNewton | 1:49d4f5ca7d58 | 151 | if (response == -1) |
SeanNewton | 1:49d4f5ca7d58 | 152 | { |
SeanNewton | 5:35a77b9b509c | 153 | mypwm.pulsewidth_ms(250); |
SeanNewton | 1:49d4f5ca7d58 | 154 | printf("Location data transmit post error\n"); |
SeanNewton | 1:49d4f5ca7d58 | 155 | } |
SeanNewton | 1:49d4f5ca7d58 | 156 | |
SeanNewton | 1:49d4f5ca7d58 | 157 | printf("\r\n"); |
SeanNewton | 5:35a77b9b509c | 158 | |
SeanNewton | 5:35a77b9b509c | 159 | |
SeanNewton | 1:49d4f5ca7d58 | 160 | } |
SeanNewton | 1:49d4f5ca7d58 | 161 | } |