From de8cd08f333fd0bd50facf76b43320f0f900958d Mon Sep 17 00:00:00 2001 From: Tim Louis Schrumpf Date: Sun, 14 Feb 2016 15:08:49 +0100 Subject: [PATCH 1/7] Update updated of file from somewhere on the internet.. tweaked to use binary for elro and intertechno --- send.cpp | 333 ++++++++++++++++++++----------------------------------- 1 file changed, 121 insertions(+), 212 deletions(-) diff --git a/send.cpp b/send.cpp index 0ce08cd..f570701 100644 --- a/send.cpp +++ b/send.cpp @@ -1,237 +1,146 @@ /* - * Usage: ./send - * Command is 0 for OFF and 1 for ON + Usage: ./send + SystemCodeType is 1 for default and 2 for switches with 10 Bits 123456ABCD + Command is 0 for OFF and 1 for ON */ #include "RCSwitch.h" #include #include -#include -//#include +#include -void printUsage() { - printf("This is rasperry remote, an application to control remote plugs with the Raspberry Pi.\n"); - printf("Based on RCSwitch and wiringPi. See github.com/xkonni/raspberry-remote for further reference.\n"); - printf("Usage: \n sudo send [-b] or:\n sudo send -h\n"); - printf("Where the -b argument switches the instance to binary mode, and the -h option displays this help.\n\n"); - printf("Binary mode means, that instead numbering the sockets by 00001 00010 00100 01000 10000, the sockets\n"); - printf("are numbered in real binary numbers as following: 00001 00010 00011 00100 00101 00110 and so on.\n"); - printf("This means that your sockets need to be setup in this manner, which often includes that the dedicated remote\n"); - printf("is rendered useless, but more than 6 sockets are supported.\n"); -} - -int multipleSwitch(int argc, char *argv[]) { - unsigned int PIN = 0; - bool binaryMode = false; - char *systemCode; - unsigned int unitCode; - unsigned int command; - unsigned int numberOfActuators; - unsigned int i; - - printf("multi Mode \n"); - - command = atoi(argv[argc - 1]); - - if (argv[1] = "-b") { - binaryMode = true; - numberOfActuators = (argc - 3) / 2; - } else { - numberOfActuators = (argc - 2) / 2; - } - - if (wiringPiSetup() == -1) return 1; - piHiPri(20); +int main(int argc, char *argv[]) { + + /* + output PIN is hardcoded for testing purposes + see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ + for pin mapping of the raspberry pi GPIO connector + */ + int PIN = 0; // GPIO-PIN 17 + int systemCodeType = atoi(argv[1]); + char* systemCode = argv[2]; + int unitCode = atoi(argv[3]); + int command = atoi(argv[4]); + char pSystemCode[14]; + + if (wiringPiSetup () == -1) return 1; + printf("sending systemCodeType[%i] systemCode[%s] unitCode[%i] command[%i] ...\n", systemCodeType, systemCode, unitCode, command); RCSwitch mySwitch = RCSwitch(); - mySwitch.setPulseLength(300); + printf("defining transmit PIN[%i] ... ",PIN); mySwitch.enableTransmit(PIN); - - for (i = 1; i < numberOfActuators + 1; i++) { - - int indexSystemCode = 0; - int indexUnitCode = 0; - - indexSystemCode = 2 + 2 * (i - 1); - indexUnitCode = 1 + 2 * i; - - systemCode = argv[indexSystemCode]; - unitCode = atoi(argv[indexUnitCode]); - - printf("sending systemCode[%s] unitCode[%i] command[%i]\n", systemCode, unitCode, command); - if (binaryMode) { - switch (command) { - case 1: - mySwitch.switchOnBinary(systemCode, unitCode); - break; + printf("success\n"); + printf("computing system Code Type ...\n"); + switch(systemCodeType) + { + case 1: + { + printf("Switching \"default\" system[%s] unit[%i] ... ", systemCode, unitCode); + switch(command) + { case 0: + { + printf("off\n"); mySwitch.switchOffBinary(systemCode, unitCode); break; + } + case 1: + { + printf("on\n"); + mySwitch.switchOnBinary(systemCode, unitCode); + break; + } default: + { printf("command[%i] is unsupported\n", command); - printUsage(); -// return -1; + return -1; + } } -// return 0; - } else { - switch (command) { - case 1: - mySwitch.switchOn(systemCode, unitCode); - break; + break; + } + case 2: + { + printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",systemCode, unitCode); + switch(atoi(systemCode)) + { + // house/family code A=1 - P=16 + case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } + case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } + case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } + case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } + case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } + case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } + case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } + case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } + case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } + case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } + case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } + case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } + case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } + case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } + case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } + case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } + default: + { + printf("systemCode[%s] is unsupported\n", systemCode); + return -1; + } + } + printf("got systemCode\n"); + switch(unitCode) + { + // unit/group code 01-16 + case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } + case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } + case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } + case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } + case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } + case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } + case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } + case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } + case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } + case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } + case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } + case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } + case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } + case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } + case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } + case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } + default: + { + printf("unitCode[%i] is unsupported\n", unitCode); + return -1; + } + } + strcat(pSystemCode,"0F"); // mandatory bits + switch(command) + { case 0: - mySwitch.switchOff(systemCode, unitCode); + { + strcat(pSystemCode,"F0"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); break; - case 2: - // 00001 2 on binary coded - mySwitch.send("010101010001000101010001"); - break; - case 3: - // 00001 2 on as TriState - mySwitch.sendTriState("FFFF0F0FFF0F"); + } + case 1: + { + strcat(pSystemCode,"FF"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); break; + } default: + { printf("command[%i] is unsupported\n", command); - printUsage(); -// return -1; + return -1; + } } -// return 0; + break; } - } - return 0; -} - -int main(int argc, char *argv[]) { - /** - * output PIN is hardcoded for testing purposes - * see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ - * for pin mapping of the raspberry pi GPIO connector - */ - int PIN = 0; - /** - * using old numbering mode by default, - * see RCSwitch.cpp for differences - */ - bool binaryMode = false; - char *systemCode; - int unitCode; - int command; - - - - printf("test1"); - if (argc > 5) { -// printf("test2"); - /** - * no matter, which mode is used, at least 4 arguments are required: - * 0: command name (send) - * [ - * 1: systemCode - * 2: unitCode - * ]*n - * 3: command - * if there are less arguments, the help should be printed - * and the application should terminate. - */ - multipleSwitch(argc, argv); - return 0; - } - - if (argc < 4) { - /** - * no matter, which mode is used, at least 4 arguments are required: - * 0: command name (send) - * 1: systemCode - * 2: unitCode - * 3: command - * if there are less arguments, the help should be printed - * and the application should terminate. - */ - printUsage(); - return 1; - } - - /** - * This needs to stand after the check of how many arguments are passed, - * because if there is only 1 argument passed, argv[1] is NULL, which - * will result in an error, because a std::string can't be constructed - * by a NULL value. Therefore it is important to terminate the application - * if there are less than 2 arguments (in this case: less than 4 arguments) - * passed. - */ - std::string firstArgument = argv[1]; - - if (firstArgument == "-b" or firstArgument == "--binary") { - if (argc < 5) { - /** - * in binaryMode, 5 arguments are required: - * 0: command name ('send') - * 1: binary operator ('-b') - * 2: systemCode - * 3: unitCode - * 4: command - * if there are less arguments, the help should be printed, - * and the application should terminate. - */ - printUsage(); - return 1; + default: + { + printf("command sequence unknown, aborting!\n"); + return -1; } - - printf("operating in binary mode...\n"); - binaryMode = true; - //position of data in input is now shifted by 1 because of '-b'-flag - systemCode = argv[2]; - unitCode = atoi(argv[3]); - command = atoi(argv[4]); - } else if (firstArgument == "-h" or firstArgument == "--help" or firstArgument == "-?") { - printUsage(); - } else { - //no binary mode, therefore using normal mode with old numbering - systemCode = argv[1]; - unitCode = atoi(argv[2]); - command = atoi(argv[3]); - } - - if (wiringPiSetup() == -1) return 1; - piHiPri(20); - printf("sending systemCode[%s] unitCode[%i] command[%i]\n", systemCode, unitCode, command); - RCSwitch mySwitch = RCSwitch(); - mySwitch.setPulseLength(300); - mySwitch.enableTransmit(PIN); - - if (binaryMode) { - switch (command) { - case 1: - mySwitch.switchOnBinary(systemCode, unitCode); - break; - case 0: - mySwitch.switchOffBinary(systemCode, unitCode); - break; - default: - printf("command[%i] is unsupported\n", command); - printUsage(); - return -1; - } - return 0; - } else { - switch (command) { - case 1: - mySwitch.switchOn(systemCode, unitCode); - break; - case 0: - mySwitch.switchOff(systemCode, unitCode); - break; - case 2: - // 00001 2 on binary coded - mySwitch.send("010101010001000101010001"); - break; - case 3: - // 00001 2 on as TriState - mySwitch.sendTriState("FFFF0F0FFF0F"); - break; - default: - printf("command[%i] is unsupported\n", command); - printUsage(); - return -1; - } - return 0; } + return 0; } From 4a27e9512108888681d8971be43999c52bd8fb9e Mon Sep 17 00:00:00 2001 From: Tim Louis Schrumpf Date: Mon, 15 Feb 2016 00:50:08 +0100 Subject: [PATCH 2/7] Middendrin --- daemon.cpp | 152 +++++++++++++++++++++--- send.cpp | 255 ++++++++++++++++++++-------------------- webinterface/config.php | 20 ++-- webinterface/index.php | 49 ++++---- 4 files changed, 297 insertions(+), 179 deletions(-) diff --git a/daemon.cpp b/daemon.cpp index ca10cb9..b900980 100644 --- a/daemon.cpp +++ b/daemon.cpp @@ -52,7 +52,7 @@ int main(int argc, char* argv[]) { usleep(50000); mySwitch.enableTransmit(0); - nPlugs=153; + nPlugs=1110; int nState[nPlugs]; nTimeout=0; memset(nState, 0, sizeof(nState)); @@ -98,27 +98,35 @@ int main(int argc, char* argv[]) { n = read(newsockfd,buffer,255); if (n < 0) error("ERROR reading from socket"); - +// +//Set Values +// printf("message: %s\n", buffer); - if (strlen(buffer) >= 8) { - for (int i=0; i<5; i++) { - nGroup[i] = buffer[i]; + if (strlen(buffer) >= 5) { + nSys = buffer[0]-48; + + + switch (nSys){ + //normal elro + case 1:{ + for (int i=1; i<6; i++) { + nGroup[i-1] = buffer[i]; } - nGroup[5] = '\0'; + nGroup[6] = '\0'; - for (int i=5;i<7; i++) { + for (int i=6;i<8; i++) { nSwitchNumber = buffer[i]-48; } - nAction = buffer[7]-48; + nAction = buffer[8]-48; nTimeout=0; - +printf("nSys: %i\n", nSys); printf("nGroup: %s\n", nGroup); printf("nSwitchNumber: %i\n", nSwitchNumber); printf("nAction: %i\n", nAction); - if (strlen(buffer) >= 9) nTimeout = buffer[8]-48; - if (strlen(buffer) >= 10) nTimeout = nTimeout*10+buffer[9]-48; + if (strlen(buffer) >= 10) nTimeout = buffer[9]-48; if (strlen(buffer) >= 11) nTimeout = nTimeout*10+buffer[10]-48; + if (strlen(buffer) >= 12) nTimeout = nTimeout*10+buffer[11]-48; /** * handle messages @@ -132,13 +140,14 @@ printf("nPlugs: %i\n", nPlugs); n = write(newsockfd,"2",1); } else { + send. switch (nAction) { /** * off */ case 0: //piThreadCreate(switchOff); - mySwitch.switchOff(nGroup, nSwitchNumber); + mySwitch.switchOffBinary(nGroup, nSwitchNumber); nState[nAddr] = 0; //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); sprintf(msg, "%d", nState[nAddr]); @@ -149,7 +158,7 @@ printf("nPlugs: %i\n", nPlugs); */ case 1: //piThreadCreate(switchOn); - mySwitch.switchOn(nGroup, nSwitchNumber); + mySwitch.switchOnBinary(nGroup, nSwitchNumber); nState[nAddr] = 1; //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); sprintf(msg, "%d", nState[nAddr]); @@ -166,6 +175,119 @@ printf("nPlugs: %i\n", nPlugs); } } } + + break; + } + //Intertechno + case 2:{ + for (int i=1; i<3; i++) { + nGroup[i-1] = buffer[i]; + } + nGroup[3] = '\0'; + for (int i=3;i<5; i++) { + nSwitchNumber = buffer[i]-48; + } + nAction = buffer[5]-48; + nTimeout=0; + printf("nSys: %i\n", nSys); + printf("nGroup: %s\n", nGroup); + printf("nSwitchNumber: %i\n", nSwitchNumber); + printf("nAction: %i\n", nAction); + int nAddr = getAddr(nGroup, nSwitchNumber); + printf("nAddr: %i\n", nAddr); + printf("nPlugs: %i\n", nPlugs); + char msg[13]; + if (nAddr > nPlugs) { + printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); + n = write(newsockfd,"2",1); + } + else { + printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",nGroup, nSwitchNumber); + char pSystemCode[14]; + switch(atoi(nGroup)) + { + // house/family code A=1 - P=16 + case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } + case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } + case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } + case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } + case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } + case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } + case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } + case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } + case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } + case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } + case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } + case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } + case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } + case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } + case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } + case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } + default: + { + printf("systemCode[%s] is unsupported\n", systemCode); + return -1; + } + } + printf("got systemCode\n"); + switch(nSwitchNumber) + { + // unit/group code 01-16 + case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } + case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } + case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } + case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } + case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } + case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } + case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } + case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } + case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } + case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } + case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } + case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } + case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } + case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } + case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } + case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } + default: + { + printf("unitCode[%i] is unsupported\n", nSwitchNumber); + return -1; + } + } + strcat(pSystemCode,"0F"); // mandatory bits + switch(nAction) + { + case 0: + { + strcat(pSystemCode,"F0"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + break; + } + case 1: + { + strcat(pSystemCode,"FF"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + break; + } + default: + { + printf("command[%i] is unsupported\n", command); + return -1; + } + } + break; + } + } + break; + } + default:{ + printf("wrong systemkey!"); + } + } + else { printf("message corrupted or incomplete"); } @@ -221,7 +343,7 @@ PI_THREAD(switchOn) { memcpy(tGroup, nGroup, sizeof(tGroup)); tSwitchNumber = nSwitchNumber; sleep(nTimeout*60); - mySwitch.switchOn(tGroup, tSwitchNumber); + mySwitch.switchOnBinary(tGroup, tSwitchNumber); return 0; } @@ -232,7 +354,7 @@ PI_THREAD(switchOff) { memcpy(tGroup, nGroup, sizeof(tGroup)); tSwitchNumber = nSwitchNumber; sleep(nTimeout*60); - mySwitch.switchOff(tGroup, tSwitchNumber); + mySwitch.switchOffBinary(tGroup, tSwitchNumber); return 0; } diff --git a/send.cpp b/send.cpp index f570701..de13459 100644 --- a/send.cpp +++ b/send.cpp @@ -10,137 +10,136 @@ #include int main(int argc, char *argv[]) { - - /* - output PIN is hardcoded for testing purposes - see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ - for pin mapping of the raspberry pi GPIO connector - */ - int PIN = 0; // GPIO-PIN 17 - int systemCodeType = atoi(argv[1]); - char* systemCode = argv[2]; - int unitCode = atoi(argv[3]); - int command = atoi(argv[4]); - char pSystemCode[14]; - - if (wiringPiSetup () == -1) return 1; - printf("sending systemCodeType[%i] systemCode[%s] unitCode[%i] command[%i] ...\n", systemCodeType, systemCode, unitCode, command); - RCSwitch mySwitch = RCSwitch(); - printf("defining transmit PIN[%i] ... ",PIN); - mySwitch.enableTransmit(PIN); - printf("success\n"); - printf("computing system Code Type ...\n"); - switch(systemCodeType) - { - case 1: + + /* + output PIN is hardcoded for testing purposes + see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ + for pin mapping of the raspberry pi GPIO connector + */ + + sendCode(atoi(argv[1]),argv[2],atoi(argv[3], atoi(argv[4]); + } + void sendCode (int systemCodeType, char* systemCode, int unitCode, int command){ + int PIN = 0; //GPIO-Pin 17 + if (wiringPiSetup () == -1) return 1; + printf("sending systemCodeType[%i] systemCode[%s] unitCode[%i] command[%i] ...\n", systemCodeType, systemCode, unitCode, command); + RCSwitch mySwitch = RCSwitch(); + printf("defining transmit PIN[%i] ... ",PIN); + mySwitch.enableTransmit(PIN); + printf("success\n"); + printf("computing system Code Type ...\n"); + switch(systemCodeType) + { + case 1: + { + printf("Switching \"default\" system[%s] unit[%i] ... ", systemCode, unitCode); + switch(command) + { + case 0: + { + printf("off\n"); + mySwitch.switchOffBinary(systemCode, unitCode); + break; + } + case 1: + { + printf("on\n"); + mySwitch.switchOnBinary(systemCode, unitCode); + break; + } + default: + { + printf("command[%i] is unsupported\n", command); + return -1; + } + } + break; + } + case 2: + { + printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",systemCode, unitCode); + char pSystemCode[14]; + switch(atoi(systemCode)) + { + // house/family code A=1 - P=16 + case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } + case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } + case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } + case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } + case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } + case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } + case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } + case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } + case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } + case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } + case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } + case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } + case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } + case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } + case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } + case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } + default: + { + printf("systemCode[%s] is unsupported\n", systemCode); + return -1; + } + } + printf("got systemCode\n"); + switch(unitCode) + { + // unit/group code 01-16 + case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } + case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } + case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } + case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } + case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } + case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } + case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } + case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } + case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } + case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } + case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } + case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } + case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } + case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } + case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } + case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } + default: + { + printf("unitCode[%i] is unsupported\n", unitCode); + return -1; + } + } + strcat(pSystemCode,"0F"); // mandatory bits + switch(command) { - printf("Switching \"default\" system[%s] unit[%i] ... ", systemCode, unitCode); - switch(command) - { - case 0: - { - printf("off\n"); - mySwitch.switchOffBinary(systemCode, unitCode); - break; - } - case 1: - { - printf("on\n"); - mySwitch.switchOnBinary(systemCode, unitCode); - break; - } - default: - { - printf("command[%i] is unsupported\n", command); - return -1; - } + case 0: + { + strcat(pSystemCode,"F0"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + break; + } + case 1: + { + strcat(pSystemCode,"FF"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + break; + } + default: + { + printf("command[%i] is unsupported\n", command); + return -1; + } } break; - } - case 2: - { - printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",systemCode, unitCode); - switch(atoi(systemCode)) - { - // house/family code A=1 - P=16 - case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } - case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } - case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } - case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } - case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } - case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } - case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } - case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } - case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } - case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } - case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } - case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } - case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } - case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } - case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } - case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } - default: - { - printf("systemCode[%s] is unsupported\n", systemCode); - return -1; - } - } - printf("got systemCode\n"); - switch(unitCode) - { - // unit/group code 01-16 - case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } - case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } - case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } - case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } - case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } - case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } - case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } - case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } - case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } - case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } - case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } - case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } - case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } - case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } - case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } - case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } - default: - { - printf("unitCode[%i] is unsupported\n", unitCode); - return -1; - } - } - strcat(pSystemCode,"0F"); // mandatory bits - switch(command) + } + default: { - case 0: - { - strcat(pSystemCode,"F0"); - mySwitch.sendTriState(pSystemCode); - printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); - break; - } - case 1: - { - strcat(pSystemCode,"FF"); - mySwitch.sendTriState(pSystemCode); - printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); - break; - } - default: - { - printf("command[%i] is unsupported\n", command); - return -1; - } + printf("command sequence unknown, aborting!\n"); + return -1; } - break; - } - default: - { - printf("command sequence unknown, aborting!\n"); - return -1; - } + } + return 0; } - return 0; -} diff --git a/webinterface/config.php b/webinterface/config.php index e909826..e18a053 100644 --- a/webinterface/config.php +++ b/webinterface/config.php @@ -10,7 +10,8 @@ /* * define ip address and port here */ -$target = '192.168.11.124'; +$source = $_SERVER['SERVER_ADDR']; +$target = '127.0.0.1'; $port = 11337; /* @@ -21,13 +22,12 @@ * */ $config=array( - array("00010", "01", "Schreibtisch"), - array("00010", "02", "Laptop"), - "", - "", - array("00001", "01", "Sofa Lampe"), - array("00001", "02", "Sofa Steckdose"), - array("00001", "03", "TV"), - array("00001", "04", "Verstaerker"), -) + array("1","00100","1","SD1"), + array("1","00100","2","SD2"), + array("1","00100","3","SD3"), + array("1","00100","4","SD4"), + array("1","00100","5","SD5"), + array("1","00100","6","SD6"), + array("2","7","2","LichtKueche"), + ?> diff --git a/webinterface/index.php b/webinterface/index.php index 8b72b54..b6e9b18 100644 --- a/webinterface/index.php +++ b/webinterface/index.php @@ -7,30 +7,17 @@ * */ - -function daemon_send($target, $port, $output) -{ - $fp = fsockopen($target, $port, $errno, $errstr, 30) or die("$errstr ($errno)\n"); - fwrite($fp, $output); - $state = ""; - while(!feof($fp)) - { - $state .= fgets($fp, 2); - } - fclose($fp); - return $state; -} - - /* * get configuration * don't forget to edit config.php */ -require 'config.php'; +include("config.php"); /* * get parameters */ + if (isset($_GET['sys'])) $nSys=$_GET['sys']; +else $nSys=""; if (isset($_GET['group'])) $nGroup=$_GET['group']; else $nGroup=""; if (isset($_GET['switch'])) $nSwitch=$_GET['switch']; @@ -46,11 +33,14 @@ function daemon_send($target, $port, $output) * then reload the webpage without parameters * except for delay */ -$output = $nGroup.$nSwitch.$nAction.$nDelay; -if (strlen($output) >= 8) { - daemon_send($target, $port, $output); +$output = $nSys.$nGroup.$nSwitch.$nAction.$nDelay; +if (strlen($output) >= 5) { + $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n"); + socket_bind($socket, $source) or die("Could not bind to socket\n"); + socket_connect($socket, $target, $port) or die("Could not connect to socket\n"); + socket_write($socket, $output, strlen ($output)) or die("Could not write output\n"); + socket_close($socket); header("Location: index.php?delay=$nDelay"); - exit(); } ?> @@ -99,14 +89,20 @@ function daemon_send($target, $port, $output) echo "\n"; foreach($config as $current) { if ($current != "") { - $ig = $current[0]; - $is = $current[1]; - $id = $current[2]; + $iSys = $current[0]; + $ig = $current[1]; + $is = $current[2]; + $id = $current[3]; if ($index%2 == 0) echo "\n"; - $output = $ig.$is."2"; - $state = daemon_send($target, $port, $output); + $socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP) or die("Could not create socket\n"); + socket_bind($socket, $source) or die("Could not bind to socket\n"); + socket_connect($socket, $target, $port) or die("Could not connect to socket\n"); + + $output = $iSys.$ig.$is."2"; + socket_write($socket, $output, strlen ($output)) or die("Could not write output\n"); + $state = socket_read($socket, 2048); if ($state == 0) { $color=" BGCOLOR=\"#C00000\""; $ia = 1; @@ -124,12 +120,13 @@ function daemon_send($target, $port, $output) echo "&action=".$ia; echo "&delay=".$nDelay."\">"; echo "

