diff --git a/motion.c b/motion.c index e192a02..d6b25a1 100644 --- a/motion.c +++ b/motion.c @@ -3,26 +3,75 @@ #include #include +#include + #define MOTION_PIN 0 -#define SECONDS_TO_OFF 30 //10*60 +#define SECONDS_TO_OFF 5*60 int pin_read = 0; int state = 1; -int counter = 0; +unsigned long counter = 0; + +size_t curl_callback(void *dest, size_t size, size_t nmemb, void *userp) { + //This is only to "process" the body of the HTTP Post. curl will not return else. + return 0; +} +size_t curl_output(void *buffer, size_t size, size_t nmemb, void *userp) { + //This is to hide the output. + return size * nmemb; +} + +void light_control_script(int turn_on) { + CURL *curl; + CURLcode res = curl_global_init(CURL_GLOBAL_ALL); + + if(res != CURLE_OK) { + printf("curl init failed\n"); + return; + } + + curl = curl_easy_init(); + + if(curl) { + if(turn_on) { + printf("Running light on script\n"); + curl_easy_setopt(curl, CURLOPT_URL, "https://hass.c0ntroller.de/api/services/script/light_kitchen_on"); + } else { + printf("Running light off script\n"); + curl_easy_setopt(curl, CURLOPT_URL, "https://hass.c0ntroller.de/api/services/script/light_kitchen_off"); + } + curl_easy_setopt(curl, CURLOPT_POST, 1L); + curl_easy_setopt(curl, CURLOPT_READFUNCTION, curl_callback); + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, curl_output); + curl_easy_setopt(curl, CURLOPT_VERBOSE, 0L); + struct curl_slist *chunk = NULL; + chunk = curl_slist_append(chunk, "Authorization: Bearer eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiIwYzBjYzMzOWZmOGQ0NjBiYmJiYzI3NjA2M2U1MjhjNCIsImlhdCI6MTYwMzcxMDAzMywiZXhwIjoxOTE5MDcwMDMzfQ.HSnBiBNG6x6b1AAgh6AEAUALj4uSWfv-y-YS7QrNve0"); + curl_easy_setopt(curl, CURLOPT_HTTPHEADER, chunk); + curl_easy_setopt(curl, CURLOPT_TIMEOUT, 1L); + + res = curl_easy_perform(curl); + if(res != CURLE_OK) { + fprintf(stderr, "curl_easy_perform() failed: %s\n",curl_easy_strerror(res)); + } + curl_easy_cleanup(curl); + curl_slist_free_all(chunk); + } + curl_global_cleanup(); +} int main(){ wiringPiSetup(); pinMode(MOTION_PIN, INPUT); while(1){ pin_read = digitalRead(MOTION_PIN); - //printf("%d\n",pin_read); while(state && !pin_read){ - //printf("%d\n",counter); counter += 1; if(counter >= SECONDS_TO_OFF*2){ printf("Turning off\n"); - system("tvservice -o"); + //system("tvservice -o"); + system("vcgencmd display_power 0"); + light_control_script(0); state = 0; } usleep(500*1000); @@ -31,14 +80,16 @@ int main(){ if(!state && pin_read){ printf("Turning on\n"); - system("tvservice -p"); - system("fbset -depth 8"); - usleep(50*1000); - system("fbset -depth 16"); - usleep(50*1000); - system("fbset -depth 24"); - usleep(50*1000); - system("fbset -depth 32"); + light_control_script(1); + system("vcgencmd display_power 1"); + //system("tvservice -p"); + //system("fbset -depth 8"); + //usleep(50*1000); + //system("fbset -depth 16"); + //usleep(50*1000); + //system("fbset -depth 24"); + //usleep(50*1000); + //system("fbset -depth 32"); state = 1; } counter = 0;