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@12:67c524db9e07, 2019-03-04 (annotated)
- Committer:
- WiredHome
- Date:
- Mon Mar 04 00:05:08 2019 +0000
- Revision:
- 12:67c524db9e07
- Parent:
- 9:2c96e69b6035
Fix the SignOfLife LED display.
Who changed what in which revision?
User | Revision | Line number | New 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(ð); |
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 | 8:a45fe77efcc5 | 90 | pc.printf(" update failed %04X - %s\r\n", su, |
WiredHome | 8:a45fe77efcc5 | 91 | SoftwareUpdateGetHTTPErrorMsg(SoftwareUpdateGetHTTPErrorCode())); |
WiredHome | 7:16129d213e6a | 92 | Thread::wait(1000); |
WiredHome | 7:16129d213e6a | 93 | } |
WiredHome | 7:16129d213e6a | 94 | linkdata = false; |
WiredHome | 7:16129d213e6a | 95 | } else { |
WiredHome | 7:16129d213e6a | 96 | pc.printf(" can't get info from ini file.\r\n"); |
WiredHome | 7:16129d213e6a | 97 | swUpdateCheck = false; |
WiredHome | 7:16129d213e6a | 98 | } |
WiredHome | 7:16129d213e6a | 99 | //eth_mutex.unlock(); |
WiredHome | 7:16129d213e6a | 100 | } |
WiredHome | 7:16129d213e6a | 101 | } |
WiredHome | 7:16129d213e6a | 102 | |
WiredHome | 7:16129d213e6a | 103 | |
WiredHome | 7:16129d213e6a | 104 | /// This function syncs the node to a timeserver, if one |
WiredHome | 7:16129d213e6a | 105 | /// is configured in the .ini file. |
WiredHome | 7:16129d213e6a | 106 | /// |
WiredHome | 7:16129d213e6a | 107 | void SyncToNTPServer(bool force) |
WiredHome | 7:16129d213e6a | 108 | { |
WiredHome | 7:16129d213e6a | 109 | char url[100]; |
WiredHome | 7:16129d213e6a | 110 | char tzone[10]; |
WiredHome | 7:16129d213e6a | 111 | char dstFlag[5]; // off,on,auto |
WiredHome | 7:16129d213e6a | 112 | char dstStart[12]; // mm/dd,hh:mm |
WiredHome | 7:16129d213e6a | 113 | char dstStop[12]; // mm/dd,hh:mm |
WiredHome | 7:16129d213e6a | 114 | static time_t tlast = 0; |
WiredHome | 7:16129d213e6a | 115 | time_t tnow = ntp.time(); |
WiredHome | 7:16129d213e6a | 116 | |
WiredHome | 7:16129d213e6a | 117 | if (((tnow - tlast) > (60*60*24)) || force) { |
WiredHome | 7:16129d213e6a | 118 | printf("SyncToNTPServer\r\n"); |
WiredHome | 7:16129d213e6a | 119 | if (INI::INI_SUCCESS == ini.ReadString("Clock", "timeserver", url, sizeof(url))) { |
WiredHome | 7:16129d213e6a | 120 | ini.ReadString("Clock", "tzoffsetmin", tzone, sizeof(tzone), "0"); |
WiredHome | 7:16129d213e6a | 121 | ini.ReadString("Clock", "dst", dstFlag, sizeof(dstFlag), "0"); |
WiredHome | 7:16129d213e6a | 122 | ini.ReadString("Clock", "dststart", dstStart, sizeof(dstStart), ""); |
WiredHome | 7:16129d213e6a | 123 | ini.ReadString("Clock", "dststop", dstStop, sizeof(dstStop), ""); |
WiredHome | 7:16129d213e6a | 124 | |
WiredHome | 7:16129d213e6a | 125 | printf("NTP update time from (%s)\r\n", url); |
WiredHome | 7:16129d213e6a | 126 | int32_t tzo_min = atoi(tzone); |
WiredHome | 7:16129d213e6a | 127 | |
WiredHome | 7:16129d213e6a | 128 | if (strcmp(dstFlag,"on") == 0) { |
WiredHome | 7:16129d213e6a | 129 | ntp.set_dst(1); |
WiredHome | 7:16129d213e6a | 130 | } else if (strcmp(dstFlag, "off") == 0) { |
WiredHome | 7:16129d213e6a | 131 | ntp.set_dst(0); |
WiredHome | 7:16129d213e6a | 132 | } else /* if (strcmp(dstFlag, "auto") == 0) */ { |
WiredHome | 7:16129d213e6a | 133 | ntp.set_dst(dstStart,dstStop); |
WiredHome | 7:16129d213e6a | 134 | } |
WiredHome | 7:16129d213e6a | 135 | ntp.set_tzo_min(tzo_min); |
WiredHome | 8:a45fe77efcc5 | 136 | linkdata = true; |
WiredHome | 7:16129d213e6a | 137 | int res = ntp.setTime(url); |
WiredHome | 7:16129d213e6a | 138 | //printf(" NTP (release ethernet)\r\n"); |
WiredHome | 7:16129d213e6a | 139 | if (res == 0) { |
WiredHome | 7:16129d213e6a | 140 | time_t ctTime; |
WiredHome | 7:16129d213e6a | 141 | ctTime = ntp.time(); |
WiredHome | 7:16129d213e6a | 142 | ntpSyncd = ntp.get_timelastset();; |
WiredHome | 7:16129d213e6a | 143 | tlast = ctTime; |
WiredHome | 7:16129d213e6a | 144 | printf(" Time set to (UTC): %s\r\n", ntp.ctime(&ctTime)); |
WiredHome | 7:16129d213e6a | 145 | printf(" ntpSyncd: %s\r\n", ntp.ctime(&ntpSyncd)); |
WiredHome | 8:a45fe77efcc5 | 146 | ntpUpdateCheck = false; |
WiredHome | 7:16129d213e6a | 147 | } else { |
WiredHome | 7:16129d213e6a | 148 | ntpSyncd = 0; |
WiredHome | 7:16129d213e6a | 149 | printf("Error return from setTime(%s) %d\r\n", url, res); |
WiredHome | 7:16129d213e6a | 150 | } |
WiredHome | 8:a45fe77efcc5 | 151 | linkdata = false; |
WiredHome | 7:16129d213e6a | 152 | } else { |
WiredHome | 7:16129d213e6a | 153 | ntpSyncd = 0; |
WiredHome | 7:16129d213e6a | 154 | printf("no time server was set\r\n"); |
WiredHome | 7:16129d213e6a | 155 | } |
WiredHome | 7:16129d213e6a | 156 | } |
WiredHome | 7:16129d213e6a | 157 | } |
WiredHome | 7:16129d213e6a | 158 | |
WiredHome | 7:16129d213e6a | 159 | |
WiredHome | 7:16129d213e6a | 160 | /// This function monitors the USB serial interface for activity |
WiredHome | 7:16129d213e6a | 161 | /// from a connected user. |
WiredHome | 7:16129d213e6a | 162 | /// |
WiredHome | 7:16129d213e6a | 163 | /// It offers a tiny bit of help if an unrecognized command is entered. |
WiredHome | 7:16129d213e6a | 164 | /// |
WiredHome | 7:16129d213e6a | 165 | void CheckConsoleInput(void) |
WiredHome | 7:16129d213e6a | 166 | { |
WiredHome | 7:16129d213e6a | 167 | static Timer timer; |
WiredHome | 7:16129d213e6a | 168 | static bool test = false; |
WiredHome | 7:16129d213e6a | 169 | static bool toggle = false; |
WiredHome | 7:16129d213e6a | 170 | static char buf[80]; |
WiredHome | 7:16129d213e6a | 171 | static int i; |
WiredHome | 7:16129d213e6a | 172 | |
WiredHome | 7:16129d213e6a | 173 | if (pc.readable()) { |
WiredHome | 7:16129d213e6a | 174 | int c = pc.getc(); |
WiredHome | 7:16129d213e6a | 175 | test = false; |
WiredHome | 7:16129d213e6a | 176 | switch (c) { |
WiredHome | 7:16129d213e6a | 177 | case 'r': |
WiredHome | 7:16129d213e6a | 178 | mbed_reset(); |
WiredHome | 7:16129d213e6a | 179 | break; |
WiredHome | 7:16129d213e6a | 180 | case 's': |
WiredHome | 7:16129d213e6a | 181 | swUpdateCheck = true; |
WiredHome | 7:16129d213e6a | 182 | break; |
WiredHome | 7:16129d213e6a | 183 | case 't': |
WiredHome | 7:16129d213e6a | 184 | ntpUpdateCheck = true; |
WiredHome | 7:16129d213e6a | 185 | break; |
WiredHome | 7:16129d213e6a | 186 | case 'x': |
WiredHome | 7:16129d213e6a | 187 | pc.printf("x10>"); |
WiredHome | 7:16129d213e6a | 188 | i = 0; |
WiredHome | 7:16129d213e6a | 189 | do { |
WiredHome | 7:16129d213e6a | 190 | c = pc.getc(); |
WiredHome | 7:16129d213e6a | 191 | pc.putc(c); |
WiredHome | 7:16129d213e6a | 192 | if (c == '\x08') { // <bs> |
WiredHome | 7:16129d213e6a | 193 | if (i < 0) { |
WiredHome | 7:16129d213e6a | 194 | pc.printf("\r\n"); |
WiredHome | 7:16129d213e6a | 195 | break; |
WiredHome | 7:16129d213e6a | 196 | } |
WiredHome | 7:16129d213e6a | 197 | } else if (c == '\r') { |
WiredHome | 7:16129d213e6a | 198 | buf[i++] = '\0'; |
WiredHome | 7:16129d213e6a | 199 | printf("Shell Command: '%s'\r\n", buf); |
WiredHome | 7:16129d213e6a | 200 | cm17a.ParseCommand(buf); |
WiredHome | 7:16129d213e6a | 201 | break; |
WiredHome | 7:16129d213e6a | 202 | } else { |
WiredHome | 7:16129d213e6a | 203 | buf[i++] = c; |
WiredHome | 7:16129d213e6a | 204 | } |
WiredHome | 7:16129d213e6a | 205 | } while(1); |
WiredHome | 7:16129d213e6a | 206 | break; |
WiredHome | 7:16129d213e6a | 207 | case 'z': |
WiredHome | 7:16129d213e6a | 208 | pc.printf("X10 test mode enabled\r\n"); |
WiredHome | 7:16129d213e6a | 209 | test = true; |
WiredHome | 7:16129d213e6a | 210 | timer.start(); |
WiredHome | 7:16129d213e6a | 211 | break; |
WiredHome | 7:16129d213e6a | 212 | case '@': |
WiredHome | 7:16129d213e6a | 213 | pc.printf("Sample '%s' file.\r\n", iniFile); |
WiredHome | 7:16129d213e6a | 214 | pc.printf("[SWUpdate]\r\n"); |
WiredHome | 7:16129d213e6a | 215 | pc.printf("url=http://192.168.1.201/mbed/\r\n"); |
WiredHome | 7:16129d213e6a | 216 | pc.printf("name=X10svr\r\n"); |
WiredHome | 7:16129d213e6a | 217 | pc.printf("[Clock]\r\n"); |
WiredHome | 7:16129d213e6a | 218 | pc.printf("timeserver=time.nist.gov\r\n"); |
WiredHome | 7:16129d213e6a | 219 | pc.printf("tzoffsetmin=-300\r\n"); |
WiredHome | 7:16129d213e6a | 220 | pc.printf("[IP]\r\n"); |
WiredHome | 7:16129d213e6a | 221 | pc.printf("ip=192.168.1.203\r\n"); |
WiredHome | 7:16129d213e6a | 222 | pc.printf("nm=255.255.254.0\r\n"); |
WiredHome | 7:16129d213e6a | 223 | pc.printf("gw=192.168.1.1\r\n"); |
WiredHome | 7:16129d213e6a | 224 | pc.printf("[Node]\r\n"); |
WiredHome | 7:16129d213e6a | 225 | pc.printf("id=X10Server-01\r\n"); |
WiredHome | 7:16129d213e6a | 226 | pc.printf(";hint: Use either the fixed IP or the Node\r\n"); |
WiredHome | 7:16129d213e6a | 227 | break; |
WiredHome | 7:16129d213e6a | 228 | case '?': |
WiredHome | 7:16129d213e6a | 229 | default: |
WiredHome | 7:16129d213e6a | 230 | pc.printf("\r\n\r\n"); |
WiredHome | 7:16129d213e6a | 231 | if (c > ' ' && c != '?') |
WiredHome | 7:16129d213e6a | 232 | pc.printf("unknown command '%c'\r\n", c); |
WiredHome | 7:16129d213e6a | 233 | pc.printf("IP: %s\r\n", eth.getIPAddress()); |
WiredHome | 7:16129d213e6a | 234 | pc.printf(" Last Boot %s %s\r\n", |
WiredHome | 7:16129d213e6a | 235 | ntp.ctime(&lastboottime), |
WiredHome | 7:16129d213e6a | 236 | (WDEventOccurred) ? "(WD event)" : ""); |
WiredHome | 7:16129d213e6a | 237 | pc.printf("\r\n"); |
WiredHome | 7:16129d213e6a | 238 | pc.printf("Valid commands:\r\n"); |
WiredHome | 7:16129d213e6a | 239 | pc.printf(" r - reset\r\n"); |
WiredHome | 7:16129d213e6a | 240 | pc.printf(" s - software update\r\n"); |
WiredHome | 7:16129d213e6a | 241 | pc.printf(" t - time server sync\r\n"); |
WiredHome | 7:16129d213e6a | 242 | pc.printf(" x <House><Unit> <cmd> | x # | /XXXX\r\n"); |
WiredHome | 7:16129d213e6a | 243 | pc.printf(" a-p House\r\n"); |
WiredHome | 7:16129d213e6a | 244 | pc.printf(" 1 - 16 Unit\r\n"); |
WiredHome | 7:16129d213e6a | 245 | pc.printf(" 1=On,0=Off,+#=Bright,-#=Dim (#=1 to 6)\r\n"); |
WiredHome | 7:16129d213e6a | 246 | pc.printf(" ex: x a1 1 a3 +2\r\n"); |
WiredHome | 7:16129d213e6a | 247 | pc.printf(" # = set baud rate\r\n"); |
WiredHome | 7:16129d213e6a | 248 | pc.printf(" /XXXX send hex code XXXX\r\n"); |
WiredHome | 7:16129d213e6a | 249 | pc.printf(" z - x10 test mode (toggles a1 on/off)\r\n"); |
WiredHome | 7:16129d213e6a | 250 | pc.printf(" @ - Show a sample '%s' file.\r\n", iniFile); |
WiredHome | 7:16129d213e6a | 251 | pc.printf("\r\n"); |
WiredHome | 7:16129d213e6a | 252 | break; |
WiredHome | 7:16129d213e6a | 253 | } |
WiredHome | 7:16129d213e6a | 254 | } else { |
WiredHome | 7:16129d213e6a | 255 | if (test) { |
WiredHome | 7:16129d213e6a | 256 | if (timer.read_ms() > 1000) { |
WiredHome | 7:16129d213e6a | 257 | timer.reset(); |
WiredHome | 7:16129d213e6a | 258 | pc.printf(" Test Mode: Sending a1 %d\r\n", toggle); |
WiredHome | 7:16129d213e6a | 259 | if (toggle) { |
WiredHome | 7:16129d213e6a | 260 | cm17a.ParseCommand("a1 1"); |
WiredHome | 7:16129d213e6a | 261 | } else { |
WiredHome | 7:16129d213e6a | 262 | cm17a.ParseCommand("a1 0"); |
WiredHome | 7:16129d213e6a | 263 | } |
WiredHome | 7:16129d213e6a | 264 | toggle = !toggle; |
WiredHome | 7:16129d213e6a | 265 | } |
WiredHome | 7:16129d213e6a | 266 | } |
WiredHome | 7:16129d213e6a | 267 | } |
WiredHome | 7:16129d213e6a | 268 | } |
WiredHome | 7:16129d213e6a | 269 | |
WiredHome | 7:16129d213e6a | 270 | |
WiredHome | 7:16129d213e6a | 271 | /// This handler is registered for callbacks from the X10server. |
WiredHome | 7:16129d213e6a | 272 | /// |
WiredHome | 7:16129d213e6a | 273 | /// It has only the simple responsibility of passing the command |
WiredHome | 7:16129d213e6a | 274 | /// forward to the CM17a driver. As a useful side-effect, it |
WiredHome | 7:16129d213e6a | 275 | /// blinks the Network interface data LED. |
WiredHome | 7:16129d213e6a | 276 | /// |
WiredHome | 7:16129d213e6a | 277 | void x10Handler(char * buffer, int size) |
WiredHome | 7:16129d213e6a | 278 | { |
WiredHome | 7:16129d213e6a | 279 | time_t ctTime; |
WiredHome | 7:16129d213e6a | 280 | |
WiredHome | 7:16129d213e6a | 281 | ctTime = ntp.time(); |
WiredHome | 7:16129d213e6a | 282 | linkdata = true; |
WiredHome | 7:16129d213e6a | 283 | pc.printf("X10 (%6s) %s (UTC)\r\n", buffer, ntp.ctime(&ctTime)); |
WiredHome | 7:16129d213e6a | 284 | cm17a.ParseCommand(buffer); |
WiredHome | 7:16129d213e6a | 285 | wait_ms(100); |
WiredHome | 7:16129d213e6a | 286 | linkdata = false; |
WiredHome | 7:16129d213e6a | 287 | } |
WiredHome | 7:16129d213e6a | 288 | |
WiredHome | 7:16129d213e6a | 289 | |
WiredHome | 7:16129d213e6a | 290 | |
WiredHome | 5:6244e237def1 | 291 | int main() |
WiredHome | 4:5da66fab599c | 292 | { |
WiredHome | 7:16129d213e6a | 293 | char ip[20],nm[20],gw[20]; |
WiredHome | 0:de1dfa2ab813 | 294 | pc.baud(460800); |
WiredHome | 0:de1dfa2ab813 | 295 | pc.printf("\r\n%s Build %s\r\n", PROG_NAME, BUILD_DATE); |
WiredHome | 4:5da66fab599c | 296 | lastboottime = ntp.timelocal(); |
WiredHome | 0:de1dfa2ab813 | 297 | if (wd.WatchdogCausedReset()) { |
WiredHome | 0:de1dfa2ab813 | 298 | pc.printf("**** Watchdog Event caused reset at %s ****\r\n", ntp.ctime(&lastboottime)); |
WiredHome | 7:16129d213e6a | 299 | WDEventOccurred = true; |
WiredHome | 0:de1dfa2ab813 | 300 | } |
WiredHome | 7:16129d213e6a | 301 | wd.Configure(25); // very generous, but this is a network appliance, so a bit less deterministic. |
WiredHome | 7:16129d213e6a | 302 | |
WiredHome | 7:16129d213e6a | 303 | ini.SetFile(iniFile, 2); |
WiredHome | 0:de1dfa2ab813 | 304 | |
WiredHome | 5:6244e237def1 | 305 | pc.printf("Initializing network interface...\r\n"); |
WiredHome | 7:16129d213e6a | 306 | int initResult; |
WiredHome | 7:16129d213e6a | 307 | |
WiredHome | 7:16129d213e6a | 308 | if (INI::INI_SUCCESS == ini.ReadString("IP", "ip", ip, sizeof(ip)) |
WiredHome | 7:16129d213e6a | 309 | && INI::INI_SUCCESS == ini.ReadString("IP", "nm", nm, sizeof(nm)) |
WiredHome | 7:16129d213e6a | 310 | && INI::INI_SUCCESS == ini.ReadString("IP", "gw", gw, sizeof(gw))) { |
WiredHome | 7:16129d213e6a | 311 | initResult = eth.init(ip,nm,gw); // use Fixed |
WiredHome | 7:16129d213e6a | 312 | } else { |
WiredHome | 7:16129d213e6a | 313 | initResult = eth.init(); // use DHCP |
WiredHome | 7:16129d213e6a | 314 | } |
WiredHome | 7:16129d213e6a | 315 | if (initResult) { |
WiredHome | 7:16129d213e6a | 316 | // Failed to init ... |
WiredHome | 7:16129d213e6a | 317 | pc.printf(" ... failed to initialize, rebooting...\r\n"); |
WiredHome | 7:16129d213e6a | 318 | wait_ms(5000); |
WiredHome | 7:16129d213e6a | 319 | mbed_reset(); |
WiredHome | 7:16129d213e6a | 320 | } else { |
WiredHome | 7:16129d213e6a | 321 | char * nn = (char *)malloc(33); |
WiredHome | 7:16129d213e6a | 322 | if (!nn) |
WiredHome | 7:16129d213e6a | 323 | error("no mem for network name"); |
WiredHome | 7:16129d213e6a | 324 | ini.ReadString("Node", "id", nn, 32, "Name Me"); |
WiredHome | 7:16129d213e6a | 325 | pc.printf("Name: %s\r\n", nn); |
WiredHome | 7:16129d213e6a | 326 | eth.setName(nn); |
WiredHome | 7:16129d213e6a | 327 | |
WiredHome | 7:16129d213e6a | 328 | char * port = (char *)malloc(33); |
WiredHome | 7:16129d213e6a | 329 | uint16_t portNum = 10630; // X10 Listener Port |
WiredHome | 7:16129d213e6a | 330 | if (!port) |
WiredHome | 7:16129d213e6a | 331 | error("no mem for port"); |
WiredHome | 7:16129d213e6a | 332 | if (INI::INI_SUCCESS == ini.ReadString("IP", "port", port, sizeof(port))) |
WiredHome | 7:16129d213e6a | 333 | portNum = atoi(port); |
WiredHome | 4:5da66fab599c | 334 | |
WiredHome | 5:6244e237def1 | 335 | do { |
WiredHome | 5:6244e237def1 | 336 | pc.printf("Connecting to network...\r\n"); |
WiredHome | 5:6244e237def1 | 337 | if (0 == eth.connect()) { |
WiredHome | 7:16129d213e6a | 338 | wd.Service(); |
WiredHome | 7:16129d213e6a | 339 | linkup = true; |
WiredHome | 7:16129d213e6a | 340 | time_t tstart = ntp.time(); |
WiredHome | 5:6244e237def1 | 341 | int speed = eth.get_connection_speed(); |
WiredHome | 5:6244e237def1 | 342 | |
WiredHome | 7:16129d213e6a | 343 | pc.printf("Ethernet Connected at: %d Mb/s\r\n", speed); |
WiredHome | 7:16129d213e6a | 344 | pc.printf(" IP: %15s\r\n", eth.getIPAddress()); |
WiredHome | 5:6244e237def1 | 345 | |
WiredHome | 5:6244e237def1 | 346 | HTTPServer svr(Server_Port, Server_Root, 15, 30, 20, 50, &pc); |
WiredHome | 5:6244e237def1 | 347 | svr.RegisterHandler("/", RootPage); |
WiredHome | 7:16129d213e6a | 348 | svr.RegisterHandler("/info", InfoPage); |
WiredHome | 7:16129d213e6a | 349 | svr.RegisterHandler("/software", SoftwarePage); |
WiredHome | 8:a45fe77efcc5 | 350 | svr.RegisterHandler("/reboot", RebootPage); |
WiredHome | 5:6244e237def1 | 351 | svr.RegisterHandler("/setup.xml", Setup_xml); |
WiredHome | 5:6244e237def1 | 352 | SSDP ssdp(My_Name, eth.getMACAddress(), eth.getIPAddress(), Server_Port); |
WiredHome | 4:5da66fab599c | 353 | |
WiredHome | 7:16129d213e6a | 354 | pc.printf(" X10 Server started %s (UTC)\r\n", ntp.ctime(&tstart)); |
WiredHome | 7:16129d213e6a | 355 | X10Server x10svr(&x10Handler, portNum); |
WiredHome | 5:6244e237def1 | 356 | |
WiredHome | 5:6244e237def1 | 357 | while (eth.is_connected()) { |
WiredHome | 7:16129d213e6a | 358 | static time_t tLastSec; |
WiredHome | 7:16129d213e6a | 359 | |
WiredHome | 5:6244e237def1 | 360 | wd.Service(); |
WiredHome | 5:6244e237def1 | 361 | time_t tNow = ntp.timelocal(); |
WiredHome | 7:16129d213e6a | 362 | CheckConsoleInput(); |
WiredHome | 7:16129d213e6a | 363 | x10svr.poll(); |
WiredHome | 7:16129d213e6a | 364 | svr.Poll(); // Web Server: non-blocking, but also not deterministic |
WiredHome | 12:67c524db9e07 | 365 | ShowSignOfLife(3); |
WiredHome | 7:16129d213e6a | 366 | if (tNow != tLastSec) { |
WiredHome | 5:6244e237def1 | 367 | pc.printf("time is %s\r\n", ntp.ctime(&tNow)); |
WiredHome | 7:16129d213e6a | 368 | tLastSec = tNow; |
WiredHome | 5:6244e237def1 | 369 | } |
WiredHome | 7:16129d213e6a | 370 | SyncToNTPServer(ntpUpdateCheck); |
WiredHome | 7:16129d213e6a | 371 | SoftwareUpdateCheck(swUpdateCheck); |
WiredHome | 5:6244e237def1 | 372 | // Any other work can happen here |
WiredHome | 7:16129d213e6a | 373 | // ... |
WiredHome | 5:6244e237def1 | 374 | Thread::yield(); |
WiredHome | 5:6244e237def1 | 375 | } |
WiredHome | 7:16129d213e6a | 376 | linkup = false; |
WiredHome | 7:16129d213e6a | 377 | linkdata = false; |
WiredHome | 5:6244e237def1 | 378 | pc.printf("lost connection.\r\n"); |
WiredHome | 5:6244e237def1 | 379 | eth.disconnect(); |
WiredHome | 5:6244e237def1 | 380 | } else { |
WiredHome | 5:6244e237def1 | 381 | pc.printf(" ... failed to connect.\r\n"); |
WiredHome | 5:6244e237def1 | 382 | } |
WiredHome | 5:6244e237def1 | 383 | } while (1); |
WiredHome | 0:de1dfa2ab813 | 384 | } |
WiredHome | 0:de1dfa2ab813 | 385 | } |