From 1b98044f3d1130268103feaa861c8c9819a5f423 Mon Sep 17 00:00:00 2001 From: Micke Prag Date: Wed, 18 May 2011 11:12:10 +0000 Subject: [PATCH] Added a sensor example in C using polling --- examples/c/sensors/polling/Makefile | 13 ++++++++++ examples/c/sensors/polling/main.c | 38 +++++++++++++++++++++++++++++ 2 files changed, 51 insertions(+) create mode 100644 examples/c/sensors/polling/Makefile create mode 100644 examples/c/sensors/polling/main.c diff --git a/examples/c/sensors/polling/Makefile b/examples/c/sensors/polling/Makefile new file mode 100644 index 00000000..20509a12 --- /dev/null +++ b/examples/c/sensors/polling/Makefile @@ -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 diff --git a/examples/c/sensors/polling/main.c b/examples/c/sensors/polling/main.c new file mode 100644 index 00000000..953ef188 --- /dev/null +++ b/examples/c/sensors/polling/main.c @@ -0,0 +1,38 @@ +#include +#include +#include + +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; +}