22#include <linux/types.h>
26#include "../inc/database.h"
27#include "../inc/microphone.h"
28#include "../inc/livestream.h"
29#include "../inc/motor.h"
30#include "../inc/dht.h"
32#define SHMEMOBJ_NAME "/shLocalDaemon"
33#define MSGQOBJ_NAME "/mqLocalDaemon"
40#define updateDatabasePrio 3
43#define motorTimeout 10
44#define sensorSample 10
66void initThread(
int priority, pthread_attr_t *pthread_attr,
struct sched_param *pthread_param);
89 printf(
"Motor flag was updated in the database.\n");
94 printf(
"Notification flag was updated in the database.\n");
99 printf(
"Terminating program...\n");
106 if(Py_IsInitialized())
122 float temperature = 0;
125 u_int8_t samplingTries = 0;
134 printf(
"Sensor sampling successful:\n");
140 fprintf(stderr,
"Failed to sample sensor...Trying again in 5 seconds.\n");
143 }
while(samplingTries > 0 && samplingTries < 3);
147 temperature = sensorSampling.TemperatureI + (float)(sensorSampling.TemperatureD)/100;
148 humidity = sensorSampling.HumidityI + (float)(sensorSampling.HumidityD)/100;
150 printf(
"\tTemperature: %.2f \ÂșC \n \tHumidity: %.2f \%\n", temperature, humidity);
162 else if(samplingTries == 3)
165 fprintf(stderr,
"Failed to sample sensor 3 times. Ignoring...\n");
183 printf(
"Livestream has started.\n");
188 printf(
"Livestream has stopped.\n");
198 struct itimerval itv;
205 itv.it_interval.tv_sec = 8;
206 itv.it_interval.tv_usec = 0;
207 itv.it_value.tv_sec = 8;
208 itv.it_value.tv_usec = 0;
209 setitimer (ITIMER_REAL, &itv, NULL);
210 printf(
"Motor has started.\n");
213 fprintf(stderr,
"Error when starting motor.\n");
219 itv.it_interval.tv_sec = 0;
220 itv.it_interval.tv_usec = 0;
221 itv.it_value.tv_sec = 0;
222 itv.it_value.tv_usec = 0;
223 setitimer (ITIMER_REAL, &itv, NULL);
224 printf(
"Motor has stopped.\n");
227 fprintf(stderr,
"Error when stopping motor.\n");
242 _Bool dMotorFlag, dStreamFlag;
247 fprintf(stderr,
"In initDatabase()\n");
261 printf(
"Temperature and Humidity were updated in the database.\n");
284 printf(
"Sending livestream flag update to daemon...\n");
288 fprintf(stderr,
"malloc() got error.\n");
297 ret = mq_send(
msgq_id, msg, strlen(msg)+1, 1);
300 fprintf(stderr,
"mq_send() got error %d\n",ret);
317int main (
int argc,
char *argv[])
329 msgq_id = mq_open(
MSGQOBJ_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRWXU | S_IRWXG | S_IRWXO, NULL);
332 perror(
"In mq_open()");
336 pid_t my_pid = getpid();
337 printf(
"PID: %d\n",my_pid);
344 fd = shm_open(
SHMEMOBJ_NAME, O_RDWR | O_CREAT | O_EXCL, S_IRUSR|S_IWUSR|S_IRGRP|S_IWGRP|S_IROTH|S_IWOTH);
348 fprintf(stderr,
"Error %s in shm_open()\n",errno);
351 ftruncate(fd,
sizeof(pid_t));
353 ptr = mmap(0,
sizeof(pid_t),PROT_WRITE | PROT_READ, MAP_SHARED, fd, 0);
354 if(ptr == (
void *)-1)
358 fprintf(stderr,
"Error %s in mmap()\n",errno);
361 sprintf(ptr,
"%d", my_pid);
366 setenv(
"PYTHONPATH",
".",1);
371 system(
"./daemon.elf");
377 ret = munmap(0,
sizeof(pid_t));
382 fprintf(stderr,
"Error %s in munmap()\n",errno);
390 fprintf(stderr,
"Error %s in shm_unlink()\n",errno);
425 pthread_attr_t thread_attr;
426 struct sched_param thread_param;
428 pthread_t readSensorID, StartStopStreamID, updateDatabaseID, StartStopMotorID;
430 pthread_attr_init (&thread_attr);
431 pthread_attr_getschedparam (&thread_attr, &thread_param);
434 anyError = pthread_create (&readSensorID, &thread_attr,
tReadSensor, NULL);
438 anyError = pthread_create (&StartStopStreamID, &thread_attr,
tStartStopStream, NULL);
442 anyError = pthread_create (&StartStopMotorID, &thread_attr,
tStartStopMotor, NULL);
446 anyError = pthread_create (&updateDatabaseID, &thread_attr,
tUpdateDatabase, NULL);
449 pthread_detach (updateDatabaseID);
450 pthread_detach(StartStopStreamID);
451 pthread_detach (StartStopMotorID);
452 pthread_detach (readSensorID);
457void initThread(
int priority, pthread_attr_t *pthread_attr,
struct sched_param *pthread_param)
459 int min = sched_get_priority_min (SCHED_RR);
460 int max = sched_get_priority_max (SCHED_RR);
462 if(priority < min || priority > max)
464 fprintf(stderr,
"Thread priorities not valid.\n");
465 perror(
"initThread()");
468 pthread_param->sched_priority = priority;
469 pthread_attr_setschedparam(&pthread_attr, &pthread_param);
476 fprintf(stderr,
"pthread_create() got error %d\n",status);
478 perror(
"pthread_create()");
int send_swing_flag(int swing_flag)
Update swing flag in database.
int send_notification_flag(int notification_flag)
Update notification flag in database.
int send_temp_hum(float temp, float hum)
Update temperature and humidity in database.
int initDatabase()
Initializes all the functions.
int get_swing_flag()
Get the swing flag from database.
int get_live_flag()
Get the live flag from database.
void remDHT11(void)
Removes the sensor device driver.
int readDHT11(dht11_t *data)
Sample from the sensor.
void initDHT11(void)
Loads the sensor device driver.
_Bool getStreamStatus()
Get the Streaming Status object.
void stopLivestream()
Stop the livestream.
void startLivestream()
Start the livestream.
void initThread(int priority, pthread_attr_t *pthread_attr, struct sched_param *pthread_param)
Initializes the thread parameters with defined priority.
void * tStartStopStream(void *arg)
Controls the livestream depending on livestream flag value.
float databaseTemperature
pthread_mutex_t streamFlag_mutex
static void signalHandler(int signo)
Proces signal handler.
void * tUpdateDatabase(void *arg)
Update values to/from database.
void * tStartStopMotor(void *arg)
Controls the motor depending on swing flag value.
#define updateDatabasePrio
void * tReadSensor(void *arg)
Reads from temperature and humidity sensor and stores values.
pthread_mutex_t motorFlag_mutex
pthread_mutex_t sensorFlag_mutex
void checkErrors(int status)
Check for errors upon creating threads.
void initMotor()
Load the motor driver device driver.
int stopMotor()
Stop the motor.
void remMotor()
Remove the motor driver device driver.
int startMotor()
Start the motor.
_Bool getMotorStatus()
Get the Motor Status object.
DHT11 Data structure CompleteSample: receives all the data from the sensor.