.

Dependencies:   BME280 BMP280 TextLCD mbed

Fork of CW_watchdog_09012018_copy by Calvin Kalintra

Files at this revision

API Documentation at this revision

Comitter:
FranciscoSalle
Date:
Wed Jan 10 01:27:01 2018 +0000
Parent:
5:8aa72ee456fc
Commit message:
.;

Changed in this revision

main.cpp Show annotated file Show diff for this revision Revisions of this file
net.h Show annotated file Show diff for this revision Revisions of this file
diff -r 8aa72ee456fc -r f95438838ece main.cpp
--- a/main.cpp	Tue Jan 09 15:45:56 2018 +0000
+++ b/main.cpp	Wed Jan 10 01:27:01 2018 +0000
@@ -1,19 +1,28 @@
 #include "mbed.h"
 #include "TextLCD.h"
 #include "BME280.h"
-#include "sd.h"
+#include "net.h"
 #define watchdog_time 15
 //DigitalOut myled(LED1);
-int dislcd = 0, wdcounter = 0, watchdog = 0;
+int dislcd = 1, wdcounter = 0, watchdog = 0, netflag = 1;
 float T = 15;
 char ch = 0;
 Ticker display;
 Ticker second_pass;
 InterruptIn button(USER_BUTTON);
+//InterruptIn button2(PE_12);
+//osThreadId mainThreadID;
 
 //NVIC_SetPriority(display, 0);     
 //NVIC_SetPriority(second_pass, 1);
 //NVIC_SetPriority(button, 1);
+//void network_main()
+//{
+//    while(1)
+//    {
+//        network();
+//    }
+//}
 
 
 void second_tick()
@@ -47,7 +56,7 @@
        NVIC_SystemReset();
    }
 }
-void watchdog()
+void watchdog_start()
 {
     wdcounter = 0;
     watchdog = 1;
@@ -78,30 +87,25 @@
     else
     {dislcd = 0;}
 }
