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

Revision:
5:6244e237def1
Parent:
4:5da66fab599c
Child:
6:80a97f156128
--- a/main.cpp	Fri Aug 31 22:51:15 2018 +0000
+++ b/main.cpp	Sat Sep 01 01:05:57 2018 +0000
@@ -2,25 +2,30 @@
 // A simple SSDP example
 //
 #include "mbed.h"               // ver 120; mbed-rtos ver 111
-#include "EthernetInterface.h"  // ver 55
-#include "SW_HTTPServer.h"      // ver 50
-#include "TimeInterface.h"      // ver 23
+#include "EthernetInterface.h"  // ver 56
+#include "SW_HTTPServer.h"      // ver 57
+#include "TimeInterface.h"      // ver 24
 #include "Watchdog.h"           // ver 6
 #include "SW_String.h"          // ver 1
-#include "SSDP.h"               // ver 0
-
+#include "SSDP.h"               // ver 4
 #include "WebPages.h"           // Private handler for web queries
 
+// Comment out the next line if you do not have an RA8875 Display
+#include "RA8875.h"             // ver 154
+
 RawSerial pc(USBTX, USBRX);
-
 EthernetInterface eth;
 TimeInterface ntp(&eth);
 Watchdog wd;
 time_t lastboottime;
 
-PwmOut signOfLife(LED4);        // LED sign of life. 100% WD reset, 20% restart, sine-wave: running
+#ifdef RA8875_H
+#define LCD_W 480
+#define LCD_H 272
+#define LCD_C 16
+RA8875 lcd(p5, p6, p7, p12, NC, p9,p10,p13, "tft"); // SPI:{MOSI,MISO,SCK,/ChipSelect,/reset}, I2C:{SDA,SCL,/IRQ}, name
+#endif
 
-//FlashFileSystem flash("flash");             // static files here for reliability and speed
 LocalFileSystem local("local");             // some place to hold settings and maybe the static web pages
 const char * Server_Root = "/local";
 
@@ -33,26 +38,8 @@
 int Server_Port = 80;
 // end public information
 
-
-
-
-/// ShowSignOfLife
-///
-/// Pulse an LED to indicate a sign of life of the program.
-/// This also has some moderate entertainment value.
-///
-void ShowSignOfLife(int degreeIncrement = 1)
+int main()
 {
-#define PI 3.14159265359
-    static int degrees = 0;
-    float v;
-
-    degrees += degreeIncrement;
-    v = sin(degrees * PI / 180)/2 + 0.5;
-    signOfLife = v;     // a little dimmer
-}
-
-int main() {
     pc.baud(460800);
     pc.printf("\r\n%s Build %s\r\n", PROG_NAME, BUILD_DATE);
     lastboottime = ntp.timelocal();
@@ -61,30 +48,72 @@
     }
     wd.Configure(45);       // very generous, but this is a network appliance, so a bit less deterministic.
 
-    eth.init(); //Use DHCP
-    eth.connect();
-    printf("IP: %s\n", eth.getIPAddress());
-    //Thread thr(SSDPListener, NULL, osPriorityLow);
-    HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc);
-    svr.RegisterHandler("/", RootPage);
-    svr.RegisterHandler("/setup.xml", Setup_xml);
-    SSDP ssdp(My_Name, eth.getMACAddress(), eth.getIPAddress(), Server_Port);
+#ifdef RA8875_H
+    lcd.init(LCD_W,LCD_H,LCD_C, 60);
+    lcd.Backlight_u8(30);
+    lcd.foreground(Red);
+    lcd.printf("Initializing network interface...\r\n");
+#endif
+    pc.printf("Initializing network interface...\r\n");
+    if (0 == eth.init()) {
+#ifdef RA8875_H
+        lcd.printf("  Name: %s\r\n", My_Name);
+#endif
+        pc.printf("Name: %s\r\n", My_Name);
+        eth.setName(My_Name);
 
-    ntp.set_dst("3/11,2:00","11/4,2:00");   // mm/dd,hh:mm
-    ntp.set_tzo_min(-360);
-    int res = ntp.setTime("pool.ntp.org");
+        do {
+#ifdef RA8875_H
+            lcd.printf("  Connecting to network...\r\n");
+#endif
+            pc.printf("Connecting to network...\r\n");
+            if (0 == eth.connect()) {
+                int speed = eth.get_connection_speed();
+
+                pc.printf("Connected at %d Mb/s\r\n", speed);
+                pc.printf("IP: %15s\r\n", eth.getIPAddress());
+#ifdef RA8875_H
+                lcd.printf("  Connected at %d Mb/s\r\n", speed);
+                lcd.printf("  IP: %15s\r\n", eth.getIPAddress());
+#endif
+
+                HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc);
+                svr.RegisterHandler("/", RootPage);
+                svr.RegisterHandler("/setup.xml", Setup_xml);
+                SSDP ssdp(My_Name, eth.getMACAddress(), eth.getIPAddress(), Server_Port);
 
-    while (1) {
-        wd.Service();
-        svr.Poll();         // non-blocking, but also not deterministic
-        Thread::yield();
-        ShowSignOfLife(10);
-        static time_t tLast;
-        time_t tNow = ntp.timelocal();
-        if (tNow != tLast) {
-            printf("time is %s\r\n", ntp.ctime(&tNow));
-            tLast = tNow;
-        }
-        //printf("Mem: %5d, %5d %5d\n", Thread::used_stack(), Thread::max_stack(), Thread::stack_size());
+                ntp.set_dst("3/11,2:00","11/4,2:00");   // mm/dd,hh:mm
+                ntp.set_tzo_min(-360);
+                int res = ntp.setTime("pool.ntp.org");
+
+                while (eth.is_connected()) {
+                    wd.Service();
+                    static time_t tLast;
+                    time_t tNow = ntp.timelocal();
+                    if (tNow != tLast) {
+#ifdef RA8875_H
+                        lcd.SetTextFontSize(2);
+                        lcd.SetTextCursor(20,120);
+                        lcd.printf("%s", ntp.ctime(&tNow));
+#endif
+                        pc.printf("time is %s\r\n", ntp.ctime(&tNow));
+                        tLast = tNow;
+                    }
+                    // Any other work can happen here
+                    Thread::yield();
+                }
+#ifdef RA8875_H
+                lcd.cls();
+                lcd.printf("lost network.\r\n");
+#endif
+                pc.printf("lost connection.\r\n");
+                eth.disconnect();
+            } else {
+#ifdef RA8875_H
+                lcd.printf("  ... failed to connect\r\n");
+#endif
+                pc.printf("  ... failed to connect.\r\n");
+            }
+        } while (1);
     }
 }