Dirk-Willem van Gulik (NXP/mbed) / Mbed 2 deprecated AmpereMeter

Dependencies:   mbed

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 
00003 #include "Servo.h"
00004 
00005 #include "EthernetNetIf.h"
00006 #include "HTTPServer.h"
00007 
00008 #include "mDNSResponder.h"
00009 
00010 #include "RestMeterHandler.h"
00011 
00012 
00013 #define __DEBUG
00014 #include "dbg/dbg.h"
00015 
00016 // Hardware I/O
00017 //
00018 Servo myservo(p21);
00019 DigitalOut netPoll(LED1);
00020 DigitalOut servoChange(LED2);
00021 
00022 // Ethernet, webserver and Bonjour responder
00023 //
00024 EthernetNetIf eth;
00025 HTTPServer srv;
00026 mDNSResponder mdns; // make sure LWIP_IGMP is set in netCfg.h !
00027 
00028 #define MYNAME "Ampere Meter"   // Human readable/descriptive name
00029 #define LUDN "amp-meter"        // Local unqualified DNS name
00030 
00031 #define RANGE 0.80    // scale of 0 .. 1 -- see Servo 
00032 #define MIN 0.36      // scale of 0 .. 1
00033 #define STEP (RANGE / 20)
00034 
00035 float pos = MIN + RANGE/2;
00036 
00037 const char * restCallback(const char * cmd)
00038 {
00039   static char resp[2048];
00040   
00041   if (cmd && (!strcmp(cmd,"min")))
00042     pos -= STEP;
00043   if (cmd && (!strcmp(cmd,"plus")))
00044     pos += STEP;
00045   
00046   if (pos > MIN+RANGE) pos = MIN + RANGE;
00047   if (pos < MIN) pos = MIN;
00048 
00049   myservo = pos;
00050   servoChange = !servoChange;
00051 
00052   snprintf(resp, sizeof(resp), 
00053     "<head><title>%s</title></head>"
00054     "<body>"
00055     "<center><h1><hr/>%s<hr/></h1>", MYNAME, MYNAME);
00056 
00057   // small HTML table to give a visual display
00058   // of the value as a VU style bar.
00059   snprintf(resp+strlen(resp), sizeof(resp)-strlen(resp),
00060     "<table cellpadding=2><tr>");
00061 
00062   for(int i = 0; i < 20; i++) 
00063     snprintf(resp+strlen(resp), sizeof(resp)-strlen(resp),
00064       "<td bgcolor=%s width=10px height=10px>&nbsp;</td>",
00065         ((pos - MIN) * 20 / RANGE > i) ? "444444" : "dddddd");
00066         
00067   snprintf(resp+strlen(resp), sizeof(resp)-strlen(resp),
00068       "</tr><table><br>");
00069 
00070   // footer with the 2 plus/minus command options.
00071   //        
00072   snprintf(resp+strlen(resp), sizeof(resp)-strlen(resp),
00073     "<a href='?cmd=min'>&lt;</a> <font size=+5>[%1.3f]</font> <a href='?cmd=plus'>&gt;</a>"
00074     "</center></body>", pos);
00075     
00076     return resp;   
00077 }
00078 
00079 int main() {
00080     DBG("\n\r\n\r"
00081         "Compiled " MYNAME " (" LUDN ") on " __DATE__ " " __TIME__ 
00082         "\r\n"
00083     );
00084     netPoll = 0;
00085 
00086     // middle the servo
00087     pos = MIN + RANGE/2;
00088     myservo = pos;
00089 
00090     EthernetErr ethErr = eth.setup();
00091     if (ethErr) {
00092         printf("Error %d in setup on DHCP.\r\n", ethErr);
00093         return -1;
00094     }
00095     
00096     // for general static content.    
00097     FSHandler::mount("/webfs", "/htdocs");     // Mount /webfs path on html docroot
00098     srv.addHandler<FSHandler>("/icons");      
00099     
00100     // handling the commands/dynamic pages
00101     //    
00102     RestMeterHandler::attach(&restCallback);
00103     srv.addHandler<RestMeterHandler>("/");
00104     
00105     // start the webserver    
00106     srv.bind(80);
00107     
00108     // Announce above web server - and keep doing that every minute or so.
00109     //
00110     mdns.announce(
00111         eth.getIp(),                // (My) IP address - where announced service runs.
00112         LUDN,                       // DNS name server or service
00113         "_http._tcp",               // protocol
00114         80,                         // Port number
00115         MYNAME,                     // User interface name service
00116         (char *[]) {                // NULL terminated list of KV's = see                                     
00117             "path=/",               // http://www.zeroconf.org/Rendezvous/txtrecords.html
00118             NULL
00119         }
00120     );
00121     
00122 #if __DEEBUG
00123     Ip ip = eth.getIp();
00124     DBG("Visible as http://" LUDN ".lcoal/ or http://%d.%d.%d.%d/\r\n", ip[0],ip[1],ip[2],ip[3]);
00125 #endif
00126     
00127     netPoll = 1;
00128     unsigned char c = 0;
00129     while(true) {
00130         netPoll = ! netPoll;    // flash net polling LED for geedback.
00131         Net::poll();
00132     }
00133 } // main