Native Temperature, Humidity and Pressure Sensor in Contiki NG
Implementation of Three sensors namely temperature, humidity and pressure sensor in contiki NG OS.
We need four files namely
Step 1: Create a Folder called temperature/ in the folder contiki-ng/examples/
and then copy paste the following four files to the above folder.
// mytemp.h
#ifndef MYTEMP_H
#define MYTEMP_H
struct Sensor {
char name[15];
float value;
};
struct Sensor read_temperature();
struct Sensor read_humidity();
struct Sensor read_pressure();
#endif
// mytemp.c
#include "mytemp.h"
#include <string.h>
#include <stdlib.h>
float random_value(float min, float max)
{
float scale = rand() / (float) RAND_MAX;
return min + scale * (max - min);
}
struct Sensor read_temperature()
{
struct Sensor temp;
strncpy(temp.name, "Temperature", 15);
temp.value = random_value(0, 35);
return temp;
}
struct Sensor read_humidity()
{
struct Sensor humdidty;
strncpy(humdidty.name, "Humidity", 15);
humdidty.value = random_value(40, 80);
return humdidty;
}
struct Sensor read_pressure()
{
struct Sensor pressure;
strncpy(pressure.name, "Pressure", 15);
pressure.value = random_value(30, 78);
return pressure;
}
// demo.c
#include "contiki.h"
#include "sys/etimer.h" //etimer Event Timer
#include "mytemp.h"
#include <stdio.h>
PROCESS(sensor_process, "Sensor process");
AUTOSTART_PROCESSES(&sensor_process);
static struct etimer timer;
PROCESS_THREAD(sensor_process, ev, data)
{
PROCESS_BEGIN();
printf("Native Sensor\n");
while(1) {
etimer_set(&timer, CLOCK_SECOND*2);
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&timer));
struct Sensor temp = read_temperature();
printf("%s: %.2f\n", temp.name, (double)temp.value);
struct Sensor hum = read_humidity();
printf("%s: %.2f\n", hum.name, (double)hum.value);
struct Sensor pressure = read_pressure();
printf("%s: %.2f\n", pressure.name, (double)pressure.value);
printf("------------\n");
}
PROCESS_END();
}
#Makefile
CONTIKI_PROJECT = demo
all: $(CONTIKI_PROJECT)
PROJECT_SOURCEFILES += mytemp.c
CONTIKI = ../../
include $(CONTIKI)/Makefile.include
To run this File
Open a Terminal and go to contiki-ng/examples/temperature/
$] cd contiki-ng/examples/temperature
$] make TARGET=native
$] ./demo.native
Please see the screenshot below:
How to average them and print the average value through seperate process thread?
ReplyDeleteMay be you can calculate manually.
ReplyDelete