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

Committer:
WiredHome
Date:
2018-09-01
Revision:
5:6244e237def1
Parent:
4:5da66fab599c
Child:
6:80a97f156128

File content as of revision 5:6244e237def1:

//
// A simple SSDP example
//
#include "mbed.h"               // ver 120; mbed-rtos ver 111
#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 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;

#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

LocalFileSystem local("local");             // some place to hold settings and maybe the static web pages
const char * Server_Root = "/local";

// public for the WebPages handler to see
//
const char * BUILD_DATE = __DATE__ " " __TIME__;
const char * PROG_NAME = "SSDP Server";
char * My_Name = "MBED SSDP Node";
const char * My_SerialNum = "0000001";
int Server_Port = 80;
// end public information

int main()
{
    pc.baud(460800);
    pc.printf("\r\n%s Build %s\r\n", PROG_NAME, BUILD_DATE);
    lastboottime = ntp.timelocal();
    if (wd.WatchdogCausedReset()) {
        pc.printf("**** Watchdog Event caused reset at %s ****\r\n", ntp.ctime(&lastboottime));
    }
    wd.Configure(45);       // very generous, but this is a network appliance, so a bit less deterministic.

#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);

        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);

                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);
    }
}