sensor web server

Dependencies:   EthernetNetIf HTTPServer mbed

Files at this revision

API Documentation at this revision

Comitter:
nobuki
Date:
Sun Nov 18 11:24:12 2012 +0000
Commit message:
version1.0

Changed in this revision

EthernetNetIf.lib Show annotated file Show diff for this revision Revisions of this file
HTTPServer.lib Show annotated file Show diff for this revision Revisions of this file
SensorHandler.cpp Show annotated file Show diff for this revision Revisions of this file
SensorHandler.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
mbed.bld Show annotated file Show diff for this revision Revisions of this file
diff -r 000000000000 -r 803d8094260a EthernetNetIf.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/EthernetNetIf.lib	Sun Nov 18 11:24:12 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/EthernetNetIf/#bc7df6da7589
diff -r 000000000000 -r 803d8094260a HTTPServer.lib
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPServer.lib	Sun Nov 18 11:24:12 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/donatien/code/HTTPServer/#d753966e4d97
diff -r 000000000000 -r 803d8094260a SensorHandler.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SensorHandler.cpp	Sun Nov 18 11:24:12 2012 +0000
@@ -0,0 +1,115 @@
+#include "SensorHandler.h"
+
+//#define __DEBUG
+#include <dbg.h>
+
+AnalogIn g_ain_brightness(p15);
+AnalogIn g_ain_temperature(p20);
+
+SensorHandler::SensorHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket) : HTTPRequestHandler(rootPath, path, pTCPSocket)
+{
+    DBG("SensorHandler()\r\n");
+}
+
+SensorHandler::~SensorHandler()
+{
+    DBG("~SensorHandler()\r\n");
+}
+void SensorHandler::printString( const char* pszString )
+{
+    writeData( pszString, strlen(pszString) );
+}
+
+double GetTemperature()
+{
+    double dV = g_ain_temperature * 3.3;
+    return dV * 100.0;
+}
+
+int GetBrightness()
+{
+    double dV = g_ain_brightness * 3.3;
+    
+    double dR = 0.0;
+    if( 0.005 < (3.3 - dV) )
+    {
+        dR = 10 * 1000 * dV / (3.3 - dV);
+    }
+    
+    if( 100000.0 < dR )
+    {
+        return 0;
+    }
+    if( 4000.0 > dR )
+    {
+        return 2;
+    }
+    return 1;
+}
+
+void SensorHandler::doGet()
+{
+    DBG("doGet()\r\n");
+
+    respHeaders()["Connection"] = "close";
+    
+    printString( "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.01//EN\">\n" );
+    printString( "<html lang=\"ja\">\n" );
+    printString( "<head>\n" );
+    printString( "<meta http-equiv=\"Content-Type\" content=\"text/html; charset=UTF-8\">\n" );
+    printString( "<meta http-equiv=\"Content-Style-Type\" content=\"text/css\">\n" );
+    printString( "<title></title>\n" );
+    printString( "</head>\n" );
+    printString( "<body>\n" );
+    
+    printString( "Temperature : " );
+    char szTemperature[10];
+    sprintf( szTemperature, "%4.1f", GetTemperature() );
+    printString( szTemperature );
+    printString( " [degree]\n" );
+    printString( "<br /><br />\n" );
+
+    printString( "Brightness : "); 
+    switch( GetBrightness() )
+    {
+    case 0:
+        printString("Dark");
+        break;
+    case 1:
+        printString("Ordinarily");
+        break;
+    case 2:
+        printString("Bright");
+        break;
+    }
+    printString( "<br />\n" );
+
+    printString( "</body>\n" );
+    printString( "</html>" );
+}
+
+void SensorHandler::doPost()
+{
+    DBG("doPost()\r\n");
+}
+
+void SensorHandler::doHead()
+{
+    DBG("doHead()\r\n");
+}
+  
+void SensorHandler::onReadable() //Data has been read
+{
+    DBG("onReadable()\r\n");
+}
+
+void SensorHandler::onWriteable() //Data has been written & buf is free
+{
+    DBG("onWriteable()\r\n");
+    close(); //Data written, we can close the connection
+}
+
+void SensorHandler::onClose() //Connection is closing
+{
+    DBG("onClose()\r\n");
+}
diff -r 000000000000 -r 803d8094260a SensorHandler.h
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/SensorHandler.h	Sun Nov 18 11:24:12 2012 +0000
@@ -0,0 +1,26 @@
+#ifndef SENSOR_HANDLER_H
+#define SENSOR_HANDLER_H
+
+#include <HTTPRequestHandler.h>
+
+class SensorHandler : public HTTPRequestHandler
+{
+public:
+  SensorHandler(const char* rootPath, const char* path, TCPSocket* pTCPSocket);
+  virtual ~SensorHandler();
+
+  static inline HTTPRequestHandler* inst(const char* rootPath, const char* path, TCPSocket* pTCPSocket) { return new SensorHandler(rootPath, path, pTCPSocket); } //if we ever could do static virtual functions, this would be one
+
+  virtual void doGet();
+  virtual void doPost();
+  virtual void doHead();
+  
+  virtual void onReadable(); //Data has been read
+  virtual void onWriteable(); //Data has been written & buf is free
+  virtual void onClose(); //Connection is closing
+  
+private:
+  void printString( const char* pszString );
+};
+
+#endif
diff -r 000000000000 -r 803d8094260a main.cpp
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/main.cpp	Sun Nov 18 11:24:12 2012 +0000
@@ -0,0 +1,39 @@
+#include "mbed.h"
+#include "EthernetNetIf.h"
+#include "HTTPServer.h"
+
+#include "SensorHandler.h"
+
+EthernetNetIf ethif( IpAddr(192,168,1,102), // IP
+                     IpAddr(255,255,255,0), // Subnet mask
+                     IpAddr(192,168,1,1),   // Gateway
+                     IpAddr(192,168,1,1) ); // DNS
+HTTPServer server;
+DigitalOut led1(LED1);  // for alive check
+
+int main(void)
+{
+    // EthernetNetIf setup
+    if( ethif.setup() )
+    {
+        return 1;
+    }
+
+    // Set web root path handler
+    server.addHandler<SensorHandler>("/");
+    
+    // Set http port
+    server.bind(80);
+
+    Timer tm;
+    tm.start();
+    while(1)
+    {
+        Net::poll();
+        if( 1.0 < tm.read() )
+        {
+            led1 = !led1;   // high->low, low->high
+            tm.start();
+        }
+    }
+}
\ No newline at end of file
diff -r 000000000000 -r 803d8094260a mbed.bld
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/mbed.bld	Sun Nov 18 11:24:12 2012 +0000
@@ -0,0 +1,1 @@
+http://mbed.org/users/mbed_official/code/mbed/builds/e2ed12d17f06
\ No newline at end of file