-
-
-int main()
+void network_bit()
 {
-    lcd.cls();
-    
-    //display_date_time();
-//    while (pointer != 14)
-//    {
-//        select_date();
-//    }
-    //bmp.initialize();
-    pc.printf("\n\rputty ready\n\r");
-    display.attach(&lcddisp, T);
-    second_pass.attach(&second_tick, 1.0);
-    button.rise(&check_display);
+    network();
+    //osSignalSet(mainThreadID, 0x1);
+}
+void putty_main()
+{
     while(1)
-    {
-        check_display();
+    {   
         if (putty_enabled == 1)
         {
-            //watchdog();
+            //watchdog_start();
+            
+            pc.printf("\n\rputty ready\n\r");
+            //stdio_mutex.lock();
             putty_write();
-            watchdog = 0;
+            //stdio_mutex.unlock();
+            
+            //watchdog = 0;
         }
         
         if (putty_enabled == 0)
@@ -122,6 +126,67 @@
             display.attach(&lcddisp, T);
             interrupt = 0;
         }
+        }
+        
+}
+
+int main()
+{
+    //network_bit();
+    lcd.cls();
+    //getdata();
+    //display_date_time();
+//    while (pointer != 14)
+//    {
+//        select_date();
+//    }
+    //bmp.initialize();
+    
+    //t1.start(network_main);
+    //Thread t2(osPriorityBelowNormal);
+    //Thread t1(osPriorityAboveNormal);
+    display.attach(&lcddisp, T);
+    second_pass.attach(&second_tick, 1.0);
+    //net_tick.attach(&network, 15);
+    button.rise(&check_display);
+    network();
+    
+    while(1)
+    {
+        Thread t2;
+        Thread t1;
+        t1.start(network_bit);
+        Thread::wait(2000);
+        t1.terminate();
+        t2.start(putty_main);
+        Thread::wait(13000);
+        t2.terminate();
+        //check_display();
+//        if (putty_enabled == 1)
+//        {
+//            //watchdog_start();
+//            putty_write();
+//            //watchdog = 0;
+//        }
+//        
+//        if (putty_enabled == 0)
+//        {
+//            while(ch != 'p')
+//            {
+//                ch = 0;
+//                yellowLED = 1;
+//                ch = pc.getc();
+//            }
+//            putty_enabled = 1;
+//        }
+//        yellowLED = 0;
+//        check_display();
+//        if (interrupt == 1)
+//        {
+//            T = t;
+//            display.attach(&lcddisp, T);
+//            interrupt = 0;
+//        }
         //if(sw1 == 1 && sw2 == 1)
 //        {
 //            sd();
diff -r 8aa72ee456fc -r f95438838ece net.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/net.h	Wed Jan 10 01:27:01 2018 +0000
@@ -0,0 +1,119 @@
+#if !FEATURE_LWIP
+    #error [NOT_SUPPORTED] LWIP not supported for this target
+#endif
+
+#include "mbed.h"
+#include "EthernetInterface.h"
+#include "TCPServer.h"
+#include "TCPSocket.h"
+#include "sd.h"
+#include <iostream>
+#include <string> 
+
+#define HTTP_STATUS_LINE "HTTP/1.0 200 OK"
+#define HTTP_HEADER_FIELDS "Content-Type: text/html; charset=utf-8"
+#define HTTP_MESSAGE_BODY1 ""                                    \
+"<html>" "\r\n"                                                  \
+"  <body style=\"display:flex;text-align:center\">" "\r\n"       \
+"    <div style=\"margin:auto\">" "\r\n"                         \
+"      <h1>Hello World</h1>" "\r\n"                              \
+"      <p>The LDR value is "                                     
+#define HTTP_MESSAGE_BODY2 ""                                    \
+       "</p>" "\r\n"                                             \       
+"      <p1>The Temperature value is"                                      
+#define HTTP_MESSAGE_BODY6 ""                                    \
+       "</p1>" "\r\n"                                            \       
+"      <p2>The Pressure value is "                                     
+#define HTTP_MESSAGE_BODY4 ""                                    \
+       "</p2>" "\r\n"                                            \
+"    </div>" "\r\n"                                              \
+"  </body>" "\r\n"                                               \
+"</html>"                                   
+
+#define HTTP_RESPONSE HTTP_STATUS_LINE "\r\n"   \
+                      HTTP_HEADER_FIELDS "\r\n" \
+                      "\r\n"                    \
+                      HTTP_MESSAGE_BODY "\r\n"
+
+#define IP        "10.0.0.1"
+#define NETMASK   "255.0.0.0"
+#define GATEWAY   "10.0.0.1"
+
+//BMP280 bmp(D14,D15,0x76);
+//AnalogIn ldr(PA_0);
+//Mutex stdio_mutex;
+
+void network()
+{
+    //printf("Basic HTTP server example\n");
+    //Configure an ethernet connection
+    
+    EthernetInterface eth;
+    eth.set_network(IP, NETMASK, GATEWAY);
+    eth.connect();
+    //stdio_mutex.lock();
+    printf("The target IP address is '%s'\n", eth.get_ip_address());
+    //stdio_mutex.unlock();
+    //Now setup a web server
+    TCPServer srv;           //TCP/IP Server
+    TCPSocket clt_sock;      //Socket for communication
+    SocketAddress clt_addr;  //Address of incoming connection
+    
+    /* Open the server on ethernet stack */
+    srv.open(&eth);
+    
+    /* Bind the HTTP port (TCP 80) to the server */
+    srv.bind(eth.get_ip_address(), 80);
+    
+    /* Can handle 5 simultaneous connections */
+    srv.listen(5);
+    //while (true) {
+        using namespace std;
+        //Block and wait on an incoming connection
+        srv.accept(&clt_sock, &clt_addr);
+        //printf("accept %s:%d\n", clt_addr.get_ip_address(), clt_addr.get_port());
+        
+        //Uses a C++ string to make it easier to concatinate
+        string response;
+        //This is a C string
+        char ldr_str[64];
+        
+        //Read the LDR value
+        float u = 7;
+        
+        //Convert to a C String
+        sprintf(ldr_str, "%5.3f", u );
+        //printf("LDR: %5.3f\n\r", u);
+     
+     string response1,response2;
+     float tempr, pressurer;
+     tempr = 64;
+     pressurer = 322;
+     char data_temp[64] , data_press[64];
+     //tempf = bmp.getTemperature();
+     //pressuref = bmp.getPressure();
+     sprintf(data_temp, "%5.3f", tempr );
+     sprintf(data_press, "%5.3f", pressurer );
+     
+
+        
+        //Build the C++ string response
+        response = HTTP_MESSAGE_BODY1;
+        response += ldr_str;
+        response += HTTP_MESSAGE_BODY2;
+        
+        
+        response1 += data_temp;
+        response1 += HTTP_MESSAGE_BODY6;
+        
+        
+        response2 += data_press;
+        response2 += HTTP_MESSAGE_BODY4;
+
+        
+        //Send static HTML response (as a C string)
+        clt_sock.send(response.c_str(), response.size()+6); 
+        clt_sock.send(response1.c_str(), response1.size()+6);
+        clt_sock.send(response2.c_str(), response2.size()+6);   
+    //}
+}