My fork of the blinky project to support ethernet and twitter

Dependencies:   EthernetInterface HTTPClient HTU21D OAuth4Tw SDFileSystem SPI_TFT_ILI9341 SerialShell mbed-rtos mbed-src

Fork of mbed_blinky by Mbed

Revision:
8:e3fb74a4772c
Parent:
7:aafb9225f866
--- a/main.cpp	Wed Apr 29 14:55:06 2015 +0000
+++ b/main.cpp	Sat May 02 02:18:51 2015 +0000
@@ -6,10 +6,13 @@
 #include "SPI_TFT_ILI9341.h"
 #include "SDFileSystem.h"
 #include "Shell.h"
+#include "HTU21D.h"
+#include "oauth.h"
 
+Serial pc(p28, p27); // (USBTX, USBRX);
 DigitalOut myled(LED1);
-//EthernetInterface eth;
-Serial pc(p9,p10);
+EthernetInterface eth;
+HTU21D htu21d(p9,p10);
 
 SDFileSystem sd(p5,p6,p7,p8,"sd"); // mosi, miso, sck, cs 
 SPI_TFT_ILI9341 TFT(p11,p12,p13,p15, p16, p17 ); // mosi, miso, sck, cs, eset, dc 
@@ -20,13 +23,32 @@
 unsigned char shellStack[SHELL_STACK_SIZ];
 Shell shell(&pc);
 
-// Local Commands
-/**
- *  \brief Gets the amount of free memory
- *  \param none
- *  \return none
- **/
-static void cmd_mem(Stream * chp, int argc, char * argv[])
+// IMPORTANT: please change the following keys for your application.
+
+static char const consumer_key[] = "AAAAAAAAAAAAAAAAAAAAAA";
+static char const consumer_secret[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+static char const token_key[] = "00000000-AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+static char const token_secret[] = "AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA";
+
+//
+// Sends tweets to Twitter
+//
+void tweet(char const *message)
+{
+
+    std::string uri = "http://api.twitter.com/1/statuses/update.xml";
+    uri += "?status=";
+    uri += oauth_url_escape(message);
+
+    std::string req_url;
+    std::string postarg;
+
+    req_url = oauth_sign_url2(uri.c_str(), &postarg, OA_HMAC, 0, consumer_key, consumer_secret, token_key, token_secret);
+    oauth_http_post(req_url.c_str(), postarg.c_str());
+}
+
+static uint32_t get_mem()
 {
    // In order to get free mem within RTOS
    // we need to get the main thread's stack pointer
@@ -65,8 +87,20 @@
    uint32_t diff = bottom_of_stack - (uint32_t) top_of_heap;
 
    free((void *) top_of_heap);
+   
+   return diff;    
+}
+
+// Local Commands
+/**
+ *  \brief Gets the amount of free memory
+ *  \param none
+ *  \return none
+ **/
+static void cmd_mem(Stream * chp, int argc, char * argv[])
+{
    chp->printf("Available Memory : %d bytes\r\n",
-        diff);
+        get_mem());
 }
 
 /**
@@ -111,6 +145,16 @@
    if (err != 1) TFT.printf(" - Err: %d", err); 
 }
 
+/**
+ *  \brief Gets sensor data on HTU21D
+ *  \param none
+ *  \return int
+ **/
+static void cmd_sensor(Stream * chp, int argc, char * argv[])
+{
+    chp->printf("Temperature : %d °C\r\n", htu21d.sample_ctemp());
+    chp->printf("Humitdity : %d%%\r\n", htu21d.sample_humid());
+}
 
 /**
  * \brief Initialize LCD
@@ -125,7 +169,7 @@
     lcdOn = 1;
 
     TFT.claim(stdout);  
-    TFT.set_orientation(1);
+    TFT.set_orientation(2);
     TFT.background(Black);    // set background to black
     TFT.foreground(White);    // set chars to white
     TFT.cls();                // clear the screen
@@ -138,7 +182,6 @@
 
 }
 
-
 void led1_thread(void const *args) {
     while (true) {
         myled = !myled;
@@ -148,32 +191,39 @@
 
 int main() {
     Thread * thread;
+    Thread * thread2;
     pc.baud(115200);
 
     pc.printf("\r\nStarting Mbed ...\r\n");
- //   pc.printf("Inititalizing ethernet ....\r\n");
- //   eth.init(); // Use DHCP
- //   eth.connect();
- //   pc.printf("IP Address is %s\n", eth.getIPAddress());
-
-    // Initialize the LCD
+    //Initialize the LCD
+    pc.printf("Initializing LCD ...\r\n");
     init_LCD();
     
+    printf("Inititalizing ethernet ....\r\n");
+    eth.init(); // Use DHCP
+    eth.connect();
+    printf("IP Address is %s\n", eth.getIPAddress());
+    
     // After initializing the ethernet interface
     // run it in its own thread
     thread = new Thread(led1_thread);
 
     // Start the shell
-    pc.printf("Starting debug shell ...\r\n");
+    printf("Starting debug shell ...\r\n");
     shell.addCommand("ls", cmd_ls);
-    //shell.addCommand("load", cmd_load);
+    shell.addCommand("load", cmd_load);
     shell.addCommand("mem", cmd_mem);
+    shell.addCommand("sensor", cmd_sensor);
     shell.start(osPriorityNormal, SHELL_STACK_SIZ, shellStack);
-    
+    printf("Shell now running!\r\n");
+    printf("Available Memory : %d\r\n", get_mem());
+        
     // Do something logical here
     // other than looping
     while(1) {
-        wait(0.2);
+        printf("Temperature : %d °C\r\n", htu21d.sample_ctemp());
+        printf("Humitdity : %d%%\r\n", htu21d.sample_humid());        
+        wait(10);
     }
     
     thread->terminate();