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

Committer:
WiredHome
Date:
Wed Feb 27 18:24:32 2019 +0000
Revision:
7:16129d213e6a
Parent:
6:80a97f156128
Child:
8:a45fe77efcc5
An improved X10 Server, now with SSDP discovery and with an integrated Web Server test interface.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
WiredHome 0:de1dfa2ab813 1 //
WiredHome 7:16129d213e6a 2 // X10 Server - Version 2
WiredHome 0:de1dfa2ab813 3 //
WiredHome 7:16129d213e6a 4 /// If you've wired the CM17a to the same mbed pins, you should be able to simply
WiredHome 7:16129d213e6a 5 /// install this application, and then install a properly configured ini file
WiredHome 7:16129d213e6a 6 /// on the local file system.
WiredHome 7:16129d213e6a 7 ///
WiredHome 7:16129d213e6a 8
WiredHome 0:de1dfa2ab813 9 #include "mbed.h" // ver 120; mbed-rtos ver 111
WiredHome 7:16129d213e6a 10 #include "EthernetInterface.h" // ver 56 - https://os.mbed.com/users/WiredHome/code/EthernetInterface/
WiredHome 7:16129d213e6a 11 #include "SW_HTTPServer.h" // ver 58
WiredHome 7:16129d213e6a 12 #include "TimeInterface.h" // ver 25
WiredHome 7:16129d213e6a 13 #include "SWUpdate.h" // ver 26
WiredHome 7:16129d213e6a 14 #include "SW_String.h" // ver 2
WiredHome 7:16129d213e6a 15 #include "SSDP.h" // ver 7
WiredHome 0:de1dfa2ab813 16 #include "Watchdog.h" // ver 6
WiredHome 7:16129d213e6a 17 #include "IniManager.h" // v20
WiredHome 7:16129d213e6a 18 #include "X10.h" // v1
WiredHome 7:16129d213e6a 19 #include "X10Server.h" // v1
WiredHome 7:16129d213e6a 20
WiredHome 0:de1dfa2ab813 21 #include "WebPages.h" // Private handler for web queries
WiredHome 7:16129d213e6a 22 #include "SignOfLife.h" // LED effects
WiredHome 0:de1dfa2ab813 23
WiredHome 7:16129d213e6a 24 extern "C" void mbed_reset();
WiredHome 7:16129d213e6a 25
WiredHome 7:16129d213e6a 26 X10Interface cm17a(p5,p12); // cm17a.ParseCommand(buf);
WiredHome 5:6244e237def1 27
WiredHome 0:de1dfa2ab813 28 RawSerial pc(USBTX, USBRX);
WiredHome 0:de1dfa2ab813 29 EthernetInterface eth;
WiredHome 7:16129d213e6a 30 DigitalOut linkup(p26);
WiredHome 7:16129d213e6a 31 DigitalOut linkdata(p25);
WiredHome 7:16129d213e6a 32
WiredHome 0:de1dfa2ab813 33 Watchdog wd;
WiredHome 7:16129d213e6a 34 bool WDEventOccurred = false;
WiredHome 0:de1dfa2ab813 35
WiredHome 7:16129d213e6a 36 TimeInterface ntp(&eth);
WiredHome 7:16129d213e6a 37 time_t ntpSyncd; // time of the last sync
WiredHome 7:16129d213e6a 38 bool ntpUpdateCheck = true; // scheduled check on startup
WiredHome 4:5da66fab599c 39
WiredHome 7:16129d213e6a 40 time_t lastboottime;
WiredHome 7:16129d213e6a 41 bool swUpdateCheck = true; // scheduled check on startup
WiredHome 7:16129d213e6a 42
WiredHome 7:16129d213e6a 43 INI ini;
WiredHome 0:de1dfa2ab813 44 LocalFileSystem local("local"); // some place to hold settings and maybe the static web pages
WiredHome 0:de1dfa2ab813 45 const char * Server_Root = "/local";
WiredHome 0:de1dfa2ab813 46
WiredHome 0:de1dfa2ab813 47 // public for the WebPages handler to see
WiredHome 0:de1dfa2ab813 48 //
WiredHome 0:de1dfa2ab813 49 const char * BUILD_DATE = __DATE__ " " __TIME__;
WiredHome 7:16129d213e6a 50 const char * PROG_NAME = "X10-Server-2";
WiredHome 7:16129d213e6a 51 char * My_Name = "X10-Server-2";
WiredHome 0:de1dfa2ab813 52 const char * My_SerialNum = "0000001";
WiredHome 4:5da66fab599c 53 int Server_Port = 80;
WiredHome 0:de1dfa2ab813 54 // end public information
WiredHome 0:de1dfa2ab813 55
WiredHome 7:16129d213e6a 56
WiredHome 7:16129d213e6a 57 const char * iniFile = "/local/X10svr.ini";
WiredHome 7:16129d213e6a 58
WiredHome 7:16129d213e6a 59
WiredHome 7:16129d213e6a 60
WiredHome 7:16129d213e6a 61 /// This function uses the settings in the .ini file to check for
WiredHome 7:16129d213e6a 62 /// and install, a software update that might be available.
WiredHome 7:16129d213e6a 63 ///
WiredHome 7:16129d213e6a 64 void SoftwareUpdateCheck(bool force)
WiredHome 7:16129d213e6a 65 {
WiredHome 7:16129d213e6a 66 char url[100], name[10];
WiredHome 7:16129d213e6a 67 static time_t tstart = ntp.time();
WiredHome 7:16129d213e6a 68 time_t tNow = ntp.time();
WiredHome 7:16129d213e6a 69
WiredHome 7:16129d213e6a 70 //eth_mutex.lock();
WiredHome 7:16129d213e6a 71 #define ONE_DAY (24 * 60 * 60)
WiredHome 7:16129d213e6a 72 if (force || (tNow - tstart) > ONE_DAY) {
WiredHome 7:16129d213e6a 73 pc.printf("SoftwareUpdateCheck at %s (UTC)\r\n", ntp.ctime(&tNow));
WiredHome 7:16129d213e6a 74 tstart = tNow;
WiredHome 7:16129d213e6a 75 swUpdateCheck = true;
WiredHome 7:16129d213e6a 76 if (INI::INI_SUCCESS == ini.ReadString("SWUpdate", "url", url, sizeof(url))
WiredHome 7:16129d213e6a 77 && INI::INI_SUCCESS == ini.ReadString("SWUpdate", "name", name, sizeof(name))) {
WiredHome 7:16129d213e6a 78 linkdata = true;
WiredHome 7:16129d213e6a 79 pc.printf(" url: %s\r\n", url);
WiredHome 7:16129d213e6a 80 pc.printf(" name: %s\r\n", name);
WiredHome 7:16129d213e6a 81 SWUpdate_T su = SoftwareUpdate(url, name, DEFER_REBOOT);
WiredHome 7:16129d213e6a 82 if (SWUP_OK == su) {
WiredHome 7:16129d213e6a 83 pc.printf(" update installed, rebooting...\r\n");
WiredHome 7:16129d213e6a 84 Thread::wait(3000);
WiredHome 7:16129d213e6a 85 mbed_reset();
WiredHome 7:16129d213e6a 86 } else if (SWUP_SAME_VER == su) {
WiredHome 7:16129d213e6a 87 pc.printf(" no update available.\r\n");
WiredHome 7:16129d213e6a 88 swUpdateCheck = false;
WiredHome 7:16129d213e6a 89 } else {
WiredHome 7:16129d213e6a 90 pc.printf(" update failed %04X\r\n", su);
WiredHome 7:16129d213e6a 91 Thread::wait(1000);
WiredHome 7:16129d213e6a 92 }
WiredHome 7:16129d213e6a 93 linkdata = false;
WiredHome 7:16129d213e6a 94 } else {
WiredHome 7:16129d213e6a 95 pc.printf(" can't get info from ini file.\r\n");
WiredHome 7:16129d213e6a 96 swUpdateCheck = false;
WiredHome 7:16129d213e6a 97 }
WiredHome 7:16129d213e6a 98 //eth_mutex.unlock();
WiredHome 7:16129d213e6a 99 }
WiredHome 7:16129d213e6a 100 }
WiredHome 7:16129d213e6a 101
WiredHome 7:16129d213e6a 102
WiredHome 7:16129d213e6a 103 /// This function syncs the node to a timeserver, if one
WiredHome 7:16129d213e6a 104 /// is configured in the .ini file.
WiredHome 7:16129d213e6a 105 ///
WiredHome 7:16129d213e6a 106 void SyncToNTPServer(bool force)
WiredHome 7:16129d213e6a 107 {
WiredHome 7:16129d213e6a 108 char url[100];
WiredHome 7:16129d213e6a 109 char tzone[10];
WiredHome 7:16129d213e6a 110 char dstFlag[5]; // off,on,auto
WiredHome 7:16129d213e6a 111 char dstStart[12]; // mm/dd,hh:mm
WiredHome 7:16129d213e6a 112 char dstStop[12]; // mm/dd,hh:mm
WiredHome 7:16129d213e6a 113 static time_t tlast = 0;
WiredHome 7:16129d213e6a 114 time_t tnow = ntp.time();
WiredHome 7:16129d213e6a 115
WiredHome 7:16129d213e6a 116 if (((tnow - tlast) > (60*60*24)) || force) {
WiredHome 7:16129d213e6a 117 printf("SyncToNTPServer\r\n");
WiredHome 7:16129d213e6a 118 if (INI::INI_SUCCESS == ini.ReadString("Clock", "timeserver", url, sizeof(url))) {
WiredHome 7:16129d213e6a 119 ini.ReadString("Clock", "tzoffsetmin", tzone, sizeof(tzone), "0");
WiredHome 7:16129d213e6a 120 ini.ReadString("Clock", "dst", dstFlag, sizeof(dstFlag), "0");
WiredHome 7:16129d213e6a 121 ini.ReadString("Clock", "dststart", dstStart, sizeof(dstStart), "");
WiredHome 7:16129d213e6a 122 ini.ReadString("Clock", "dststop", dstStop, sizeof(dstStop), "");
WiredHome 7:16129d213e6a 123
WiredHome 7:16129d213e6a 124 printf("NTP update time from (%s)\r\n", url);
WiredHome 7:16129d213e6a 125 int32_t tzo_min = atoi(tzone);
WiredHome 7:16129d213e6a 126
WiredHome 7:16129d213e6a 127 if (strcmp(dstFlag,"on") == 0) {
WiredHome 7:16129d213e6a 128 ntp.set_dst(1);
WiredHome 7:16129d213e6a 129 } else if (strcmp(dstFlag, "off") == 0) {
WiredHome 7:16129d213e6a 130 ntp.set_dst(0);
WiredHome 7:16129d213e6a 131 } else /* if (strcmp(dstFlag, "auto") == 0) */ {
WiredHome 7:16129d213e6a 132 ntp.set_dst(dstStart,dstStop);
WiredHome 7:16129d213e6a 133 }
WiredHome 7:16129d213e6a 134 ntp.set_tzo_min(tzo_min);
WiredHome 7:16129d213e6a 135 int res = ntp.setTime(url);
WiredHome 7:16129d213e6a 136 //printf(" NTP (release ethernet)\r\n");
WiredHome 7:16129d213e6a 137 if (res == 0) {
WiredHome 7:16129d213e6a 138 time_t ctTime;
WiredHome 7:16129d213e6a 139 ctTime = ntp.time();
WiredHome 7:16129d213e6a 140 ntpSyncd = ntp.get_timelastset();;
WiredHome 7:16129d213e6a 141 tlast = ctTime;
WiredHome 7:16129d213e6a 142 printf(" Time set to (UTC): %s\r\n", ntp.ctime(&ctTime));
WiredHome 7:16129d213e6a 143 printf(" ntpSyncd: %s\r\n", ntp.ctime(&ntpSyncd));
WiredHome 7:16129d213e6a 144 } else {
WiredHome 7:16129d213e6a 145 ntpSyncd = 0;
WiredHome 7:16129d213e6a 146 printf("Error return from setTime(%s) %d\r\n", url, res);
WiredHome 7:16129d213e6a 147 }
WiredHome 7:16129d213e6a 148 } else {
WiredHome 7:16129d213e6a 149 ntpSyncd = 0;
WiredHome 7:16129d213e6a 150 printf("no time server was set\r\n");
WiredHome 7:16129d213e6a 151 }
WiredHome 7:16129d213e6a 152 }
WiredHome 7:16129d213e6a 153 }
WiredHome 7:16129d213e6a 154
WiredHome 7:16129d213e6a 155
WiredHome 7:16129d213e6a 156 /// This function monitors the USB serial interface for activity
WiredHome 7:16129d213e6a 157 /// from a connected user.
WiredHome 7:16129d213e6a 158 ///
WiredHome 7:16129d213e6a 159 /// It offers a tiny bit of help if an unrecognized command is entered.
WiredHome 7:16129d213e6a 160 ///
WiredHome 7:16129d213e6a 161 void CheckConsoleInput(void)
WiredHome 7:16129d213e6a 162 {
WiredHome 7:16129d213e6a 163 static Timer timer;
WiredHome 7:16129d213e6a 164 static bool test = false;
WiredHome 7:16129d213e6a 165 static bool toggle = false;
WiredHome 7:16129d213e6a 166 static char buf[80];
WiredHome 7:16129d213e6a 167 static int i;
WiredHome 7:16129d213e6a 168
WiredHome 7:16129d213e6a 169 if (pc.readable()) {
WiredHome 7:16129d213e6a 170 int c = pc.getc();
WiredHome 7:16129d213e6a 171 test = false;
WiredHome 7:16129d213e6a 172 switch (c) {
WiredHome 7:16129d213e6a 173 case 'r':
WiredHome 7:16129d213e6a 174 mbed_reset();
WiredHome 7:16129d213e6a 175 break;
WiredHome 7:16129d213e6a 176 case 's':
WiredHome 7:16129d213e6a 177 swUpdateCheck = true;
WiredHome 7:16129d213e6a 178 break;
WiredHome 7:16129d213e6a 179 case 't':
WiredHome 7:16129d213e6a 180 ntpUpdateCheck = true;
WiredHome 7:16129d213e6a 181 break;
WiredHome 7:16129d213e6a 182 case 'x':
WiredHome 7:16129d213e6a 183 pc.printf("x10>");
WiredHome 7:16129d213e6a 184 i = 0;
WiredHome 7:16129d213e6a 185 do {
WiredHome 7:16129d213e6a 186 c = pc.getc();
WiredHome 7:16129d213e6a 187 pc.putc(c);
WiredHome 7:16129d213e6a 188 if (c == '\x08') { // <bs>
WiredHome 7:16129d213e6a 189 if (i < 0) {
WiredHome 7:16129d213e6a 190 pc.printf("\r\n");
WiredHome 7:16129d213e6a 191 break;
WiredHome 7:16129d213e6a 192 }
WiredHome 7:16129d213e6a 193 } else if (c == '\r') {
WiredHome 7:16129d213e6a 194 buf[i++] = '\0';
WiredHome 7:16129d213e6a 195 printf("Shell Command: '%s'\r\n", buf);
WiredHome 7:16129d213e6a 196 cm17a.ParseCommand(buf);
WiredHome 7:16129d213e6a 197 break;
WiredHome 7:16129d213e6a 198 } else {
WiredHome 7:16129d213e6a 199 buf[i++] = c;
WiredHome 7:16129d213e6a 200 }
WiredHome 7:16129d213e6a 201 } while(1);
WiredHome 7:16129d213e6a 202 break;
WiredHome 7:16129d213e6a 203 case 'z':
WiredHome 7:16129d213e6a 204 pc.printf("X10 test mode enabled\r\n");
WiredHome 7:16129d213e6a 205 test = true;
WiredHome 7:16129d213e6a 206 timer.start();
WiredHome 7:16129d213e6a 207 break;
WiredHome 7:16129d213e6a 208 case '@':
WiredHome 7:16129d213e6a 209 pc.printf("Sample '%s' file.\r\n", iniFile);
WiredHome 7:16129d213e6a 210 pc.printf("[SWUpdate]\r\n");
WiredHome 7:16129d213e6a 211 pc.printf("url=http://192.168.1.201/mbed/\r\n");
WiredHome 7:16129d213e6a 212 pc.printf("name=X10svr\r\n");
WiredHome 7:16129d213e6a 213 pc.printf("[Clock]\r\n");
WiredHome 7:16129d213e6a 214 pc.printf("timeserver=time.nist.gov\r\n");
WiredHome 7:16129d213e6a 215 pc.printf("tzoffsetmin=-300\r\n");
WiredHome 7:16129d213e6a 216 pc.printf("[IP]\r\n");
WiredHome 7:16129d213e6a 217 pc.printf("ip=192.168.1.203\r\n");
WiredHome 7:16129d213e6a 218 pc.printf("nm=255.255.254.0\r\n");
WiredHome 7:16129d213e6a 219 pc.printf("gw=192.168.1.1\r\n");
WiredHome 7:16129d213e6a 220 pc.printf("[Node]\r\n");
WiredHome 7:16129d213e6a 221 pc.printf("id=X10Server-01\r\n");
WiredHome 7:16129d213e6a 222 pc.printf(";hint: Use either the fixed IP or the Node\r\n");
WiredHome 7:16129d213e6a 223 break;
WiredHome 7:16129d213e6a 224 case '?':
WiredHome 7:16129d213e6a 225 default:
WiredHome 7:16129d213e6a 226 pc.printf("\r\n\r\n");
WiredHome 7:16129d213e6a 227 if (c > ' ' && c != '?')
WiredHome 7:16129d213e6a 228 pc.printf("unknown command '%c'\r\n", c);
WiredHome 7:16129d213e6a 229 pc.printf("IP: %s\r\n", eth.getIPAddress());
WiredHome 7:16129d213e6a 230 pc.printf(" Last Boot %s %s\r\n",
WiredHome 7:16129d213e6a 231 ntp.ctime(&lastboottime),
WiredHome 7:16129d213e6a 232 (WDEventOccurred) ? "(WD event)" : "");
WiredHome 7:16129d213e6a 233 pc.printf("\r\n");
WiredHome 7:16129d213e6a 234 pc.printf("Valid commands:\r\n");
WiredHome 7:16129d213e6a 235 pc.printf(" r - reset\r\n");
WiredHome 7:16129d213e6a 236 pc.printf(" s - software update\r\n");
WiredHome 7:16129d213e6a 237 pc.printf(" t - time server sync\r\n");
WiredHome 7:16129d213e6a 238 pc.printf(" x <House><Unit> <cmd> | x # | /XXXX\r\n");
WiredHome 7:16129d213e6a 239 pc.printf(" a-p House\r\n");
WiredHome 7:16129d213e6a 240 pc.printf(" 1 - 16 Unit\r\n");
WiredHome 7:16129d213e6a 241 pc.printf(" 1=On,0=Off,+#=Bright,-#=Dim (#=1 to 6)\r\n");
WiredHome 7:16129d213e6a 242 pc.printf(" ex: x a1 1 a3 +2\r\n");
WiredHome 7:16129d213e6a 243 pc.printf(" # = set baud rate\r\n");
WiredHome 7:16129d213e6a 244 pc.printf(" /XXXX send hex code XXXX\r\n");
WiredHome 7:16129d213e6a 245 pc.printf(" z - x10 test mode (toggles a1 on/off)\r\n");
WiredHome 7:16129d213e6a 246 pc.printf(" @ - Show a sample '%s' file.\r\n", iniFile);
WiredHome 7:16129d213e6a 247 pc.printf("\r\n");
WiredHome 7:16129d213e6a 248 break;
WiredHome 7:16129d213e6a 249 }
WiredHome 7:16129d213e6a 250 } else {
WiredHome 7:16129d213e6a 251 if (test) {
WiredHome 7:16129d213e6a 252 if (timer.read_ms() > 1000) {
WiredHome 7:16129d213e6a 253 timer.reset();
WiredHome 7:16129d213e6a 254 pc.printf(" Test Mode: Sending a1 %d\r\n", toggle);
WiredHome 7:16129d213e6a 255 if (toggle) {
WiredHome 7:16129d213e6a 256 cm17a.ParseCommand("a1 1");
WiredHome 7:16129d213e6a 257 } else {
WiredHome 7:16129d213e6a 258 cm17a.ParseCommand("a1 0");
WiredHome 7:16129d213e6a 259 }
WiredHome 7:16129d213e6a 260 toggle = !toggle;
WiredHome 7:16129d213e6a 261 }
WiredHome 7:16129d213e6a 262 }
WiredHome 7:16129d213e6a 263 }
WiredHome 7:16129d213e6a 264 }
WiredHome 7:16129d213e6a 265
WiredHome 7:16129d213e6a 266
WiredHome 7:16129d213e6a 267 /// This handler is registered for callbacks from the X10server.
WiredHome 7:16129d213e6a 268 ///
WiredHome 7:16129d213e6a 269 /// It has only the simple responsibility of passing the command
WiredHome 7:16129d213e6a 270 /// forward to the CM17a driver. As a useful side-effect, it
WiredHome 7:16129d213e6a 271 /// blinks the Network interface data LED.
WiredHome 7:16129d213e6a 272 ///
WiredHome 7:16129d213e6a 273 void x10Handler(char * buffer, int size)
WiredHome 7:16129d213e6a 274 {
WiredHome 7:16129d213e6a 275 time_t ctTime;
WiredHome 7:16129d213e6a 276
WiredHome 7:16129d213e6a 277 ctTime = ntp.time();
WiredHome 7:16129d213e6a 278 linkdata = true;
WiredHome 7:16129d213e6a 279 pc.printf("X10 (%6s) %s (UTC)\r\n", buffer, ntp.ctime(&ctTime));
WiredHome 7:16129d213e6a 280 cm17a.ParseCommand(buffer);
WiredHome 7:16129d213e6a 281 wait_ms(100);
WiredHome 7:16129d213e6a 282 linkdata = false;
WiredHome 7:16129d213e6a 283 }
WiredHome 7:16129d213e6a 284
WiredHome 7:16129d213e6a 285
WiredHome 7:16129d213e6a 286
WiredHome 5:6244e237def1 287 int main()
WiredHome 4:5da66fab599c 288 {
WiredHome 7:16129d213e6a 289 char ip[20],nm[20],gw[20];
WiredHome 0:de1dfa2ab813 290 pc.baud(460800);
WiredHome 0:de1dfa2ab813 291 pc.printf("\r\n%s Build %s\r\n", PROG_NAME, BUILD_DATE);
WiredHome 4:5da66fab599c 292 lastboottime = ntp.timelocal();
WiredHome 0:de1dfa2ab813 293 if (wd.WatchdogCausedReset()) {
WiredHome 0:de1dfa2ab813 294 pc.printf("**** Watchdog Event caused reset at %s ****\r\n", ntp.ctime(&lastboottime));
WiredHome 7:16129d213e6a 295 WDEventOccurred = true;
WiredHome 0:de1dfa2ab813 296 }
WiredHome 7:16129d213e6a 297 wd.Configure(25); // very generous, but this is a network appliance, so a bit less deterministic.
WiredHome 7:16129d213e6a 298
WiredHome 7:16129d213e6a 299 ini.SetFile(iniFile, 2);
WiredHome 0:de1dfa2ab813 300
WiredHome 5:6244e237def1 301 pc.printf("Initializing network interface...\r\n");
WiredHome 7:16129d213e6a 302 int initResult;
WiredHome 7:16129d213e6a 303
WiredHome 7:16129d213e6a 304 if (INI::INI_SUCCESS == ini.ReadString("IP", "ip", ip, sizeof(ip))
WiredHome 7:16129d213e6a 305 && INI::INI_SUCCESS == ini.ReadString("IP", "nm", nm, sizeof(nm))
WiredHome 7:16129d213e6a 306 && INI::INI_SUCCESS == ini.ReadString("IP", "gw", gw, sizeof(gw))) {
WiredHome 7:16129d213e6a 307 initResult = eth.init(ip,nm,gw); // use Fixed
WiredHome 7:16129d213e6a 308 } else {
WiredHome 7:16129d213e6a 309 initResult = eth.init(); // use DHCP
WiredHome 7:16129d213e6a 310 }
WiredHome 7:16129d213e6a 311 if (initResult) {
WiredHome 7:16129d213e6a 312 // Failed to init ...
WiredHome 7:16129d213e6a 313 pc.printf(" ... failed to initialize, rebooting...\r\n");
WiredHome 7:16129d213e6a 314 wait_ms(5000);
WiredHome 7:16129d213e6a 315 mbed_reset();
WiredHome 7:16129d213e6a 316 } else {
WiredHome 7:16129d213e6a 317 char * nn = (char *)malloc(33);
WiredHome 7:16129d213e6a 318 if (!nn)
WiredHome 7:16129d213e6a 319 error("no mem for network name");
WiredHome 7:16129d213e6a 320 ini.ReadString("Node", "id", nn, 32, "Name Me");
WiredHome 7:16129d213e6a 321 pc.printf("Name: %s\r\n", nn);
WiredHome 7:16129d213e6a 322 eth.setName(nn);
WiredHome 7:16129d213e6a 323
WiredHome 7:16129d213e6a 324 char * port = (char *)malloc(33);
WiredHome 7:16129d213e6a 325 uint16_t portNum = 10630; // X10 Listener Port
WiredHome 7:16129d213e6a 326 if (!port)
WiredHome 7:16129d213e6a 327 error("no mem for port");
WiredHome 7:16129d213e6a 328 if (INI::INI_SUCCESS == ini.ReadString("IP", "port", port, sizeof(port)))
WiredHome 7:16129d213e6a 329 portNum = atoi(port);
WiredHome 4:5da66fab599c 330
WiredHome 5:6244e237def1 331 do {
WiredHome 5:6244e237def1 332 pc.printf("Connecting to network...\r\n");
WiredHome 5:6244e237def1 333 if (0 == eth.connect()) {
WiredHome 7:16129d213e6a 334 wd.Service();
WiredHome 7:16129d213e6a 335 linkup = true;
WiredHome 7:16129d213e6a 336 time_t tstart = ntp.time();
WiredHome 5:6244e237def1 337 int speed = eth.get_connection_speed();
WiredHome 5:6244e237def1 338
WiredHome 7:16129d213e6a 339 pc.printf("Ethernet Connected at: %d Mb/s\r\n", speed);
WiredHome 7:16129d213e6a 340 pc.printf(" IP: %15s\r\n", eth.getIPAddress());
WiredHome 5:6244e237def1 341
WiredHome 5:6244e237def1 342 HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc);
WiredHome 5:6244e237def1 343 svr.RegisterHandler("/", RootPage);
WiredHome 7:16129d213e6a 344 svr.RegisterHandler("/info", InfoPage);
WiredHome 7:16129d213e6a 345 svr.RegisterHandler("/software", SoftwarePage);
WiredHome 5:6244e237def1 346 svr.RegisterHandler("/setup.xml", Setup_xml);
WiredHome 5:6244e237def1 347 SSDP ssdp(My_Name, eth.getMACAddress(), eth.getIPAddress(), Server_Port);
WiredHome 4:5da66fab599c 348
WiredHome 7:16129d213e6a 349 pc.printf(" X10 Server started %s (UTC)\r\n", ntp.ctime(&tstart));
WiredHome 7:16129d213e6a 350 X10Server x10svr(&x10Handler, portNum);
WiredHome 5:6244e237def1 351
WiredHome 5:6244e237def1 352 while (eth.is_connected()) {
WiredHome 7:16129d213e6a 353 static time_t tLastSec;
WiredHome 7:16129d213e6a 354
WiredHome 5:6244e237def1 355 wd.Service();
WiredHome 5:6244e237def1 356 time_t tNow = ntp.timelocal();
WiredHome 7:16129d213e6a 357 CheckConsoleInput();
WiredHome 7:16129d213e6a 358 x10svr.poll();
WiredHome 7:16129d213e6a 359 svr.Poll(); // Web Server: non-blocking, but also not deterministic
WiredHome 7:16129d213e6a 360 ShowSignOfLife(1);
WiredHome 7:16129d213e6a 361 if (tNow != tLastSec) {
WiredHome 5:6244e237def1 362 pc.printf("time is %s\r\n", ntp.ctime(&tNow));
WiredHome 7:16129d213e6a 363 tLastSec = tNow;
WiredHome 5:6244e237def1 364 }
WiredHome 7:16129d213e6a 365 SyncToNTPServer(ntpUpdateCheck);
WiredHome 7:16129d213e6a 366 SoftwareUpdateCheck(swUpdateCheck);
WiredHome 5:6244e237def1 367 // Any other work can happen here
WiredHome 7:16129d213e6a 368 // ...
WiredHome 5:6244e237def1 369 Thread::yield();
WiredHome 5:6244e237def1 370 }
WiredHome 7:16129d213e6a 371 linkup = false;
WiredHome 7:16129d213e6a 372 linkdata = false;
WiredHome 5:6244e237def1 373 pc.printf("lost connection.\r\n");
WiredHome 5:6244e237def1 374 eth.disconnect();
WiredHome 5:6244e237def1 375 } else {
WiredHome 5:6244e237def1 376 pc.printf(" ... failed to connect.\r\n");
WiredHome 5:6244e237def1 377 }
WiredHome 5:6244e237def1 378 } while (1);
WiredHome 0:de1dfa2ab813 379 }
WiredHome 0:de1dfa2ab813 380 }