2021-10-16 19:08:32 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <wiringPi.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
2021-10-16 19:10:49 +02:00
|
|
|
#include <curl/curl.h>
|
|
|
|
|
2021-10-16 19:08:32 +02:00
|
|
|
#define MOTION_PIN 0
|
2021-10-16 19:10:49 +02:00
|
|
|
#define SECONDS_TO_OFF 5*60
|
2021-10-16 19:08:32 +02:00
|
|
|
|
|
|
|
int pin_read = 0;
|
|
|
|
int state = 1;
|
2021-10-16 19:10:49 +02:00
|
|
|
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();
|
|
|
|
}
|
2021-10-16 19:08:32 +02:00
|
|
|
|
|
|
|
int main(){
|
|
|
|
wiringPiSetup();
|
|
|
|
pinMode(MOTION_PIN, INPUT);
|
|
|
|
while(1){
|
|
|
|
pin_read = digitalRead(MOTION_PIN);
|
|
|
|
|
|
|
|
while(state && !pin_read){
|
|
|
|
counter += 1;
|
|
|
|
if(counter >= SECONDS_TO_OFF*2){
|
|
|
|
printf("Turning off\n");
|
2021-10-16 19:10:49 +02:00
|
|
|
//system("tvservice -o");
|
|
|
|
system("vcgencmd display_power 0");
|
|
|
|
light_control_script(0);
|
2021-10-16 19:08:32 +02:00
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
usleep(500*1000);
|
|
|
|
pin_read = digitalRead(MOTION_PIN);
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!state && pin_read){
|
|
|
|
printf("Turning on\n");
|
2021-10-16 19:10:49 +02:00
|
|
|
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");
|
2021-10-16 19:08:32 +02:00
|
|
|
state = 1;
|
|
|
|
}
|
|
|
|
counter = 0;
|
|
|
|
usleep(500*1000);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|