Use the STM32F411 Nucleo Board, Nucleo Sensor Shield, WIZnet5500 Ethernet to upload temperature data to M2X

Dependencies:   M2XStreamClient Nucleo_Sensor_Shield WIZnet_Library jsonlite mbed

Fork of M2X_Nucleo411_ESP8266-wifi by AT&T Developer Summit Hackathon 2016

Committer:
sjallouli
Date:
Sat Jan 02 22:12:46 2016 +0000
Revision:
10:ac1aa06eec29
Parent:
9:02839f8ce7ff
Send Temperature, Pressure and Humidity to M2X
; separate function for Ethernet Init
; Sensor and M2X runs on a Ticker function every 5 seconds

Who changed what in which revision?

UserRevisionLine numberNew contents of line
jb8414 0:ece599ab76bb 1 #include "mbed.h"
jb8414 0:ece599ab76bb 2 #include "M2XStreamClient.h"
sjallouli 7:72e29cb05e2b 3 #include "WIZnetInterface.h"
mbedAustin 3:694a1a67b156 4 #include "TCPSocketConnection.h"
sjallouli 7:72e29cb05e2b 5 #include "x_cube_mems.h"
jb8414 0:ece599ab76bb 6
sjallouli 9:02839f8ce7ff 7 #define RPINT_DEBUG_INFO 1
jb8414 0:ece599ab76bb 8
sjallouli 9:02839f8ce7ff 9 #if RPINT_DEBUG_INFO
sjallouli 9:02839f8ce7ff 10 #define DEBUG pc.printf
sjallouli 9:02839f8ce7ff 11 #else
sjallouli 9:02839f8ce7ff 12 #define DEBUG(...)
sjallouli 9:02839f8ce7ff 13 #endif
sjallouli 7:72e29cb05e2b 14
sjallouli 7:72e29cb05e2b 15 #define USE_DHCP 1
mbedAustin 6:694279899cf2 16
sjallouli 9:02839f8ce7ff 17 Serial pc(SERIAL_TX, SERIAL_RX);
sjallouli 9:02839f8ce7ff 18 SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk
sjallouli 9:02839f8ce7ff 19 WIZnetInterface ethernet(&spi,D10, D3);/* WIZnet 5500 Config for nucleo 411 */
sjallouli 9:02839f8ce7ff 20
sjallouli 9:02839f8ce7ff 21 /* Fill these field in from you ATT M2X Account */
sjallouli 9:02839f8ce7ff 22 char streamName_T[] = "temperature"; // Stream you want to push to
sjallouli 9:02839f8ce7ff 23 char streamName_H[] = "humidity"; // Stream you want to push to
sjallouli 9:02839f8ce7ff 24 char streamName_P[] = "pressure"; // Stream you want to push to
sjallouli 9:02839f8ce7ff 25
sjallouli 9:02839f8ce7ff 26 char m2xKey[] = "cf5454902aad1b81f4bf55743fbc0ccc"; // Your M2X API Key or Master API Key
sjallouli 9:02839f8ce7ff 27 char deviceId[] = "1e166d399d3996f3b9da939b6b78dab9"; // Device you want to push to
sjallouli 9:02839f8ce7ff 28 /* to access the data go to: https://m2x.att.com/catalog/1e166d399d3996f3b9da939b6b78dab9 */
sjallouli 9:02839f8ce7ff 29
sjallouli 9:02839f8ce7ff 30 Client client;
sjallouli 9:02839f8ce7ff 31 M2XStreamClient m2xClient(&client, m2xKey,1,"52.22.150.98");/* Initialize the M2X client: api-m2x.att.com */
sjallouli 7:72e29cb05e2b 32
sjallouli 7:72e29cb05e2b 33 volatile float TEMPERATURE_Value_C;
sjallouli 9:02839f8ce7ff 34 volatile float HUMIDITY_Value;
sjallouli 9:02839f8ce7ff 35 volatile float PRESSURE_Value;
sjallouli 9:02839f8ce7ff 36
sjallouli 9:02839f8ce7ff 37 /* location: https://www.google.com/maps/@47.6752865,-122.1444922,13.5z */
sjallouli 9:02839f8ce7ff 38 double latitude = 47.6752865;
sjallouli 9:02839f8ce7ff 39 double longitude = -122.1444922; // You can also read those values from a GPS
sjallouli 9:02839f8ce7ff 40 double elevation = 13.5;
sjallouli 9:02839f8ce7ff 41
sjallouli 9:02839f8ce7ff 42 X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance(); /* Create sensor board object */
sjallouli 9:02839f8ce7ff 43
sjallouli 9:02839f8ce7ff 44 Ticker update_task_ticker;
sjallouli 9:02839f8ce7ff 45
sjallouli 9:02839f8ce7ff 46 void ethernet_init(void);
sjallouli 9:02839f8ce7ff 47 void update_task(void);
mbedAustin 3:694a1a67b156 48
jb8414 0:ece599ab76bb 49 int main()
jb8414 0:ece599ab76bb 50 {
sjallouli 9:02839f8ce7ff 51 ethernet_init();
sjallouli 9:02839f8ce7ff 52
sjallouli 9:02839f8ce7ff 53 int ret = m2xClient.updateLocation(deviceId, streamName_T, latitude, longitude, elevation);
sjallouli 9:02839f8ce7ff 54
sjallouli 9:02839f8ce7ff 55 update_task_ticker.attach(&update_task, 5.0);
sjallouli 7:72e29cb05e2b 56
sjallouli 9:02839f8ce7ff 57 while (true)
sjallouli 9:02839f8ce7ff 58 {
sjallouli 9:02839f8ce7ff 59 }
sjallouli 9:02839f8ce7ff 60 }
sjallouli 9:02839f8ce7ff 61
sjallouli 9:02839f8ce7ff 62 void update_task()
sjallouli 9:02839f8ce7ff 63 {
sjallouli 9:02839f8ce7ff 64 int ret;
sjallouli 9:02839f8ce7ff 65
sjallouli 9:02839f8ce7ff 66 mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);/* Read temperature */
sjallouli 9:02839f8ce7ff 67 mems_expansion_board->hts221.GetHumidity ((float *)&HUMIDITY_Value);
sjallouli 9:02839f8ce7ff 68 mems_expansion_board->lps25h.GetPressure ((float *)&PRESSURE_Value);
sjallouli 9:02839f8ce7ff 69
sjallouli 9:02839f8ce7ff 70 ret = m2xClient.updateStreamValue(deviceId, streamName_T, TEMPERATURE_Value_C);/* Send to M2X */
sjallouli 9:02839f8ce7ff 71 ret = m2xClient.updateStreamValue(deviceId, streamName_P, PRESSURE_Value);/* Send to M2X */
sjallouli 9:02839f8ce7ff 72 ret = m2xClient.updateStreamValue(deviceId, streamName_H, HUMIDITY_Value);/* Send to M2X */
sjallouli 9:02839f8ce7ff 73
sjallouli 9:02839f8ce7ff 74 DEBUG("Temperature:\t\t %f C\r\n", TEMPERATURE_Value_C);
sjallouli 9:02839f8ce7ff 75 DEBUG("Humidity:\t\t %f%%\r\n", HUMIDITY_Value);
sjallouli 9:02839f8ce7ff 76 DEBUG("Pressure:\t\t %f hPa\r\n", PRESSURE_Value);
sjallouli 9:02839f8ce7ff 77 DEBUG("send() returned %d\r\n", ret);
sjallouli 9:02839f8ce7ff 78 }
sjallouli 9:02839f8ce7ff 79
sjallouli 9:02839f8ce7ff 80 void ethernet_init()
sjallouli 9:02839f8ce7ff 81 {
sjallouli 9:02839f8ce7ff 82 #if (USE_DHCP == 0)
sjallouli 9:02839f8ce7ff 83 const char * IP_Addr = "192.168.1.2";
sjallouli 9:02839f8ce7ff 84 const char * IP_Subnet = "255.255.255.0";
sjallouli 9:02839f8ce7ff 85 const char * IP_Gateway = "192.168.1.1";
sjallouli 9:02839f8ce7ff 86 #endif
sjallouli 9:02839f8ce7ff 87
sjallouli 9:02839f8ce7ff 88 unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
sjallouli 9:02839f8ce7ff 89 //unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDE };
sjallouli 7:72e29cb05e2b 90
sjallouli 7:72e29cb05e2b 91 spi.frequency(1000000);
sjallouli 7:72e29cb05e2b 92
sjallouli 9:02839f8ce7ff 93 DEBUG("Ethernet Init\r\n");
sjallouli 7:72e29cb05e2b 94 #if USE_DHCP
sjallouli 7:72e29cb05e2b 95 int ret = ethernet.init(MAC_Addr);
sjallouli 7:72e29cb05e2b 96 #else
sjallouli 7:72e29cb05e2b 97 int ret = ethernet.init(MAC_Addr,IP_Addr,IP_Subnet,IP_Gateway);
sjallouli 7:72e29cb05e2b 98 #endif
mbedAustin 3:694a1a67b156 99
sjallouli 7:72e29cb05e2b 100 if (!ret)
sjallouli 7:72e29cb05e2b 101 {
sjallouli 9:02839f8ce7ff 102 DEBUG("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
sjallouli 7:72e29cb05e2b 103 ret = ethernet.connect();
sjallouli 7:72e29cb05e2b 104
sjallouli 7:72e29cb05e2b 105 if (!ret)
sjallouli 7:72e29cb05e2b 106 {
sjallouli 9:02839f8ce7ff 107 DEBUG("IP: %s, MASK: %s, GW: %s\r\n", ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
sjallouli 7:72e29cb05e2b 108 }
sjallouli 7:72e29cb05e2b 109 else
sjallouli 7:72e29cb05e2b 110 {
sjallouli 9:02839f8ce7ff 111 DEBUG("Error ethernet.connect() - ret = %d\r\n", ret);
sjallouli 7:72e29cb05e2b 112 exit(0);
sjallouli 7:72e29cb05e2b 113 }
sjallouli 7:72e29cb05e2b 114 }
sjallouli 7:72e29cb05e2b 115 else
sjallouli 7:72e29cb05e2b 116 {
sjallouli 9:02839f8ce7ff 117 DEBUG("Error ethernet.init() - ret = %d\r\n", ret);
sjallouli 7:72e29cb05e2b 118 exit(0);
sjallouli 7:72e29cb05e2b 119 }
sjallouli 9:02839f8ce7ff 120 }