diff --git a/daemon.cpp b/daemon.cpp index ca10cb9..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 @@ -40,141 +45,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=153; - int nState[nPlugs]; - nTimeout=0; - memset(nState, 0, sizeof(nState)); + /** + * 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; - - 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"); - - printf("message: %s\n", buffer); - if (strlen(buffer) >= 8) { - for (int i=0; i<5; i++) { - nGroup[i] = buffer[i]; - } - nGroup[5] = '\0'; - - for (int i=5;i<7; i++) { - nSwitchNumber = buffer[i]-48; - } - nAction = buffer[7]-48; - nTimeout=0; - -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) >= 11) nTimeout = nTimeout*10+buffer[10]-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.switchOff(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.switchOn(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; - } - } - } - else { - printf("message corrupted or incomplete"); - } + /* + * 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); + 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 (n < 0) - error("ERROR writing to socket"); + /** + * 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; + } - close(newsockfd); + //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", nGroup); + return -1; + } + } + printf("got systemCode[%s] ",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; + } + default:{ + printf("wrong systemkey!\n"); + } + } + } + else { + printf("message corrupted or incomplete"); + } + if (n < 0){ + error("ERROR writing to socket"); + close(newsockfd); + } } /** @@ -221,7 +342,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 +353,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/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 ab5680a..1b06e85 --- a/send.cpp +++ b/send.cpp @@ -201,4 +201,4 @@ int main(int argc, char *argv[]) { printUsage(); return -1; } -} +} \ No newline at end of file diff --git a/sendOld.cpp b/sendOld.cpp new file mode 100755 index 0000000..942578f --- /dev/null +++ b/sendOld.cpp @@ -0,0 +1,437 @@ +/* + 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 + +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 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; + 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) + { + 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; + } + default: + { + printf("command sequence unknown, aborting!\n"); + return -1; + } + } + 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; +<<<<<<< HEAD + + 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; + } + } + +======= + + 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; + } + } + +>>>>>>> xkonni/master + // 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; +<<<<<<< HEAD + } + + 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; +======= +>>>>>>> xkonni/master + } + piHiPri(20); + RCSwitch mySwitch = RCSwitch(); + mySwitch.setPulseLength(300); + mySwitch.enableTransmit(pin); + +<<<<<<< HEAD + 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; + } + } + } + } +======= + 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; + } + } + } + } +>>>>>>> xkonni/master + return 0; + } else { + printUsage(); + return -1; +<<<<<<< HEAD +>>>>>>> xkonni/master +======= +>>>>>>> xkonni/master + } 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/config.php b/webinterface/config.php index e909826..0f70d53 100644 --- a/webinterface/config.php +++ b/webinterface/config.php @@ -10,24 +10,18 @@ /* * define ip address and port here */ -$target = '192.168.11.124'; +$source = $_SERVER['SERVER_ADDR']; +$target = '127.0.0.1'; $port = 11337; /* * 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("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", "01", "PowerPlug1"), + ) ?> 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/index.php b/webinterface/index.php old mode 100644 new mode 100755 index 8b72b54..0e7bae3 --- 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; @@ -120,16 +116,18 @@ function daemon_send($target, $port, $output) echo "\n"; + socket_close($socket); } else { echo "\n"; diff --git a/webinterface/style.css b/webinterface/style.css old mode 100644 new mode 100755
\n"; echo ""; echo "
"; echo ""; echo "

".$id."


"; - echo $ig.":".$is."
"; + echo $iSys.":".$ig.":".$is."
"; echo "switch ".$direction; echo "
"; echo "
\n"; echo "