qsdf

Committer:
StefR
Date:
Sun Oct 30 17:07:17 2011 +0000
Revision:
0:d63b2aa1d448
dqf

Who changed what in which revision?

UserRevisionLine numberNew contents of line
StefR 0:d63b2aa1d448 1 // Written by IVA2K
StefR 0:d63b2aa1d448 2 //
StefR 0:d63b2aa1d448 3 // Example of HTTPServer with additional features:
StefR 0:d63b2aa1d448 4 // * Link status indication (LED4)
StefR 0:d63b2aa1d448 5 // * Local file system (create index.htm page on MBED!)
StefR 0:d63b2aa1d448 6 // * RPC-able class (myrpc, allows remote function call that blinks LED1 N times)
StefR 0:d63b2aa1d448 7 //
StefR 0:d63b2aa1d448 8 // Instructions:
StefR 0:d63b2aa1d448 9 // 1 Plug MBED into BoB2 (or other ETH connector breakout)
StefR 0:d63b2aa1d448 10 // 2 Plug ETH connector into your network (needs DHCP to get IP address)
StefR 0:d63b2aa1d448 11 // 3 Power up MBED using USB cable
StefR 0:d63b2aa1d448 12 // 4 Install MBED serial driver (http://mbed.org/handbook/SerialPC)
StefR 0:d63b2aa1d448 13 // 5 Copy compiled .bin to your MBED (make sure target device selected in the compiler is correct)
StefR 0:d63b2aa1d448 14 // 6 Open terminal on the mbed serial port
StefR 0:d63b2aa1d448 15 // 7 Push MBED reset button
StefR 0:d63b2aa1d448 16 // 8 Terminal will display info message with mac address, followed by IP address (if connection succeeds)
StefR 0:d63b2aa1d448 17 // 9 Open browser and enter the following URL, inserting your MBED's IP address:
StefR 0:d63b2aa1d448 18 // http://10.0.0.321/rpc/myrpc1/blink,10 (instead of 10.0.0.321 use MBED IP address from terminal)
StefR 0:d63b2aa1d448 19 // 10 MBED will blink the LED 10 times
StefR 0:d63b2aa1d448 20 //
StefR 0:d63b2aa1d448 21 // Notes: there are still some bugs in HTTPServer code.
StefR 0:d63b2aa1d448 22 // To help fight some of them, copy a valid favicon.ico (a 16x16 icon) file to MBED.
StefR 0:d63b2aa1d448 23 //
StefR 0:d63b2aa1d448 24
StefR 0:d63b2aa1d448 25 #include "mbed.h"
StefR 0:d63b2aa1d448 26 #include "HTTPServer.h"
StefR 0:d63b2aa1d448 27 #include "HTTPRPC.h"
StefR 0:d63b2aa1d448 28 #include "HTTPFS.h"
StefR 0:d63b2aa1d448 29 #include "myrpc.h"
StefR 0:d63b2aa1d448 30 #include "main.h"
StefR 0:d63b2aa1d448 31 #include "TextLCD.h"
StefR 0:d63b2aa1d448 32
StefR 0:d63b2aa1d448 33 DigitalIn knop(p14,"p14");
StefR 0:d63b2aa1d448 34 DigitalOut bel(p21, "p21");
StefR 0:d63b2aa1d448 35 DigitalOut led1(LED1, "led1");
StefR 0:d63b2aa1d448 36 DigitalOut led2(LED2, "led2");
StefR 0:d63b2aa1d448 37 DigitalOut led3(LED3, "led3");
StefR 0:d63b2aa1d448 38 DigitalOut led4(LED4, "led4");
StefR 0:d63b2aa1d448 39 LocalFileSystem local("local");
StefR 0:d63b2aa1d448 40 myrpc myrpc1(p21, "myrpc1");
StefR 0:d63b2aa1d448 41
StefR 0:d63b2aa1d448 42 extern Ethernet eth; // eth is defined elsewhere, avoid compiler error.
StefR 0:d63b2aa1d448 43 Serial pc(USBTX, USBRX); // serial poort met pc
StefR 0:d63b2aa1d448 44 TextLCD lcd(p5, p6, p7, p8, p9, p10, p11); // rs, rw, e, d0, d1, d2, d3
StefR 0:d63b2aa1d448 45 Ticker eth_timer; //ethernet timer
StefR 0:d63b2aa1d448 46
StefR 0:d63b2aa1d448 47 void print_tijd() //haal uur en min uit npt protocol
StefR 0:d63b2aa1d448 48 {
StefR 0:d63b2aa1d448 49 print_time(cTime->tm_hour, cTime->tm_min);
StefR 0:d63b2aa1d448 50 }
StefR 0:d63b2aa1d448 51
StefR 0:d63b2aa1d448 52 void print_time(int hour, int minute)
StefR 0:d63b2aa1d448 53 //timezonecorrectie en tijd op display schrijven
StefR 0:d63b2aa1d448 54 //en led aan laten gaan op ingestelde tijd of door drukknop
StefR 0:d63b2aa1d448 55 {
StefR 0:d63b2aa1d448 56 /* Variable tmphr is 24 hour time adjusted for timezone */
StefR 0:d63b2aa1d448 57 int tmphr = hour + 13 ;
StefR 0:d63b2aa1d448 58 while (tmphr < 0)
StefR 0:d63b2aa1d448 59 tmphr += 24;
StefR 0:d63b2aa1d448 60 tmphr %= 24;
StefR 0:d63b2aa1d448 61
StefR 0:d63b2aa1d448 62 int tmpmin = minute;
StefR 0:d63b2aa1d448 63
StefR 0:d63b2aa1d448 64 //24 uren klok
StefR 0:d63b2aa1d448 65 int printhr = tmphr % 24;
StefR 0:d63b2aa1d448 66 if (printhr == 0)
StefR 0:d63b2aa1d448 67 printhr += 24;
StefR 0:d63b2aa1d448 68 lcd.locate(0,1);
StefR 0:d63b2aa1d448 69 lcd.printf("%2d:%02d ", printhr, minute);
StefR 0:d63b2aa1d448 70 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.
StefR 0:d63b2aa1d448 71 bel.write(1);
StefR 0:d63b2aa1d448 72 wait(10); //hoelang het alarm moet afgaan
StefR 0:d63b2aa1d448 73 bel.write(0);
StefR 0:d63b2aa1d448 74 }
StefR 0:d63b2aa1d448 75 }
StefR 0:d63b2aa1d448 76
StefR 0:d63b2aa1d448 77 //#define LED_ETH_LINK(val) do { led4=val; } while (0)
StefR 0:d63b2aa1d448 78 void eth_link_status() { //internet deffinieren
StefR 0:d63b2aa1d448 79 static bool first = true; // eerste keer ip opvragen
StefR 0:d63b2aa1d448 80 static int eth_link = -1; // hebben we verbinding met de switch
StefR 0:d63b2aa1d448 81 int new_link = eth.link();
StefR 0:d63b2aa1d448 82 if (eth_link != new_link) {
StefR 0:d63b2aa1d448 83 if (new_link) {
StefR 0:d63b2aa1d448 84 // From http://mbed.org/forum/post/909/
StefR 0:d63b2aa1d448 85 NetServer *net = NetServer::get();
StefR 0:d63b2aa1d448 86 struct ip_addr ip = net->getIPAddr();
StefR 0:d63b2aa1d448 87 // struct ip_addr gw = net->getGateway();
StefR 0:d63b2aa1d448 88 // struct ip_addr nm = net->getNetmask();
StefR 0:d63b2aa1d448 89 // struct ip_addr dns = net->getDNS1();
StefR 0:d63b2aa1d448 90 lcd.locate(0,0);
StefR 0:d63b2aa1d448 91 lcd.printf("%hhu.%hhu.%hhu.%hhu\n", (ip.addr)&0xFF, (ip.addr>>8)&0xFF, (ip.addr>>16)&0xFF, (ip.addr>>24)&0xFF);
StefR 0:d63b2aa1d448 92 }
StefR 0:d63b2aa1d448 93 else {
StefR 0:d63b2aa1d448 94 lcd.locate(0,0);
StefR 0:d63b2aa1d448 95 lcd.printf("<link down> \n");
StefR 0:d63b2aa1d448 96 }
StefR 0:d63b2aa1d448 97 //LED_ETH_LINK(new_link);
StefR 0:d63b2aa1d448 98 eth_link = new_link;
StefR 0:d63b2aa1d448 99 }
StefR 0:d63b2aa1d448 100 }
StefR 0:d63b2aa1d448 101
StefR 0:d63b2aa1d448 102 int main(void)
StefR 0:d63b2aa1d448 103 { //Alle led aan maken dat je kan zijn als het ergens vast zit of niet vind.
StefR 0:d63b2aa1d448 104 led1=1;
StefR 0:d63b2aa1d448 105 led2=1;
StefR 0:d63b2aa1d448 106 led3=1;
StefR 0:d63b2aa1d448 107 led4=1;
StefR 0:d63b2aa1d448 108 pc.baud(9600);
StefR 0:d63b2aa1d448 109 // om internet server te maken
StefR 0:d63b2aa1d448 110 char *hostname = "mbed";
StefR 0:d63b2aa1d448 111 HTTPServer http(hostname); // Use DHCP
StefR 0:d63b2aa1d448 112 http.timeout(60);
StefR 0:d63b2aa1d448 113
StefR 0:d63b2aa1d448 114 // dit is nodig om via het internet het rpc protocol uit te voeren
StefR 0:d63b2aa1d448 115 Base::add_rpc_class<AnalogIn>();
StefR 0:d63b2aa1d448 116 Base::add_rpc_class<AnalogOut>();
StefR 0:d63b2aa1d448 117 Base::add_rpc_class<DigitalIn>();
StefR 0:d63b2aa1d448 118 Base::add_rpc_class<DigitalOut>();
StefR 0:d63b2aa1d448 119 Base::add_rpc_class<PwmOut>();
StefR 0:d63b2aa1d448 120 Base::add_rpc_class<Timer>();
StefR 0:d63b2aa1d448 121 Base::add_rpc_class<SPI>();
StefR 0:d63b2aa1d448 122 Base::add_rpc_class<BusOut>();
StefR 0:d63b2aa1d448 123 Base::add_rpc_class<BusIn>();
StefR 0:d63b2aa1d448 124 Base::add_rpc_class<myrpc>();
StefR 0:d63b2aa1d448 125 led1=0;
StefR 0:d63b2aa1d448 126
StefR 0:d63b2aa1d448 127 http.addHandler(new HTTPRPC());
StefR 0:d63b2aa1d448 128 led2=0;
StefR 0:d63b2aa1d448 129
StefR 0:d63b2aa1d448 130 http.addHandler(new HTTPFileSystemHandler("/", "/local/"));
StefR 0:d63b2aa1d448 131 led3=0;
StefR 0:d63b2aa1d448 132
StefR 0:d63b2aa1d448 133 // FIXME: BUG:: If eth is not plugged, http.bind() hangs for a while!
StefR 0:d63b2aa1d448 134 http.bind();
StefR 0:d63b2aa1d448 135 led4 = 0;
StefR 0:d63b2aa1d448 136
StefR 0:d63b2aa1d448 137 //pc.printf("\r"); // Add linefeed for stupid Hyperterminal
StefR 0:d63b2aa1d448 138 eth_timer.attach(&eth_link_status, 0.1);
StefR 0:d63b2aa1d448 139 //NTP uitlezen vanuit ntp server
StefR 0:d63b2aa1d448 140 Host server(IpAddr(), 123, "0;uk.pool.ntp.org");
StefR 0:d63b2aa1d448 141 ntp.setTime(server);
StefR 0:d63b2aa1d448 142 pc.printf("\nstart");
StefR 0:d63b2aa1d448 143 while(1)
StefR 0:d63b2aa1d448 144 {
StefR 0:d63b2aa1d448 145 http.poll(); // Have to call this method at least every 250ms to let the http server run.
StefR 0:d63b2aa1d448 146 wait(0.100);
StefR 0:d63b2aa1d448 147
StefR 0:d63b2aa1d448 148 /* Temporary variables for control loop */
StefR 0:d63b2aa1d448 149 time_t rtc_time = time(NULL);
StefR 0:d63b2aa1d448 150 int minute = -1;
StefR 0:d63b2aa1d448 151
StefR 0:d63b2aa1d448 152 /* Update current time */
StefR 0:d63b2aa1d448 153 rtc_time = time(NULL);
StefR 0:d63b2aa1d448 154
StefR 0:d63b2aa1d448 155 cTime = localtime(&rtc_time);
StefR 0:d63b2aa1d448 156
StefR 0:d63b2aa1d448 157 /* Only redraw the lcd display if anything changes */
StefR 0:d63b2aa1d448 158 if (cTime->tm_min != minute)
StefR 0:d63b2aa1d448 159 {
StefR 0:d63b2aa1d448 160 minute = cTime->tm_min;
StefR 0:d63b2aa1d448 161
StefR 0:d63b2aa1d448 162 print_tijd();
StefR 0:d63b2aa1d448 163 /* Update time from NTP server if it's midnight UTC */
StefR 0:d63b2aa1d448 164 //changed min to 1
StefR 0:d63b2aa1d448 165 if ((cTime->tm_min == 1) && (cTime->tm_hour == 0))
StefR 0:d63b2aa1d448 166 {
StefR 0:d63b2aa1d448 167 Host server(IpAddr(), 123, "0.north-america.pool.ntp.org");
StefR 0:d63b2aa1d448 168
StefR 0:d63b2aa1d448 169 ntp.setTime(server);
StefR 0:d63b2aa1d448 170 }
StefR 0:d63b2aa1d448 171 }
StefR 0:d63b2aa1d448 172 } //return 0;
StefR 0:d63b2aa1d448 173 }
StefR 0:d63b2aa1d448 174
StefR 0:d63b2aa1d448 175 //END