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

Revision:
9:02839f8ce7ff
Parent:
8:031089277ba0
--- a/main.cpp	Mon Dec 28 23:54:32 2015 +0000
+++ b/main.cpp	Sat Jan 02 22:08:54 2016 +0000
@@ -4,39 +4,93 @@
 #include "TCPSocketConnection.h"
 #include "x_cube_mems.h"
 
+#define RPINT_DEBUG_INFO 1
 
-/*
-*  WIZnet 5500 Config for nucleo 411
-*/
-Serial pc(SERIAL_TX, SERIAL_RX);
-SPI spi(D11, D12, D13); // mosi, miso, sclk
+#if RPINT_DEBUG_INFO
+#define DEBUG pc.printf
+#else
+#define DEBUG(...)
+#endif
 
 #define USE_DHCP    1
 
-const char * IP_Addr    = "192.168.2.72";
-const char * IP_Subnet  = "255.255.255.0";
-const char * IP_Gateway = "192.168.2.2";
-unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
-//
-// Fill these field in from you ATT M2X Account
-//
-char deviceId[] = "<deviceID>"; // Device you want to push to
-char streamName[] = "<streamID>"; // Stream you want to push to
-char m2xKey[] = "<deviceAPIKey>"; // Your M2X API Key or Master API Key
+Serial pc(SERIAL_TX, SERIAL_RX);
+SPI spi(SPI_MOSI, SPI_MISO, SPI_SCK); // mosi, miso, sclk
+WIZnetInterface ethernet(&spi,D10, D3);/*  WIZnet 5500 Config for nucleo 411 */
+
+/* Fill these field in from you ATT M2X Account */
+char streamName_T[] = "temperature"; // Stream you want to push to
+char streamName_H[] = "humidity";    // Stream you want to push to
+char streamName_P[] = "pressure";    // Stream you want to push to
+
+char m2xKey[] = "cf5454902aad1b81f4bf55743fbc0ccc"; // Your M2X API Key or Master API Key
+char deviceId[] = "1e166d399d3996f3b9da939b6b78dab9"; // Device you want to push to
+/* to access the data go to: https://m2x.att.com/catalog/1e166d399d3996f3b9da939b6b78dab9 */
+
+Client client;
+M2XStreamClient m2xClient(&client, m2xKey,1,"52.22.150.98");/* Initialize the M2X client: api-m2x.att.com */
 
 volatile float TEMPERATURE_Value_C;
+volatile float HUMIDITY_Value;
+volatile float PRESSURE_Value;
+
+/* location: https://www.google.com/maps/@47.6752865,-122.1444922,13.5z */
+double latitude = 47.6752865;
+double longitude = -122.1444922; // You can also read those values from a GPS
+double elevation = 13.5;
+
+X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance(); /* Create sensor board object */
+
+Ticker update_task_ticker;
+
+void ethernet_init(void);
+void update_task(void);
 
 int main()
 {
-  /* Create sensor board object */
-  static X_CUBE_MEMS *mems_expansion_board = X_CUBE_MEMS::Instance();
+  ethernet_init();
+
+  int ret = m2xClient.updateLocation(deviceId, streamName_T, latitude, longitude, elevation);
+
+  update_task_ticker.attach(&update_task, 5.0);
   
-  pc.baud(115200); // console terminal to 115200 baud
+  while (true) 
+  {
+  }
+}
+
+void update_task()
+{
+    int ret;
+    
+    mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);/* Read temperature */
+    mems_expansion_board->hts221.GetHumidity   ((float *)&HUMIDITY_Value);
+    mems_expansion_board->lps25h.GetPressure   ((float *)&PRESSURE_Value);
+    
+    ret = m2xClient.updateStreamValue(deviceId, streamName_T, TEMPERATURE_Value_C);/* Send to M2X */
+    ret = m2xClient.updateStreamValue(deviceId, streamName_P, PRESSURE_Value);/* Send to M2X */
+    ret = m2xClient.updateStreamValue(deviceId, streamName_H, HUMIDITY_Value);/* Send to M2X */
+        
+    DEBUG("Temperature:\t\t %f C\r\n", TEMPERATURE_Value_C);
+    DEBUG("Humidity:\t\t %f%%\r\n", HUMIDITY_Value);
+    DEBUG("Pressure:\t\t %f hPa\r\n", PRESSURE_Value);
+    DEBUG("send() returned %d\r\n", ret);
+}
+
+void ethernet_init()
+{
+#if (USE_DHCP == 0)
+const char * IP_Addr    = "192.168.1.2";
+const char * IP_Subnet  = "255.255.255.0";
+const char * IP_Gateway = "192.168.1.1";
+#endif
+
+unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
+//unsigned char MAC_Addr[6] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xDE };
 
   spi.frequency(1000000);
-  WIZnetInterface ethernet(&spi,D10, D3);
 
-  pc.printf("Ethernet Init\r\n");
+  DEBUG("Ethernet Init\r\n");
 #if USE_DHCP
   int ret = ethernet.init(MAC_Addr);
 #else
@@ -45,40 +99,22 @@
 
   if (!ret) 
   {
-    pc.printf("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
+    DEBUG("Initialized, MAC: %s\r\n", ethernet.getMACAddress());
     ret = ethernet.connect();
     
     if (!ret) 
     {
-      pc.printf("IP: %s, MASK: %s, GW: %s\r\n",
-      ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
+      DEBUG("IP: %s, MASK: %s, GW: %s\r\n", ethernet.getIPAddress(), ethernet.getNetworkMask(), ethernet.getGateway());
     }
     else
     {
-      pc.printf("Error ethernet.connect() - ret = %d\r\n", ret);
+      DEBUG("Error ethernet.connect() - ret = %d\r\n", ret);
       exit(0);
     }
   }
   else
   {
-    pc.printf("Error ethernet.init() - ret = %d\r\n", ret);
+    DEBUG("Error ethernet.init() - ret = %d\r\n", ret);
     exit(0);
   }
-
-    // Initialize the M2X client
-    Client client;
-    M2XStreamClient m2xClient(&client, m2xKey,1,"52.22.150.98"); // api-m2x.att.com
-    
-    volatile int randomNumber = 0;
-
-    while (true) 
-    {
-        // send the temperature to M2X every 5 seconds
-        mems_expansion_board->hts221.GetTemperature((float *)&TEMPERATURE_Value_C);/* Read temperature */
-        ret = m2xClient.updateStreamValue(deviceId, streamName, TEMPERATURE_Value_C);/* Send to M2X */
-        
-        pc.printf("Temperature:\t\t %f C\r\n", TEMPERATURE_Value_C);
-        printf("send() returned %d\r\n", ret);
-        wait(5.0);
-    }
-}
\ No newline at end of file
+}