2021-10-16 19:08:32 +02:00
|
|
|
#include <unistd.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2021-10-17 13:44:59 +02:00
|
|
|
#include <stdint.h>
|
2021-10-16 19:08:32 +02:00
|
|
|
|
2021-10-17 13:44:59 +02:00
|
|
|
#include <wiringPi.h>
|
2021-10-16 19:10:49 +02:00
|
|
|
#include <curl/curl.h>
|
2021-10-17 13:44:59 +02:00
|
|
|
#include "TSL2561.h"
|
2021-10-16 19:10:49 +02:00
|
|
|
|
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
|
|
|
|
2021-10-17 13:44:59 +02:00
|
|
|
void setup_light_sensor(TSL2561* tsl) {
|
|
|
|
int rc = TSL2561_OPEN(tsl);
|
|
|
|
if(rc != 0) {
|
|
|
|
fprintf(stderr, "Error initializing TSL2561 sensor (%d). Check your i2c bus (es. i2cdetect)\n", tsl->lasterr);
|
|
|
|
// you don't need to TSL2561_CLOSE() if TSL2561_OPEN() failed, but it's safe doing it.
|
|
|
|
TSL2561_CLOSE(tsl);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
TSL2561_SETGAIN(tsl, TSL2561_GAIN_1X);
|
|
|
|
TSL2561_SETINTEGRATIONTIME(tsl, TSL2561_INTEGRATIONTIME_101MS);
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t read_light_level(TSL2561* tsl) {
|
|
|
|
uint32_t lux=0;
|
|
|
|
uint16_t broadband, ir;
|
|
|
|
TSL2561_SENSELIGHT(tsl, &broadband, &ir, &lux, 0);
|
|
|
|
return lux;
|
|
|
|
}
|
|
|
|
|
2021-10-16 19:08:32 +02:00
|
|
|
int main(){
|
|
|
|
wiringPiSetup();
|
|
|
|
pinMode(MOTION_PIN, INPUT);
|
2021-10-17 13:44:59 +02:00
|
|
|
|
|
|
|
TSL2561 light_sensor = TSL2561_INIT(1, TSL2561_ADDR_FLOAT);
|
|
|
|
setup_light_sensor(&light_sensor);
|
|
|
|
|
2021-10-16 19:08:32 +02:00
|
|
|
while(1){
|
|
|
|
pin_read = digitalRead(MOTION_PIN);
|
|
|
|
|
|
|
|
while(state && !pin_read){
|
2021-10-17 13:44:59 +02:00
|
|
|
counter += 1;
|
|
|
|
if(counter >= SECONDS_TO_OFF*2){
|
|
|
|
printf("Turning off\n");
|
|
|
|
//system("tvservice -o");
|
|
|
|
system("vcgencmd display_power 0");
|
|
|
|
light_control_script(0);
|
|
|
|
state = 0;
|
|
|
|
}
|
|
|
|
usleep(500*1000);
|
|
|
|
pin_read = digitalRead(MOTION_PIN);
|
2021-10-16 19:08:32 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if(!state && pin_read){
|
|
|
|
printf("Turning on\n");
|
2021-10-16 19:10:49 +02:00
|
|
|
system("vcgencmd display_power 1");
|
2021-10-17 13:44:59 +02:00
|
|
|
|
|
|
|
if(read_light_level(&light_sensor) < 10) {
|
|
|
|
light_control_script(1);
|
|
|
|
}
|
|
|
|
|
2021-10-16 19:10:49 +02:00
|
|
|
//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);
|
|
|
|
}
|
2021-10-17 13:44:59 +02:00
|
|
|
|
|
|
|
return 0;
|
2021-10-16 19:08:32 +02:00
|
|
|
}
|
|
|
|
|