".$id."


"; - echo $ig.":".$is."
"; + echo $iSys.":".$ig.":".$is."
"; echo "switch ".$direction; echo ""; echo ""; echo "
\n"; echo "\n"; + socket_close($socket); } else { echo "\n"; From a21f9e8aba31a2f2b2d13ae8c1e23e438c84a1c1 Mon Sep 17 00:00:00 2001 From: Tim Louis Schrumpf Date: Mon, 15 Feb 2016 02:24:42 +0100 Subject: [PATCH 3/7] implemented Intertechno MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit better description tomorrow… --- daemon.cpp | 486 ++++++++++++++++++++-------------------- daemon.h | 1 + send.cpp | 20 +- webinterface/config.php | 18 +- webinterface/index.php | 1 + 5 files changed, 261 insertions(+), 265 deletions(-) mode change 100644 => 100755 send.cpp mode change 100644 => 100755 webinterface/index.php diff --git a/daemon.cpp b/daemon.cpp index b900980..59b971f 100644 --- a/daemon.cpp +++ b/daemon.cpp @@ -40,263 +40,257 @@ RCSwitch mySwitch; int main(int argc, char* argv[]) { - /** - * Setup wiringPi and RCSwitch - * set high priority scheduling - */ - if (wiringPiSetup () == -1) - return 1; - piHiPri(20); - mySwitch = RCSwitch(); - mySwitch.setPulseLength(300); - usleep(50000); - mySwitch.enableTransmit(0); - - nPlugs=1110; - int nState[nPlugs]; - nTimeout=0; - memset(nState, 0, sizeof(nState)); - - /** - * setup socket - */ - int sockfd, newsockfd, portno; - socklen_t clilen; - char buffer[256]; - struct sockaddr_in serv_addr, cli_addr; - int n; + /** + * Setup wiringPi and RCSwitch + * set high priority scheduling + */ + if (wiringPiSetup () == -1){ + return 1; + } + piHiPri(20); + mySwitch = RCSwitch(); + mySwitch.setPulseLength(300); + usleep(50000); + mySwitch.enableTransmit(0); + nPlugs=1110; + int nState[nPlugs]; + nTimeout=0; + memset(nState, 0, sizeof(nState)); - bzero((char *) &serv_addr, sizeof(serv_addr)); - portno = PORT; - serv_addr.sin_family = AF_INET; - serv_addr.sin_addr.s_addr = INADDR_ANY; - serv_addr.sin_port = htons(portno); - // receiving socket - sockfd = socket(AF_INET, SOCK_STREAM, 0); - if (sockfd < 0) - error("ERROR opening socket"); - if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0) - error("ERROR on binding"); + /** + * setup socket + */ + int sockfd, newsockfd, portno; + socklen_t clilen; + char buffer[256]; + struct sockaddr_in serv_addr, cli_addr; + int n; - // sending socket - newsockfd = socket(AF_INET, SOCK_STREAM, 0); - if (newsockfd < 0) + bzero((char *) &serv_addr, sizeof(serv_addr)); + portno = PORT; + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons(portno); + // receiving socket + sockfd = socket(AF_INET, SOCK_STREAM, 0); + if (sockfd < 0){ + error("ERROR opening socket"); + } + if (bind(sockfd, (struct sockaddr *) &serv_addr, sizeof(serv_addr)) < 0){ + error("ERROR on binding"); + } + // sending socket + newsockfd = socket(AF_INET, SOCK_STREAM, 0); + if (newsockfd < 0) error("ERROR opening socket"); - /* - * start listening - */ - while (true) { - listen(sockfd,5); - clilen = sizeof(cli_addr); - newsockfd = accept(sockfd, - (struct sockaddr *) &cli_addr, - &clilen); - if (newsockfd < 0) - error("ERROR on accept"); - bzero(buffer,256); - n = read(newsockfd,buffer,255); - if (n < 0) - error("ERROR reading from socket"); -// -//Set Values -// - printf("message: %s\n", buffer); - if (strlen(buffer) >= 5) { - nSys = buffer[0]-48; - - - switch (nSys){ - //normal elro - case 1:{ - for (int i=1; i<6; i++) { - nGroup[i-1] = buffer[i]; - } - nGroup[6] = '\0'; + /* + * start listening + */ + while (true) { + listen(sockfd,5); + clilen = sizeof(cli_addr); + newsockfd = accept(sockfd,(struct sockaddr *) &cli_addr,&clilen); + if (newsockfd < 0){ + error("ERROR on accept"); + } + bzero(buffer,256); + n = read(newsockfd,buffer,255); + if (n < 0){ + error("ERROR reading from socket"); + } + /* + * get values + */ + printf("message: %s\n", buffer); + if (strlen(buffer) >= 5) { + nSys = buffer[0]-48; + switch (nSys){ + //normal elro + case 1:{ + for (int i=1; i<6; i++) { + nGroup[i-1] = buffer[i]; + } + nGroup[6] = '\0'; + for (int i=6;i<8; i++) { + nSwitchNumber = buffer[i]-48; + } + nAction = buffer[8]-48; + nTimeout=0; + printf("nSys: %i\n", nSys); + printf("nGroup: %s\n", nGroup); + printf("nSwitchNumber: %i\n", nSwitchNumber); + printf("nAction: %i\n", nAction); - for (int i=6;i<8; i++) { - nSwitchNumber = buffer[i]-48; - } - nAction = buffer[8]-48; - nTimeout=0; -printf("nSys: %i\n", nSys); -printf("nGroup: %s\n", nGroup); -printf("nSwitchNumber: %i\n", nSwitchNumber); -printf("nAction: %i\n", nAction); + if (strlen(buffer) >= 10) nTimeout = buffer[9]-48; + if (strlen(buffer) >= 11) nTimeout = nTimeout*10+buffer[10]-48; + if (strlen(buffer) >= 12) nTimeout = nTimeout*10+buffer[11]-48; - if (strlen(buffer) >= 10) nTimeout = buffer[9]-48; - if (strlen(buffer) >= 11) nTimeout = nTimeout*10+buffer[10]-48; - if (strlen(buffer) >= 12) nTimeout = nTimeout*10+buffer[11]-48; + /** + * handle messages + */ + int nAddr = getAddr(nGroup, nSwitchNumber); + printf("nAddr: %i\n", nAddr); + printf("nPlugs: %i\n", nPlugs); + char msg[13]; + if (nAddr > nPlugs) { + printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); + n = write(newsockfd,"2",1); + } + else { + switch (nAction) { + //OFF + case 0:{ + //piThreadCreate(switchOff); + mySwitch.switchOffBinary(nGroup, nSwitchNumber); + nState[nAddr] = 0; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + //ON + case 1:{ + //piThreadCreate(switchOn); + mySwitch.switchOnBinary(nGroup, nSwitchNumber); + nState[nAddr] = 1; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + //STATUS + case 2:{ + sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + } + } + break; + } - /** - * handle messages - */ - int nAddr = getAddr(nGroup, nSwitchNumber); -printf("nAddr: %i\n", nAddr); -printf("nPlugs: %i\n", nPlugs); - char msg[13]; - if (nAddr > nPlugs) { - printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); - n = write(newsockfd,"2",1); - } - else { - send. - switch (nAction) { - /** - * off - */ - case 0: - //piThreadCreate(switchOff); - mySwitch.switchOffBinary(nGroup, nSwitchNumber); - nState[nAddr] = 0; - //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - sprintf(msg, "%d", nState[nAddr]); - n = write(newsockfd,msg,1); - break; - /** - * on - */ - case 1: - //piThreadCreate(switchOn); - mySwitch.switchOnBinary(nGroup, nSwitchNumber); - nState[nAddr] = 1; - //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - sprintf(msg, "%d", nState[nAddr]); - n = write(newsockfd,msg,1); - break; - /** - * status - */ - case 2: - sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); - sprintf(msg, "%d", nState[nAddr]); - n = write(newsockfd,msg,1); - break; - } - } - } - - break; - } - //Intertechno - case 2:{ - for (int i=1; i<3; i++) { - nGroup[i-1] = buffer[i]; - } - nGroup[3] = '\0'; - for (int i=3;i<5; i++) { - nSwitchNumber = buffer[i]-48; - } - nAction = buffer[5]-48; - nTimeout=0; - printf("nSys: %i\n", nSys); - printf("nGroup: %s\n", nGroup); - printf("nSwitchNumber: %i\n", nSwitchNumber); - printf("nAction: %i\n", nAction); - int nAddr = getAddr(nGroup, nSwitchNumber); - printf("nAddr: %i\n", nAddr); - printf("nPlugs: %i\n", nPlugs); - char msg[13]; - if (nAddr > nPlugs) { - printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); - n = write(newsockfd,"2",1); - } - else { - printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",nGroup, nSwitchNumber); - char pSystemCode[14]; - switch(atoi(nGroup)) - { + //Intertechno + case 2:{ + nGroup[0] = buffer[1]; + + nGroup[1] = '\0'; + for (int i=2;i<4; i++) { + nSwitchNumber = buffer[i]-48; + } + nAction = buffer[4]-48; + nTimeout=0; + printf("nSys: %i\n", nSys); + printf("nGroup: %s\n", nGroup); + printf("nSwitchNumber: %i\n", nSwitchNumber); + printf("nAction: %i\n", nAction); + int nAddr = getAddr(nGroup, nSwitchNumber); + printf("nAddr: %i\n", nAddr); + printf("nPlugs: %i\n", nPlugs); + char msg[13]; + if (nAddr > nPlugs) { + printf("Switch out of range: %s:%d\n", nGroup, nSwitchNumber); + n = write(newsockfd,"2",1); + } + else { + printf("computing systemcode for Intertechno Type B house[%s] unit[%i] ... ",nGroup, nSwitchNumber); + char pSystemCode[14]; + switch(atoi(nGroup)){ // house/family code A=1 - P=16 - case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } - case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } - case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } - case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } - case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } - case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } - case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } - case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } - case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } - case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } - case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } - case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } - case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } - case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } - case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } - case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } - default: - { - printf("systemCode[%s] is unsupported\n", systemCode); - return -1; + case 1: { printf("1/A ... "); strcpy(pSystemCode,"0000"); break; } + case 2: { printf("2/B ... "); strcpy(pSystemCode,"F000"); break; } + case 3: { printf("3/C ... "); strcpy(pSystemCode,"0F00"); break; } + case 4: { printf("4/D ... "); strcpy(pSystemCode,"FF00"); break; } + case 5: { printf("5/E ... "); strcpy(pSystemCode,"00F0"); break; } + case 6: { printf("6/F ... "); strcpy(pSystemCode,"F0F0"); break; } + case 7: { printf("7/G ... "); strcpy(pSystemCode,"0FF0"); break; } + case 8: { printf("8/H ... "); strcpy(pSystemCode,"FFF0"); break; } + case 9: { printf("9/I ... "); strcpy(pSystemCode,"000F"); break; } + case 10: { printf("10/J ... "); strcpy(pSystemCode,"F00F"); break; } + case 11: { printf("11/K ... "); strcpy(pSystemCode,"0F0F"); break; } + case 12: { printf("12/L ... "); strcpy(pSystemCode,"FF0F"); break; } + case 13: { printf("13/M ... "); strcpy(pSystemCode,"00FF"); break; } + case 14: { printf("14/N ... "); strcpy(pSystemCode,"F0FF"); break; } + case 15: { printf("15/O ... "); strcpy(pSystemCode,"0FFF"); break; } + case 16: { printf("16/P ... "); strcpy(pSystemCode,"FFFF"); break; } + default:{ + printf("systemCode[%s] is unsupported\n", nGroup); + return -1; + } } - } - printf("got systemCode\n"); - switch(nSwitchNumber) - { - // unit/group code 01-16 - case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } - case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } - case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } - case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } - case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } - case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } - case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } - case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } - case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } - case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } - case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } - case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } - case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } - case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } - case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } - case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } - default: - { - printf("unitCode[%i] is unsupported\n", nSwitchNumber); - return -1; - } - } - strcat(pSystemCode,"0F"); // mandatory bits - switch(nAction) - { - case 0: - { - strcat(pSystemCode,"F0"); - mySwitch.sendTriState(pSystemCode); - printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); - break; - } - case 1: - { - strcat(pSystemCode,"FF"); - mySwitch.sendTriState(pSystemCode); - printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); - break; - } - default: - { - printf("command[%i] is unsupported\n", command); - return -1; - } + printf("got systemCode[%s]\n",nGroup); + switch(nSwitchNumber){ + // unit/group code 01-16 + case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } + case 2: { printf("2 ... "); strcat(pSystemCode,"F000"); break; } + case 3: { printf("3 ... "); strcat(pSystemCode,"0F00"); break; } + case 4: { printf("4 ... "); strcat(pSystemCode,"FF00"); break; } + case 5: { printf("5 ... "); strcat(pSystemCode,"00F0"); break; } + case 6: { printf("6 ... "); strcat(pSystemCode,"F0F0"); break; } + case 7: { printf("7 ... "); strcat(pSystemCode,"0FF0"); break; } + case 8: { printf("8 ... "); strcat(pSystemCode,"FFF0"); break; } + case 9: { printf("9 ... "); strcat(pSystemCode,"000F"); break; } + case 10: { printf("10 ... "); strcat(pSystemCode,"F00F"); break; } + case 11: { printf("11 ... "); strcat(pSystemCode,"0F0F"); break; } + case 12: { printf("12 ... "); strcat(pSystemCode,"FF0F"); break; } + case 13: { printf("13 ... "); strcat(pSystemCode,"00FF"); break; } + case 14: { printf("14 ... "); strcat(pSystemCode,"F0FF"); break; } + case 15: { printf("15 ... "); strcat(pSystemCode,"0FFF"); break; } + case 16: { printf("16 ... "); strcat(pSystemCode,"FFFF"); break; } + default:{ + printf("unitCode[%i] is unsupported\n", nSwitchNumber); + return -1; + } + } + strcat(pSystemCode,"0F"); // mandatory bits + switch(nAction){ + case 0:{ + strcat(pSystemCode,"F0"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + nState[nAddr] = 0; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + case 1:{ + strcat(pSystemCode,"FF"); + mySwitch.sendTriState(pSystemCode); + printf("sent TriState signal: pSystemCode[%s]\n",pSystemCode); + nState[nAddr] = 1; + //sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + case 2:{ + sprintf(msg, "nState[%d] = %d", nAddr, nState[nAddr]); + sprintf(msg, "%d", nState[nAddr]); + n = write(newsockfd,msg,1); + break; + } + default:{ + printf("command[%i] is unsupported\n", nAction); + return -1; + } } - break; - } - } - break; - } - default:{ - printf("wrong systemkey!"); + } + break; + } + default:{ + printf("wrong systemkey!\n"); + } + } + } + else { + printf("message corrupted or incomplete"); + } + if (n < 0){ + error("ERROR writing to socket"); + close(newsockfd); } - } - - else { - printf("message corrupted or incomplete"); - } - - - if (n < 0) - error("ERROR writing to socket"); - - close(newsockfd); } /** diff --git a/daemon.h b/daemon.h index 05b1a6a..bcb8b12 100644 --- a/daemon.h +++ b/daemon.h @@ -1,6 +1,7 @@ #include char nGroup[6]; +int nSys; int nSwitchNumber; int nAction; int nPlugs; diff --git a/send.cpp b/send.cpp old mode 100644 new mode 100755 index de13459..29c01fa --- a/send.cpp +++ b/send.cpp @@ -11,17 +11,17 @@ int main(int argc, char *argv[]) { - /* - output PIN is hardcoded for testing purposes - see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ - for pin mapping of the raspberry pi GPIO connector - */ - - sendCode(atoi(argv[1]),argv[2],atoi(argv[3], atoi(argv[4]); - } - void sendCode (int systemCodeType, char* systemCode, int unitCode, int command){ + /* + output PIN is hardcoded for testing purposes + see https://projects.drogon.net/raspberry-pi/wiringpi/pins/ + for pin mapping of the raspberry pi GPIO connector + */ + int systemCodeType = atoi(argv[1]); + char* systemCode = argv[2]; + int unitCode = atoi(argv[3]); + int command = atoi(argv[4]); int PIN = 0; //GPIO-Pin 17 - if (wiringPiSetup () == -1) return 1; + if (wiringPiSetup () == -1) return 1; printf("sending systemCodeType[%i] systemCode[%s] unitCode[%i] command[%i] ...\n", systemCodeType, systemCode, unitCode, command); RCSwitch mySwitch = RCSwitch(); printf("defining transmit PIN[%i] ... ",PIN); diff --git a/webinterface/config.php b/webinterface/config.php index e18a053..c2d9880 100644 --- a/webinterface/config.php +++ b/webinterface/config.php @@ -10,7 +10,7 @@ /* * define ip address and port here */ -$source = $_SERVER['SERVER_ADDR']; +$source = $_SERVER['SERVER_ADDR']; $target = '127.0.0.1'; $port = 11337; @@ -22,12 +22,12 @@ * */ $config=array( - array("1","00100","1","SD1"), - array("1","00100","2","SD2"), - array("1","00100","3","SD3"), - array("1","00100","4","SD4"), - array("1","00100","5","SD5"), - array("1","00100","6","SD6"), - array("2","7","2","LichtKueche"), - + array("1", "00100", "01", "Steckdose1"), + array("1", "00100", "02", "Steckdose2"), + array("1", "00100", "03", "Steckdose3"), + array("1", "00100", "04", "Steckdose4"), + array("1", "00100", "05", "Steckdose5"), + array("1", "00100", "06", "Steckdose6"), + array("2", "7" , "02", "Kueche"), +) ?> diff --git a/webinterface/index.php b/webinterface/index.php old mode 100644 new mode 100755 index b6e9b18..0e7bae3 --- a/webinterface/index.php +++ b/webinterface/index.php @@ -116,6 +116,7 @@ echo "\n"; echo "
"; echo ""; From e1c1dd9d16e0946f991d57a6b2d2dd6ba573c001 Mon Sep 17 00:00:00 2001 From: tillepille Date: Mon, 15 Feb 2016 12:18:16 +0100 Subject: [PATCH 4/7] Works now with Intertechno 16-16 switches and binary coded elro. Added new param systemCodeType, ToDo: also add method for standard coded elro. --- daemon.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/daemon.cpp b/daemon.cpp index 59b971f..46bfa04 100644 --- a/daemon.cpp +++ b/daemon.cpp @@ -219,7 +219,7 @@ int main(int argc, char* argv[]) { return -1; } } - printf("got systemCode[%s]\n",nGroup); + printf("got systemCode[%s] ",nGroup); switch(nSwitchNumber){ // unit/group code 01-16 case 1: { printf("1 ... "); strcat(pSystemCode,"0000"); break; } From 39ae0a0227b297ce5eef0cbc3a414b0cee419cb1 Mon Sep 17 00:00:00 2001 From: Tim Louis Schrumpf Date: Tue, 16 Feb 2016 14:14:00 +0100 Subject: [PATCH 5/7] icons --- webinterface/apple-touch-icon-precomposed.png | Bin webinterface/apple-touch-icon.png | Bin webinterface/favicon.ico | Bin webinterface/favicon.png | Bin webinterface/style.css | 0 5 files changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 webinterface/apple-touch-icon-precomposed.png mode change 100644 => 100755 webinterface/apple-touch-icon.png mode change 100644 => 100755 webinterface/favicon.ico mode change 100644 => 100755 webinterface/favicon.png mode change 100644 => 100755 webinterface/style.css diff --git a/webinterface/apple-touch-icon-precomposed.png b/webinterface/apple-touch-icon-precomposed.png old mode 100644 new mode 100755 diff --git a/webinterface/apple-touch-icon.png b/webinterface/apple-touch-icon.png old mode 100644 new mode 100755 diff --git a/webinterface/favicon.ico b/webinterface/favicon.ico old mode 100644 new mode 100755 diff --git a/webinterface/favicon.png b/webinterface/favicon.png old mode 100644 new mode 100755 diff --git a/webinterface/style.css b/webinterface/style.css old mode 100644 new mode 100755 From 0483112ab3de4b4b8fe17b338fb3d1d60c4fd05b Mon Sep 17 00:00:00 2001 From: tillepille Date: Fri, 25 Mar 2016 18:48:24 +0100 Subject: [PATCH 6/7] updateFromGitHub --- README.md | 20 +++- send.cpp | 197 ++++++++++++++++++++++++++++++++++++++++ webinterface/config.php | 12 +-- 3 files changed, 215 insertions(+), 14 deletions(-) diff --git a/README.md b/README.md index f162c52..5c8b3ce 100644 --- a/README.md +++ b/README.md @@ -29,9 +29,15 @@ https://github.com/Panzenbaby/Raspberry-Remote-for-Windows-10-IoT-Core ## Usage Try if all is working with the send program -* Switch on single socket: `./send.cpp 00001 1 1` +* Switch on single socket: `./send 00001 1 1` +* Switch on multiple sockets: `./send 00001 1 00001 2 00001 3 1` -Pass the `-b`-option to use binary socket numbering instead of the common "only one switch up"-numbering. See [Binary Mode](#binary-mode) for further details. +### Options +* `-b`, `--binary`: Use binary socket numbering instead of the common "only one switch up"-numbering. See [Binary Mode](#binary-mode) for further details. +* `-p X`, `--pin=X` (X=pin number): Sets the pin number to use. Default is 0 in normal mode and 17 in [user mode](#user-mode). +* `-u`, `--user`: Run in user mode. This mode does not need root permissions, but the GPIO pin has to be exported beforehand using the `gpio` command. See [User Mode](#user-mode) for further details. +* `-s`, `--silent`: Disables all text output except for error messages. +* `-h`, `--help`: Display help. ## Binary Mode Most sockets available for purchase use the following numbering scheme: @@ -43,7 +49,7 @@ no. | address C | 00100 D | 00010 E | 00001 - + Of course, this doesn't make much sense, because it limits the maximum of supported sockets to 5 (or 6, if 00000 is included), and is less intuitive. Using real binary numbering would increase the limit of supported sockets per system to 31, and be more intutive. In binary mode, the sockets need to be numbered as below: no. | address @@ -56,9 +62,13 @@ no. | address 8 | 01000 16 | 10000 31 | 11111 - + Note that you need to configure your sockets to this kind of numbering to use this feature. This often includes that the dedicated remote that gets shipped with the sockets often is rendered useless, since it only supports the former way of numbering. - + +## User Mode +Use this mode if you want to use `send` without root permission. The pin must be exported with the `gpio` utility beforehand because it will be used via the `/sys/class/gpio` interface. The command for the default pin is `gpio export 17 out`. The user must be a member of the *gpio* group to access exported gpio pins!. +**Important Note:** pin numbering is different in this mode! While wiringPi uses its own numbering scheme in default mode, this mode requires the native Broadcom GPIO numbers (the default port 0 is 17 in this mode). See the [wiringPi documentation](http://wiringpi.com/pins/) for further details. + ## Daemon Use the daemon in combination with the webinterface * Copy the files in webinterface in your http directory diff --git a/send.cpp b/send.cpp index 29c01fa..74f2df1 100755 --- a/send.cpp +++ b/send.cpp @@ -7,6 +7,7 @@ #include "RCSwitch.h" #include #include +<<<<<<< HEAD #include int main(int argc, char *argv[]) { @@ -142,4 +143,200 @@ int main(int argc, char *argv[]) { } } return 0; +======= +#include +#include +//#include + +void printUsage() { + printf("This is rasperry remote, an application to control remote plugs with the\nRaspberry Pi.\n"); + printf("Based on RCSwitch and wiringPi. See github.com/xkonni/raspberry-remote for\nfurther reference.\n"); + printf("Usage: \n sudo send [options] [ ] \n or: sudo send -h\n\n"); + printf("Options:\n\n"); + printf(" -b, --binary:\n"); + printf(" Switches the instance to binary mode.\n"); + printf(" Binary mode means, that instead numbering the sockets by 00001 00010 00100\n"); + printf(" 01000 10000, the sockets are numbered in real binary numbers as following:\n"); + printf(" 00001 00010 00011 00100 00101 00110 and so on.\n"); + printf(" This means that your sockets need to be setup in this manner, which often\n"); + printf(" includes that the dedicated remote is rendered useless, but more than\n"); + printf(" 6 sockets are supported.\n\n"); + printf(" -h, --help:\n"); + printf(" displays this help\n\n"); + printf(" -p X, --pin=X\n"); + printf(" Sets the pin to use to communicate with the sender.\n"); + printf(" Important note: when running as root, pin numbers are wiringPi numbers while\n"); + printf(" in user mode (-u, --user) pin numbers are BCM_GPIO numbers.\n"); + printf(" See http://wiringpi.com/pins/ for details.\n"); + printf(" Default: 0 in normal mode, 17 in user mode\n\n"); + printf(" -s, --silent:\n"); + printf(" Don't print any text, except for errors\n\n"); + printf(" -u, --user:\n"); + printf(" Switches the instance to user mode.\n"); + printf(" In this mode this program does not need root privileges. It is required to\n"); + printf(" export the GPIO pin using the gpio utility. Note that pin numbers are\n"); + printf(" BCM_GPIO numbers in this mode! the export command for the default port is\n"); + printf(" \"gpio export 17 out\". For more information about port numbering see\n"); + printf(" http://wiringpi.com/pins/.\n\n"); +} + +int main(int argc, char *argv[]) { + bool silentMode = false; + bool binaryMode = false; + bool userMode = false; + int pin = 0; + int controlArgCount = 0; + char *systemCode; + int unitCode; + int command; + bool multiMode; + + int c; + while (1) { + static struct option long_options[] = + { + {"binary", no_argument, 0, 'b'}, + {"help", no_argument, 0, 'h'}, + {"pin", required_argument, 0, 'p'}, + {"silent", no_argument, 0, 's'}, + {"user", no_argument, 0, 'u'}, + 0 + }; + int option_index = 0; + + c = getopt_long (argc, argv, "bhp:su",long_options, &option_index); + /* Detect the end of the options. */ + if (c == -1) + break; + + switch (c) { + case 'b': + binaryMode = true; + break; + case 'p': + pin = atoi(optarg); + break; + case 's': + silentMode = true; + break; + case 'u': + userMode = true; + break; + case 'h': + printUsage(); + return 0; + } + } + + // When started in user mode, wiringPiSetupSys() is used. In this mode + // wiringPi uses the /sys/class/gpio interface and the GPIO pin are numbered + // by the native Broadcom GPIO numbers, where wiringPi-number 0 translates to + // bcm-number 17... so use this as default if no pin was set with -p + // reference: + // http://wiringpi.com/reference/setup/ + // http://wiringpi.com/pins/ + if (pin == 0 && userMode) { + pin = 17; + } + + controlArgCount = argc - optind; + // we need at least 3 args: systemCode, unitCode and command + if (controlArgCount >= 3) { + if (!silentMode) { + if (binaryMode) { + printf("operating in binary mode\n"); + } + if (userMode) { + printf("operating in user mode\n"); + } + printf("using pin %d\n", pin); + } + + char *controlArgs[controlArgCount]; + int i = 0; + while (optind < argc && i < controlArgCount) { + controlArgs[i] = argv[optind++]; + i++; + } + + multiMode = controlArgCount > 3; + + int numberOfActuators = (controlArgCount - 1) / 2; + // check if there are enough arguments supplied, we need (numberOfActuators * 2) + 1 + + if (controlArgCount != (numberOfActuators * 2) + 1) { + printf("invalid set of control arguments\nuse or\n"); + printf(" [ ...] \n"); + return 1; + } + + if (multiMode && !silentMode) { + printf("multi mode\n"); + } + + command = atoi(controlArgs[controlArgCount - 1]); + if (userMode) { + if (wiringPiSetupSys() == -1) return 1; + } else { + if (wiringPiSetup() == -1) return 1; + } + piHiPri(20); + RCSwitch mySwitch = RCSwitch(); + mySwitch.setPulseLength(300); + mySwitch.enableTransmit(pin); + + for (i = 0; i < numberOfActuators; i++) { + int indexSystemCode = i * 2; + int indexUnitCode = indexSystemCode + 1; + systemCode = controlArgs[indexSystemCode]; + unitCode = atoi(controlArgs[indexUnitCode]); + + if (!silentMode) { + printf("sending systemCode[%s] unitCode[%i] command[%i]\n", systemCode, unitCode, command); + } + if (binaryMode) { + switch (command) { + case 1: + mySwitch.switchOnBinary(systemCode, unitCode); + break; + case 0: + mySwitch.switchOffBinary(systemCode, unitCode); + break; + default: + printf("command[%i] is unsupported\n", command); + printUsage(); + if (!multiMode) { + return -1; + } + } + } else { + switch (command) { + case 1: + mySwitch.switchOn(systemCode, unitCode); + break; + case 0: + mySwitch.switchOff(systemCode, unitCode); + break; + case 2: + // 00001 2 on binary coded + mySwitch.send("010101010001000101010001"); + break; + case 3: + // 00001 2 on as TriState + mySwitch.sendTriState("FFFF0F0FFF0F"); + break; + default: + printf("command[%i] is unsupported\n", command); + printUsage(); + if (!multiMode) { + return -1; + } + } + } + } + return 0; + } else { + printUsage(); + return -1; +>>>>>>> xkonni/master } diff --git a/webinterface/config.php b/webinterface/config.php index c2d9880..0f70d53 100644 --- a/webinterface/config.php +++ b/webinterface/config.php @@ -16,18 +16,12 @@ /* * specify configuration of sockets to use - * array("group", "plug", "description"); + * array("systemcode", "group" , "plug", "description"); * use empty string to create empty box * "" * */ $config=array( - array("1", "00100", "01", "Steckdose1"), - array("1", "00100", "02", "Steckdose2"), - array("1", "00100", "03", "Steckdose3"), - array("1", "00100", "04", "Steckdose4"), - array("1", "00100", "05", "Steckdose5"), - array("1", "00100", "06", "Steckdose6"), - array("2", "7" , "02", "Kueche"), -) + array("1", "00100", "01", "PowerPlug1"), + ) ?> From c05c0bec0664b325f59f23186f5c6767b9a01929 Mon Sep 17 00:00:00 2001 From: tillepille Date: Fri, 25 Mar 2016 19:10:36 +0100 Subject: [PATCH 7/7] documentation --- daemon.cpp | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/daemon.cpp b/daemon.cpp index 46bfa04..3734073 100644 --- a/daemon.cpp +++ b/daemon.cpp @@ -7,7 +7,8 @@ * DATA to pin11/gpio0 * * Usage - * send xxxxxyyz to ip:port + * send axxxxxyyz to ip:port + * a systemcode. 1 for classic elro, 2 for Intertechno * xxxxx encoding * 00001 for first channel * yy plug @@ -17,13 +18,17 @@ * * Examples of remote actions * Switch plug 01 on 00001 to on - * echo 01111011 | nc localhost 11337 + * echo 101111011 | nc localhost 11337 * * Switch plug 01 on 00001 to off - * echo 01111010 | nc localhost 11337 + * echo 101111010 | nc localhost 11337 * * Get status of plug 01 on 00001 - * echo 01111012 | nc localhost 11337 + * echo 101111012 | nc localhost 11337 + * + * Switch intertechno plug 2 on group 2 to on + * echo 2221 | nc localhost 11337 + * */ #include @@ -218,7 +223,7 @@ int main(int argc, char* argv[]) { printf("systemCode[%s] is unsupported\n", nGroup); return -1; } - } + } printf("got systemCode[%s] ",nGroup); switch(nSwitchNumber){ // unit/group code 01-16