Roelants Stef / Mbed 2 deprecated Bel_systeem
Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 // Written by IVA2K
00002 //
00003 // Example of HTTPServer with additional features:
00004 // * Link status indication (LED4)
00005 // * Local file system (create index.htm page on MBED!)
00006 // * RPC-able class (myrpc, allows remote function call that blinks LED1 N times)
00007 //
00008 // Instructions:
00009 // 1  Plug MBED into BoB2 (or other ETH connector breakout)
00010 // 2  Plug ETH connector into your network (needs DHCP to get IP address)
00011 // 3  Power up MBED using USB cable
00012 // 4  Install MBED serial driver (http://mbed.org/handbook/SerialPC)
00013 // 5  Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct)
00014 // 6  Open terminal on the mbed serial port
00015 // 7  Push MBED reset button
00016 // 8  Terminal will display info message with mac address, followed by IP address (if connection succeeds)
00017 // 9  Open browser and enter the following URL, inserting your MBED's IP address:
00018 //    http://10.0.0.321/rpc/myrpc1/blink,10 (instead of 10.0.0.321 use MBED IP address from terminal)
00019 // 10 MBED will blink the LED 10 times
00020 //
00021 // Notes: there are still some bugs in HTTPServer code. 
00022 // To help fight some of them, copy a valid favicon.ico (a 16x16 icon) file to MBED.
00023 //
00024 
00025 #include "mbed.h"
00026 #include "HTTPServer.h"
00027 #include "HTTPRPC.h"
00028 #include "HTTPFS.h"
00029 #include "myrpc.h"
00030 #include "main.h"
00031 #include "TextLCD.h"
00032 
00033 DigitalIn knop(p14,"p14");
00034 DigitalOut bel(p21, "p21");
00035 DigitalOut led1(LED1, "led1");
00036 DigitalOut led2(LED2, "led2");
00037 DigitalOut led3(LED3, "led3");
00038 DigitalOut led4(LED4, "led4");
00039 LocalFileSystem local("local");
00040 myrpc myrpc1(p21, "myrpc1");
00041 
00042 extern Ethernet eth;        // eth is defined elsewhere, avoid compiler error.
00043 Serial pc(USBTX, USBRX);    // serial poort met pc
00044 TextLCD lcd(p5, p6, p7, p8, p9, p10, p11); // rs, rw, e, d0, d1, d2, d3
00045 Ticker eth_timer;           //ethernet timer
00046 
00047 void print_tijd()           //haal uur en min uit npt protocol
00048 {
00049        print_time(cTime->tm_hour, cTime->tm_min);
00050 }
00051 
00052 void print_time(int hour, int minute) 
00053  //timezonecorrectie en tijd op display schrijven
00054  //en led aan laten gaan op ingestelde tijd of door drukknop
00055 {
00056     /* Variable tmphr is 24 hour time adjusted for timezone */
00057  int tmphr = hour + 13 ;
00058     while (tmphr < 0)
00059         tmphr += 24;
00060     tmphr %= 24;
00061     
00062  int tmpmin = minute;
00063     
00064    //24 uren klok
00065     int printhr = tmphr % 24;
00066     if (printhr == 0)
00067         printhr += 24;
00068         lcd.locate(0,1);
00069         lcd.printf("%2d:%02d ", printhr, minute);
00070  if ((tmpmin == 1 & tmphr == 1)+(knop==1)){ //Hier moete een tijd ingeven om wat uur het alarm aan gaat of als op de druk knop word gedrukt. 
00071     bel.write(1);
00072     wait(10); //hoelang het alarm moet afgaan
00073     bel.write(0);
00074     }
00075 }    
00076 
00077 //#define LED_ETH_LINK(val) do { led4=val; } while (0)
00078 void eth_link_status() {    //internet deffinieren
00079     static bool first = true;        // eerste keer ip opvragen
00080     static int eth_link = -1;        // hebben we verbinding met de switch
00081     int new_link = eth.link();
00082     if (eth_link != new_link) {
00083         if (new_link) {
00084             // From http://mbed.org/forum/post/909/
00085             NetServer *net = NetServer::get();
00086             struct ip_addr ip = net->getIPAddr();
00087 //            struct ip_addr gw = net->getGateway();
00088 //            struct ip_addr nm = net->getNetmask();
00089 //            struct ip_addr dns = net->getDNS1();
00090             lcd.locate(0,0); 
00091             lcd.printf("%hhu.%hhu.%hhu.%hhu\n", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF);
00092             }
00093         else {
00094             lcd.locate(0,0);
00095             lcd.printf("<link down>       \n");
00096         }
00097         //LED_ETH_LINK(new_link);
00098         eth_link = new_link;
00099     }
00100 }
00101 
00102 int main(void) 
00103 { //Alle led aan maken dat je kan zijn als het ergens vast zit of niet vind.
00104     led1=1;
00105     led2=1;
00106     led3=1;
00107     led4=1;
00108     pc.baud(9600);
00109 // om internet server te maken
00110     char *hostname = "mbed";
00111     HTTPServer http(hostname);    // Use DHCP
00112     http.timeout(60);
00113 
00114 // dit is nodig om via het internet het rpc protocol uit te voeren
00115     Base::add_rpc_class<AnalogIn>();
00116     Base::add_rpc_class<AnalogOut>();
00117     Base::add_rpc_class<DigitalIn>();
00118     Base::add_rpc_class<DigitalOut>();
00119     Base::add_rpc_class<PwmOut>();
00120     Base::add_rpc_class<Timer>();
00121     Base::add_rpc_class<SPI>();
00122     Base::add_rpc_class<BusOut>();
00123     Base::add_rpc_class<BusIn>();
00124     Base::add_rpc_class<myrpc>();
00125     led1=0;
00126 
00127     http.addHandler(new HTTPRPC());
00128     led2=0;
00129 
00130     http.addHandler(new HTTPFileSystemHandler("/", "/local/"));
00131     led3=0;
00132 
00133 // FIXME: BUG:: If eth is not plugged, http.bind() hangs for a while!
00134     http.bind();
00135     led4 = 0;
00136 
00137     //pc.printf("\r");    // Add linefeed for stupid Hyperterminal
00138     eth_timer.attach(&eth_link_status, 0.1);
00139 //NTP uitlezen vanuit ntp server
00140     Host server(IpAddr(), 123, "0;uk.pool.ntp.org");
00141     ntp.setTime(server);
00142     pc.printf("\nstart");
00143     while(1) 
00144     {
00145         http.poll();    // Have to call this method at least every 250ms to let the http server run.
00146         wait(0.100);
00147    
00148         /* Temporary variables for control loop */
00149         time_t rtc_time = time(NULL);
00150         int minute = -1; 
00151 
00152         /* Update current time */
00153         rtc_time = time(NULL);
00154 
00155         cTime = localtime(&rtc_time);
00156       
00157         /* Only redraw the lcd display if anything changes */
00158         if (cTime->tm_min != minute) 
00159         {
00160             minute = cTime->tm_min;
00161 
00162             print_tijd();
00163             /* Update time from NTP server if it's midnight UTC */
00164 //changed min to 1
00165             if ((cTime->tm_min == 1) && (cTime->tm_hour == 0)) 
00166             {
00167                 Host server(IpAddr(), 123, "0.north-america.pool.ntp.org");
00168 
00169                 ntp.setTime(server); 
00170             }
00171         }
00172     } //return 0; 
00173 }
00174    
00175 //END