int main(int argc, char *argv[]) { char *ip; char *host; int sock; struct sockaddr_in server; char server_reply[2000]; printf("OpenWRT smart home v1.00a\n"); printf("http://smarthome.russemotto.com\n\n"); if(argc <2) { printf("\nI need a hostname\n"); return 0; } // host name to ip host = argv[1]; struct hostent *h; if ((h=gethostbyname(host)) == NULL) { // get the host info herror("gethostbyname"); return 0; } ip = inet_ntoa(*((struct in_addr *)h->h_addr)); pinMode(29, OUTPUT); // set pin 29 as output // Does a long pull on a webserver to see if there are new commands while(1) { //Create socket sock = socket(AF_INET , SOCK_STREAM , 0); if (sock == -1) { printf("Could not create socket"); } server.sin_addr.s_addr = inet_addr(ip); server.sin_family = AF_INET; server.sin_port = htons(80); // Connect if (connect(sock , (struct sockaddr *)&server , sizeof(server)) < 0) { perror(" Error: connection failed."); } printf("Connected\n"); // Send the GET message to server char *message = malloc(26+strlen(host)+28); strcpy(message, "GET /LP HTTP/1.0\r\nHost: "); strcat(message, host); strcat(message, "\r\nUser-Agent: smarthome\r\n\r\n"); if( send(sock , message , strlen(message) , 0) < 0) { printf("Error: Send failed"); } //Wait for replay from server if( recv(sock , server_reply , 2000 , 0) >= 0) { puts("Server reply :"); puts(server_reply); int htmlstart = 0; char * htmlcontent; if(htmlstart == 0) { htmlcontent = strstr(server_reply, "\r\n\r\n"); if(htmlcontent != NULL){ htmlstart = 1; htmlcontent += 4; } } // Check if there was a command for us if (strncmp(htmlcontent, "on", 2) == 0) { printf("Turning on"); turnOn(2); } if (strncmp(htmlcontent, "off", 3) == 0) { printf("Turning off"); turnOff(2); } } close(sock); } return 0; }
void loop() { if (timing) { timerTime += millis() - lastTime; lastTime = millis(); } if (state == WAITING || state == TIMER_COUNTDOWN) { if (state == WAITING && timing) disableTiming(); if (readRFID()) { //Serial.print("RFID: "); //Serial.println(rfidNum); convertRFID(); if (serverRequest()) { state = SERVER_WAIT; enableTiming(); } else state = WAITING; } } else if (state == SERVER_WAIT){ //Serial.println(timerTime); if (timerTime < TIMEOUT) { if (readServerResponse()) { disableTiming(); parseResponse(); if (authorized) { state = TIMER_COUNTDOWN; turnOn(); } else state = DENIED; } } else { state = WAITING; } } else if (state == DENIED) { Serial.println("DENIED"); state = WAITING; } if (state == TIMER_COUNTDOWN && timing && currentOn()) { disableTiming(); } else if (state == TIMER_COUNTDOWN && (!timing && !currentOn())) { enableTiming(); } if (state == TIMER_COUNTDOWN) { if ((timing && (timerTime > authorizedTime)) || checkButton()) { state = WAITING; disableTiming(); turnOff(); } } /*if (timerTime > 0) Serial.println(timerTime);*/ }
void Relay::attach(uint8_t pin) { this->pin = pin; turnOff(); }
/* Input should look like: "remote.cgi 1 ON" */ int main(int argc, char *argv[]){ initialize(); // Check for correct number of arguments if(argc != 3){ return(-1); } int GPIO_num = atoi(argv[1]); if(strcmp(argv[2], "ON\0") == 0){ switch (GPIO_num){ case 1: GPIO_num = LIGHT1_ON; break; case 2: GPIO_num = LIGHT2_ON; break; case 3: GPIO_num = LIGHT3_ON; break; case 4: GPIO_num = LIGHT4_ON; break; case 5: GPIO_num = LIGHT5_ON; break; default: return -1; } turnOn(GPIO_num); } else if(strcmp(argv[2], "OFF\0") == 0){ switch (GPIO_num){ case 1: GPIO_num = LIGHT1_OFF; break; case 2: GPIO_num = LIGHT2_OFF; break; case 3: GPIO_num = LIGHT3_OFF; break; case 4: GPIO_num = LIGHT4_OFF; break; case 5: GPIO_num = LIGHT5_OFF; break; default: return(-1); } turnOff(GPIO_num); } else{ return(-1); } return(0); }
void HalRgbLed::disable() { turnOff(); enabled = false; }
int main(void) { //factory settings is to divide internal clock 8MHz by 8. //don't, and just run at 8 MHz (set the clock divider to 1 so no effect) CLKPR = (1<<CLKPCE); CLKPR = 0; // Divide by 1 //PB1 out, PB5 & PB0 in DDRB = 0x02; //Pullup on PB0 % PB5 PORTB = (1 << PORTB5) | (1 << PORTB0); //just make sure pullups are NOT disabled MCUCR |= (0 << PUD); setupWatchdog(); setup7seg(); int16_t vSpeed = 1000; while(1) { /* //1000 run makes it nearly 20 sec showWaitAnimation(vSpeed); vSpeed -= 20; if (vSpeed <= 0){ vSpeed = 1000; } */ if (mRemainingOnDuration > ONE_SEC * 60){ //more convert the seconds to minutes ('1+' to show remaining minutes rounded up) uint16_t vMinutes = 1+ (uint16_t)mRemainingOnDuration / (ONE_SEC * 60); //mul by 10 to shift for the display only (shows digits _XX_) vMinutes *= 10; showNumber(vMinutes, 1,2,false); } else { if (mRemainingOnDuration > ONE_SEC * 20){ //until 20 sec is countdown : show seconds as is showNumber((uint16_t)mRemainingOnDuration, 1,2,false); } else { if (mRemainingOnDuration > ONE_SEC){ //until last sec put the animation //1000 run makes it nearly 20 sec vSpeed = 1000; showWaitAnimation(vSpeed); vSpeed -= 20; } else { //zero! clearDisplay(); } } } //check for button press : PB0 -> Add 3 minutes if ((~PINB & (1 << PINB0)) != 0){ clearDisplay(); //pushed button : turns on if not already anyway and add and extra 3 minutes mRemainingOnDuration +=ONE_SEC * 180; //max is 10 minutes if (mRemainingOnDuration > MAX_DURATION_SEC){ mRemainingOnDuration = MAX_DURATION_SEC; } turnOn(); //wait while pressed and wait again 1/2 sec (debouncing) while ((~PINB & (1 << PINB0)) != 0); _delay_ms(300); } //check for button press : PB5 -> Stop now ! if ((~PINB & (1 << PINB5)) != 0){ clearDisplay(); //pushed button : turns off mRemainingOnDuration = 0; turnOff(); //wait while pressed and wait again 1/2 sec (debouncing) while ((~PINB & (1 << PINB5)) != 0); _delay_ms(300); } } }
void ActuatorRelay::begin(void) { pinMode(pin_,OUTPUT); turnOff(); }