feucht

Sensor application for transmitting humidity values

git clone https://git.8pit.net/feucht.git

 1#include <stdio.h>
 2#include <stdlib.h>
 3#include <string.h>
 4#include <assert.h>
 5
 6#include "hdc1000.h"
 7#include "hdc1000_params.h"
 8
 9#include "net/ipv6/addr.h"
10#include "xtimer.h"
11#include "feucht.h"
12
13/**
14 * HDC1000 device.
15 */
16static hdc1000_t hdc;
17
18/* import "ifconfig" shell command, used for printing addresses */
19extern int _netif_config(int argc, char **argv);
20
21int
22main(void)
23{
24	int ret;
25	char buf[17];
26	int16_t temp, hum;
27	ipv6_addr_t remote;
28
29	if ((ret = hdc1000_init(&hdc, &hdc1000_params[0])) != HDC1000_OK) {
30		fprintf(stderr, "Couldn't initialize HDC1000: %d\n", ret);
31		return EXIT_FAILURE;
32	}
33
34	if (!ipv6_addr_from_str(&remote, FEUCHT_HOST)) {
35		fprintf(stderr, "Address '%s' is malformed\n", FEUCHT_HOST);
36		return EXIT_FAILURE;
37	}
38
39	puts("Waiting for address autoconfiguration...");
40	xtimer_sleep(3);
41
42	puts("Configured network interfaces:");
43	_netif_config(0, NULL);
44
45	puts("Initialize protocol backend...");
46	if ((ret = init_protocol(&remote))) {
47		fprintf(stderr, "init_protocol failed: %d\n", ret);
48		return EXIT_FAILURE;
49	}
50
51#ifdef FEUCHT_RUNS
52	for (int n = 1; n <= FEUCHT_RUNS; n++) {
53#else
54	for (;;) {
55#endif
56		if (hdc1000_read(&hdc, &temp, &hum) != HDC1000_OK) {
57			fprintf(stderr, "hdc1000_read failed\n");
58			continue;
59		}
60
61		memset(buf, '\0', sizeof(buf));
62		ret = snprintf(buf, sizeof(buf) - 1, "%d", hum);
63
64		assert(ret < sizeof(buf));
65		buf[ret] = '\n';
66
67		if ((ret = update_humidity(buf, ret + 1))) {
68			fprintf(stderr, "update_humidity failed: %d\n", ret);
69			continue;
70		}
71
72		xtimer_sleep(FEUCHT_INTERVAL);
73	}
74
75	free_protocol();
76	return EXIT_SUCCESS;
77}