Web Server and send X10 RF orders to X10 RF transceivers Switch On/Off Appliance No weather Rx at the moment...

Dependencies:   EthernetNetIf mbed HTTPServer

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers main.cpp Source File

main.cpp

00001 #include "mbed.h"
00002 #include "EthernetNetIf.h"
00003 //#include "HTTPClient.h"
00004 #include "HTTPServer.h"
00005 #include "RPCFunction.h"
00006 #include "x10rf.h"
00007 
00008 EthernetNetIf eth(
00009     IpAddr(192,168,1,25), //IP Address
00010     IpAddr(255,255,255,0), //Network Mask
00011     IpAddr(192,168,1,1), //Gateway
00012     IpAddr(192,168,1,1)  //DNS
00013 );
00014 //HTTPClient http;
00015 HTTPServer svr;
00016 
00017 DigitalOut led1(LED1, "led1");
00018 DigitalOut led2(LED2, "led2");
00019 DigitalOut led3(LED3, "led3");
00020 DigitalOut led4(LED4, "led4");
00021 
00022 LocalFileSystem fs("webfs");
00023 
00024 //Create a function of the required format
00025 void rpcX10rf(char * input, char * output);
00026 //Attach it to an RPC object
00027 RPCFunction rpc_foo(&rpcX10rf, "rpcX10rf");
00028 
00029 void rpcX10rf(char * input, char * output) {
00030 char houseCode;
00031 short int numberCode, action;
00032     printf("%s\r\n", input);
00033     sscanf(input, "  %c,%d,%d", &houseCode, &numberCode, &action);
00034     printf("%c, %d,%d\r\n", houseCode, numberCode, action);
00035     // calls the X10RFLib
00036     SendX10rf( houseCode, numberCode, action);
00037     
00038     //nothing to send back - sprintf(output, "%i, %i", x, y );
00039 }
00040 
00041 int main() {
00042     Base::add_rpc_class<DigitalOut>();
00043     EthernetErr ethErr = eth.setup();
00044     if (ethErr) {
00045         printf("Error %d in setup.\n", ethErr);
00046         return -1;
00047     }
00048     printf("\r\nSetup OK\r\n");
00049 
00050     FSHandler::mount("/webfs", "/files"); //Mount /webfs path on /files web path
00051     FSHandler::mount("/webfs", "/"); //Mount /webfs path on web root path
00052 
00053     //svr.addHandler<SimpleHandler>("/hello");
00054     svr.addHandler<RPCHandler>("/rpc");
00055     svr.addHandler<FSHandler>("/files");
00056     svr.addHandler<FSHandler>("/"); //Default handler
00057     //Example : Access to mbed.htm : http://a.b.c.d/mbed.htm or http://a.b.c.d/files/mbed.htm
00058     svr.bind(80);
00059 
00060     printf("Listening...\n");
00061 
00062     Timer tm;
00063     tm.start();
00064     //Listen indefinitely
00065     while (1) {
00066         Net::poll();
00067         if (tm.read()>.5) {
00068             led1=!led1; //Show that we are alive
00069             tm.start();
00070         }
00071     }
00072 }