summaryrefslogtreecommitdiff
path: root/sensor.c
blob: c06bc2306136cbdbd455a244df7daa6bdaa7cda3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*
	Copyright (c) 2016 CurlyMo <curlymoo1@gmail.com>

  This Source Code Form is subject to the terms of the Mozilla Public
  License, v. 2.0. If a copy of the MPL was not distributed with this
  file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <unistd.h>
#include <time.h>

#include "wiringx.h"

void drop_privileges();

int main() {

	if(wiringXSetup("raspberrypi3", NULL) == -1) {
		wiringXGC();
		return -1;
	}

	pinMode(9, PINMODE_OUTPUT);
	digitalWrite(9, HIGH);
	pinMode(9, PINMODE_INPUT);
	pinMode(8, PINMODE_OUTPUT);

	pinMode(25, PINMODE_OUTPUT);

	drop_privileges();

	char data[5];
	int trials = 3;
	int bit = 0;
	while ((bit != 40) || ((data[0]+data[1]+data[2]+data[3]-data[4]) % 255 != 0) || ((data[0]|data[1]|data[2]|data[3]|data[4]) == 0)) {
		trials--;
		if (trials < 0) {
			fprintf(stderr, "Failed to obtain valid reading within 3 trials.\n");
			return -1;
		}
		if (trials < 2)
			sleep(2);

		memset(&data[0], 0, 5);

		// start bit
		digitalWrite(8, HIGH);
		usleep(18000);
		digitalWrite(8, LOW);

		struct timespec start_time, stop_time;
		int last_state = 1;
		long int low_ns = 0;
		// ignore first bit
		bit = -1;
		if (clock_gettime(CLOCK_MONOTONIC_RAW, &start_time) != 0) {
			perror("clock_gettime start_time failed");
			return -1;
		}
		if (clock_gettime(CLOCK_MONOTONIC_RAW, &stop_time) != 0) {
			perror("clock_gettime stop_time failed");
			return -1;
		}
		while ((!digitalRead(9)) && (stop_time.tv_sec - start_time.tv_sec + (stop_time.tv_nsec - start_time.tv_nsec) / 1000000000. < 0.0002)) {
			if (clock_gettime(CLOCK_MONOTONIC_RAW, &stop_time) != 0) {
				perror("clock_gettime stop_time failed");
				return -1;
			}
		}
		start_time.tv_sec = stop_time.tv_sec;
		start_time.tv_nsec = stop_time.tv_nsec;
		while (stop_time.tv_sec - start_time.tv_sec + (stop_time.tv_nsec - start_time.tv_nsec) / 1000000000. < 0.0002) {
			if (clock_gettime(CLOCK_MONOTONIC_RAW, &stop_time) != 0) {
				perror("clock_gettime stop_time failed");
				return -1;
			}
			if (last_state == digitalRead(9))
				continue;
			if (last_state == 1) {
				if ((bit >= 0) && ((stop_time.tv_sec - start_time.tv_sec) * 1000000000 + stop_time.tv_nsec - start_time.tv_nsec > low_ns))
					data[bit / 8] |= 1 << 7 - (bit % 8);
				bit++;
			} else {
				low_ns = (stop_time.tv_sec - start_time.tv_sec) * 1000000000 + stop_time.tv_nsec - start_time.tv_nsec;
			}
			start_time.tv_sec = stop_time.tv_sec;
			start_time.tv_nsec = stop_time.tv_nsec;
			last_state = 1-last_state;
		}
	}
	FILE *of;
	of = fopen("/srv/http/tmp/sensor", "w");
	if (of == NULL) {
		fprintf(stderr, "Failed to open '%s' for writing.\n", "/srv/http/tmp/sensor");
		return -1;
	}
	time_t now = time(NULL);
	char time_str[20];
	strftime(time_str, 20, "%Y-%m-%d %H:%M:%S", gmtime(&now));
	fprintf(of, "RH: %f %%\n", data[0] + data[1]/256.);
	fprintf(of, "T: %f °C\n", data[2] + data[3]/256.);
	fprintf(of, "t: %s\n", time_str);
	fclose(of);

	if (data[0] + data[1]/256. > 50.)
		digitalWrite(25, HIGH);
	else
		digitalWrite(25, LOW);
} // main

//
// Drop (root) privileges
//
void drop_privileges() {
	/*  Drop superuser privileges in correct order */

	int is_already_unprivileged = 0;

	if (setgid(65534) == -1) { // nobody
		perror("can't drop group privileges");
		if (! (is_already_unprivileged = (errno == 1)))
			exit(-1);
	}
	if (setuid(65534) == -1) { // nobody
		perror("can't drop user privileges");
		if (is_already_unprivileged != (errno == 1))
		exit(-1);
	}
} // drop_privileges