Added a sensor example in C using polling
This commit is contained in:
parent
692fa85290
commit
1b98044f3d
2 changed files with 51 additions and 0 deletions
13
examples/c/sensors/polling/Makefile
Normal file
13
examples/c/sensors/polling/Makefile
Normal file
|
@ -0,0 +1,13 @@
|
|||
CC=gcc
|
||||
CFLAGS=-Wall -ltelldus-core
|
||||
|
||||
OBJS = main.o
|
||||
|
||||
all: ${OBJS}
|
||||
${CC} -o sensor ${CFLAGS} ${OBJS}
|
||||
|
||||
main.c:
|
||||
${CC} ${CFLAGS} -c main.c
|
||||
|
||||
clean:
|
||||
rm -f sensor main.o
|
38
examples/c/sensors/polling/main.c
Normal file
38
examples/c/sensors/polling/main.c
Normal file
|
@ -0,0 +1,38 @@
|
|||
#include <telldus-core.h>
|
||||
#include <time.h>
|
||||
#include <stdio.h>
|
||||
|
||||
const int DATA_LENGTH = 20;
|
||||
|
||||
int main(void) {
|
||||
char protocol[DATA_LENGTH], model[DATA_LENGTH];
|
||||
int sensorId = 0, dataTypes = 0;
|
||||
char value[DATA_LENGTH];
|
||||
char timeBuf[80];
|
||||
time_t timestamp = 0;
|
||||
|
||||
|
||||
tdInit();
|
||||
|
||||
while(tdSensor(protocol, DATA_LENGTH, model, DATA_LENGTH, &sensorId, &dataTypes) == TELLSTICK_SUCCESS) {
|
||||
//Print the sensor
|
||||
printf("%s,\t%s,\t%i\n", protocol, model, sensorId);
|
||||
|
||||
//Retrieve the values the sensor supports
|
||||
if (dataTypes & TELLSTICK_TEMPERATURE) {
|
||||
tdSensorValue(protocol, model, sensorId, TELLSTICK_TEMPERATURE, value, DATA_LENGTH, (int *)×tamp);
|
||||
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp));
|
||||
printf("Temperature:\t%sº\t(%s)\n", value, timeBuf);
|
||||
}
|
||||
if (dataTypes & TELLSTICK_HUMIDITY) {
|
||||
tdSensorValue(protocol, model, sensorId, TELLSTICK_HUMIDITY, value, DATA_LENGTH, (int *)×tamp);
|
||||
strftime(timeBuf, sizeof(timeBuf), "%Y-%m-%d %H:%M:%S", localtime(×tamp));
|
||||
printf("Humidity:\t%s%%\t(%s)\n", value, timeBuf);
|
||||
}
|
||||
printf("\n");
|
||||
}
|
||||
|
||||
tdClose();
|
||||
|
||||
return 0;
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue