X10 Server - IOT device to leverage a collection of old X10 devices for home automation and lighting control.
Dependencies: IniManager mbed HTTPClient SWUpdate mbed-rtos Watchdog X10 SW_HTTPServer SW_String EthernetInterface TimeInterface SSDP
X10 Server
See the X10 Server Nodebook page
main.cpp@5:6244e237def1, 2018-09-01 (annotated)
- Committer:
- WiredHome
- Date:
- Sat Sep 01 01:05:57 2018 +0000
- Revision:
- 5:6244e237def1
- Parent:
- 4:5da66fab599c
- Child:
- 6:80a97f156128
Enhanced the demo to optionally include the RA8875 display library.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
WiredHome | 0:de1dfa2ab813 | 1 | // |
WiredHome | 0:de1dfa2ab813 | 2 | // A simple SSDP example |
WiredHome | 0:de1dfa2ab813 | 3 | // |
WiredHome | 0:de1dfa2ab813 | 4 | #include "mbed.h" // ver 120; mbed-rtos ver 111 |
WiredHome | 5:6244e237def1 | 5 | #include "EthernetInterface.h" // ver 56 |
WiredHome | 5:6244e237def1 | 6 | #include "SW_HTTPServer.h" // ver 57 |
WiredHome | 5:6244e237def1 | 7 | #include "TimeInterface.h" // ver 24 |
WiredHome | 0:de1dfa2ab813 | 8 | #include "Watchdog.h" // ver 6 |
WiredHome | 0:de1dfa2ab813 | 9 | #include "SW_String.h" // ver 1 |
WiredHome | 5:6244e237def1 | 10 | #include "SSDP.h" // ver 4 |
WiredHome | 0:de1dfa2ab813 | 11 | #include "WebPages.h" // Private handler for web queries |
WiredHome | 0:de1dfa2ab813 | 12 | |
WiredHome | 5:6244e237def1 | 13 | // Comment out the next line if you do not have an RA8875 Display |
WiredHome | 5:6244e237def1 | 14 | #include "RA8875.h" // ver 154 |
WiredHome | 5:6244e237def1 | 15 | |
WiredHome | 0:de1dfa2ab813 | 16 | RawSerial pc(USBTX, USBRX); |
WiredHome | 0:de1dfa2ab813 | 17 | EthernetInterface eth; |
WiredHome | 0:de1dfa2ab813 | 18 | TimeInterface ntp(ð); |
WiredHome | 0:de1dfa2ab813 | 19 | Watchdog wd; |
WiredHome | 0:de1dfa2ab813 | 20 | time_t lastboottime; |
WiredHome | 0:de1dfa2ab813 | 21 | |
WiredHome | 5:6244e237def1 | 22 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 23 | #define LCD_W 480 |
WiredHome | 5:6244e237def1 | 24 | #define LCD_H 272 |
WiredHome | 5:6244e237def1 | 25 | #define LCD_C 16 |
WiredHome | 5:6244e237def1 | 26 | RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, I2C:{SDA,SCL,/IRQ}, name |
WiredHome | 5:6244e237def1 | 27 | #endif |
WiredHome | 4:5da66fab599c | 28 | |
WiredHome | 0:de1dfa2ab813 | 29 | LocalFileSystem local("local"); // some place to hold settings and maybe the static web pages |
WiredHome | 0:de1dfa2ab813 | 30 | const char * Server_Root = "/local"; |
WiredHome | 0:de1dfa2ab813 | 31 | |
WiredHome | 0:de1dfa2ab813 | 32 | // public for the WebPages handler to see |
WiredHome | 0:de1dfa2ab813 | 33 | // |
WiredHome | 0:de1dfa2ab813 | 34 | const char * BUILD_DATE = __DATE__ " " __TIME__; |
WiredHome | 0:de1dfa2ab813 | 35 | const char * PROG_NAME = "SSDP Server"; |
WiredHome | 4:5da66fab599c | 36 | char * My_Name = "MBED SSDP Node"; |
WiredHome | 0:de1dfa2ab813 | 37 | const char * My_SerialNum = "0000001"; |
WiredHome | 4:5da66fab599c | 38 | int Server_Port = 80; |
WiredHome | 0:de1dfa2ab813 | 39 | // end public information |
WiredHome | 0:de1dfa2ab813 | 40 | |
WiredHome | 5:6244e237def1 | 41 | int main() |
WiredHome | 4:5da66fab599c | 42 | { |
WiredHome | 0:de1dfa2ab813 | 43 | pc.baud(460800); |
WiredHome | 0:de1dfa2ab813 | 44 | pc.printf("\r\n%s Build %s\r\n", PROG_NAME, BUILD_DATE); |
WiredHome | 4:5da66fab599c | 45 | lastboottime = ntp.timelocal(); |
WiredHome | 0:de1dfa2ab813 | 46 | if (wd.WatchdogCausedReset()) { |
WiredHome | 0:de1dfa2ab813 | 47 | pc.printf("**** Watchdog Event caused reset at %s ****\r\n", ntp.ctime(&lastboottime)); |
WiredHome | 0:de1dfa2ab813 | 48 | } |
WiredHome | 0:de1dfa2ab813 | 49 | wd.Configure(45); // very generous, but this is a network appliance, so a bit less deterministic. |
WiredHome | 0:de1dfa2ab813 | 50 | |
WiredHome | 5:6244e237def1 | 51 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 52 | lcd.init(LCD_W,LCD_H,LCD_C, 60); |
WiredHome | 5:6244e237def1 | 53 | lcd.Backlight_u8(30); |
WiredHome | 5:6244e237def1 | 54 | lcd.foreground(Red); |
WiredHome | 5:6244e237def1 | 55 | lcd.printf("Initializing network interface...\r\n"); |
WiredHome | 5:6244e237def1 | 56 | #endif |
WiredHome | 5:6244e237def1 | 57 | pc.printf("Initializing network interface...\r\n"); |
WiredHome | 5:6244e237def1 | 58 | if (0 == eth.init()) { |
WiredHome | 5:6244e237def1 | 59 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 60 | lcd.printf(" Name: %s\r\n", My_Name); |
WiredHome | 5:6244e237def1 | 61 | #endif |
WiredHome | 5:6244e237def1 | 62 | pc.printf("Name: %s\r\n", My_Name); |
WiredHome | 5:6244e237def1 | 63 | eth.setName(My_Name); |
WiredHome | 4:5da66fab599c | 64 | |
WiredHome | 5:6244e237def1 | 65 | do { |
WiredHome | 5:6244e237def1 | 66 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 67 | lcd.printf(" Connecting to network...\r\n"); |
WiredHome | 5:6244e237def1 | 68 | #endif |
WiredHome | 5:6244e237def1 | 69 | pc.printf("Connecting to network...\r\n"); |
WiredHome | 5:6244e237def1 | 70 | if (0 == eth.connect()) { |
WiredHome | 5:6244e237def1 | 71 | int speed = eth.get_connection_speed(); |
WiredHome | 5:6244e237def1 | 72 | |
WiredHome | 5:6244e237def1 | 73 | pc.printf("Connected at %d Mb/s\r\n", speed); |
WiredHome | 5:6244e237def1 | 74 | pc.printf("IP: %15s\r\n", eth.getIPAddress()); |
WiredHome | 5:6244e237def1 | 75 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 76 | lcd.printf(" Connected at %d Mb/s\r\n", speed); |
WiredHome | 5:6244e237def1 | 77 | lcd.printf(" IP: %15s\r\n", eth.getIPAddress()); |
WiredHome | 5:6244e237def1 | 78 | #endif |
WiredHome | 5:6244e237def1 | 79 | |
WiredHome | 5:6244e237def1 | 80 | HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc); |
WiredHome | 5:6244e237def1 | 81 | svr.RegisterHandler("/", RootPage); |
WiredHome | 5:6244e237def1 | 82 | svr.RegisterHandler("/setup.xml", Setup_xml); |
WiredHome | 5:6244e237def1 | 83 | SSDP ssdp(My_Name, eth.getMACAddress(), eth.getIPAddress(), Server_Port); |
WiredHome | 4:5da66fab599c | 84 | |
WiredHome | 5:6244e237def1 | 85 | ntp.set_dst("3/11,2:00","11/4,2:00"); // mm/dd,hh:mm |
WiredHome | 5:6244e237def1 | 86 | ntp.set_tzo_min(-360); |
WiredHome | 5:6244e237def1 | 87 | int res = ntp.setTime("pool.ntp.org"); |
WiredHome | 5:6244e237def1 | 88 | |
WiredHome | 5:6244e237def1 | 89 | while (eth.is_connected()) { |
WiredHome | 5:6244e237def1 | 90 | wd.Service(); |
WiredHome | 5:6244e237def1 | 91 | static time_t tLast; |
WiredHome | 5:6244e237def1 | 92 | time_t tNow = ntp.timelocal(); |
WiredHome | 5:6244e237def1 | 93 | if (tNow != tLast) { |
WiredHome | 5:6244e237def1 | 94 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 95 | lcd.SetTextFontSize(2); |
WiredHome | 5:6244e237def1 | 96 | lcd.SetTextCursor(20,120); |
WiredHome | 5:6244e237def1 | 97 | lcd.printf("%s", ntp.ctime(&tNow)); |
WiredHome | 5:6244e237def1 | 98 | #endif |
WiredHome | 5:6244e237def1 | 99 | pc.printf("time is %s\r\n", ntp.ctime(&tNow)); |
WiredHome | 5:6244e237def1 | 100 | tLast = tNow; |
WiredHome | 5:6244e237def1 | 101 | } |
WiredHome | 5:6244e237def1 | 102 | // Any other work can happen here |
WiredHome | 5:6244e237def1 | 103 | Thread::yield(); |
WiredHome | 5:6244e237def1 | 104 | } |
WiredHome | 5:6244e237def1 | 105 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 106 | lcd.cls(); |
WiredHome | 5:6244e237def1 | 107 | lcd.printf("lost network.\r\n"); |
WiredHome | 5:6244e237def1 | 108 | #endif |
WiredHome | 5:6244e237def1 | 109 | pc.printf("lost connection.\r\n"); |
WiredHome | 5:6244e237def1 | 110 | eth.disconnect(); |
WiredHome | 5:6244e237def1 | 111 | } else { |
WiredHome | 5:6244e237def1 | 112 | #ifdef RA8875_H |
WiredHome | 5:6244e237def1 | 113 | lcd.printf(" ... failed to connect\r\n"); |
WiredHome | 5:6244e237def1 | 114 | #endif |
WiredHome | 5:6244e237def1 | 115 | pc.printf(" ... failed to connect.\r\n"); |
WiredHome | 5:6244e237def1 | 116 | } |
WiredHome | 5:6244e237def1 | 117 | } while (1); |
WiredHome | 0:de1dfa2ab813 | 118 | } |
WiredHome | 0:de1dfa2ab813 | 119 | } |