motion-sensor/motion.c
2022-10-31 14:06:27 +01:00

122 lines
3.6 KiB
C

#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <wiringPi.h>
#include <curl/curl.h>
#include "TSL2561.h"
#define MOTION_PIN 0
#define SECONDS_TO_OFF 5*60
int pin_read = 0;
int state = 1;
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.eyJpc3MiOiIyNDdkMTIxOGY5YmM0MTQ0ODcxYjE2YzRlNzYxNmE2NSIsImlhdCI6MTY0MTI1MTU1OSwiZXhwIjoxOTU2NjExNTU5fQ.OXI80PmYRgGS1HdI7a2cp3jJi-t3c-AQd7Et3CgYqZQ");
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();
}
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;
}
int main(){
wiringPiSetup();
pinMode(MOTION_PIN, INPUT);
TSL2561 light_sensor = TSL2561_INIT(1, TSL2561_ADDR_FLOAT);
setup_light_sensor(&light_sensor);
while(1){
pin_read = digitalRead(MOTION_PIN);
while(state && !pin_read){
counter += 1;
if(counter >= SECONDS_TO_OFF*2){
printf("Turning off\n");
system("xrandr --output HDMI-1 --off");
light_control_script(0);
state = 0;
}
usleep(500*1000);
pin_read = digitalRead(MOTION_PIN);
}
if(!state && pin_read){
printf("Turning on\n");
system("xrandr --output HDMI-1 --auto");
if(read_light_level(&light_sensor) < 10) {
light_control_script(1);
}
state = 1;
}
counter = 0;
usleep(500*1000);
}
return 0;
}