2016-01-17 00:04:32 +01:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2016, Zolertia - http://www.zolertia.com
|
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* 1. Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* 2. Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in the
|
|
|
|
* documentation and/or other materials provided with the distribution.
|
|
|
|
* 3. Neither the name of the Institute nor the names of its contributors
|
|
|
|
* may be used to endorse or promote products derived from this software
|
|
|
|
* without specific prior written permission.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE INSTITUTE AND CONTRIBUTORS ``AS IS'' AND
|
|
|
|
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
|
|
|
|
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
|
|
|
|
* ARE DISCLAIMED. IN NO EVENT SHALL THE INSTITUTE OR CONTRIBUTORS BE LIABLE
|
|
|
|
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
|
|
|
|
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
|
|
|
|
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
|
|
|
|
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
|
|
|
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
|
|
|
|
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* \addtogroup zoul-examples
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \defgroup zoul-weather-meter-test Test the Sparkfun's weather meter
|
|
|
|
*
|
|
|
|
* The example application shows how to read data from the anemometer, wind vane
|
|
|
|
* and rain gauge, on board the Sparkfun's weater meter
|
|
|
|
*
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* \file
|
2016-09-01 16:08:01 +02:00
|
|
|
* Test application for the Sparkfun's weather meter
|
2016-01-17 00:04:32 +01:00
|
|
|
*
|
|
|
|
* \author
|
|
|
|
* Antonio Lignan <alinan@zolertia.com>
|
|
|
|
*/
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
#include "contiki.h"
|
|
|
|
#include "cpu.h"
|
|
|
|
#include "sys/etimer.h"
|
|
|
|
#include "dev/leds.h"
|
|
|
|
#include "dev/weather-meter.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdint.h>
|
|
|
|
/*---------------------------------------------------------------------------*/
|
2016-01-17 02:22:19 +01:00
|
|
|
#define READ_SENSOR_PERIOD CLOCK_SECOND
|
2016-01-17 14:56:12 +01:00
|
|
|
#define ANEMOMETER_THRESHOLD_TICK 13 /**< 16 Km/h */
|
|
|
|
#define RAIN_GAUGE_THRESHOLD_TICK 15 /**< each increment of 4.19 mm */
|
2016-01-17 00:04:32 +01:00
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static struct etimer et;
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS(test_weather_meter_sensors, "Test Weather meter sensors");
|
|
|
|
AUTOSTART_PROCESSES(&test_weather_meter_sensors);
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2016-01-17 02:22:19 +01:00
|
|
|
rain_callback(uint16_t value)
|
2016-01-17 00:04:32 +01:00
|
|
|
{
|
2016-01-17 14:56:12 +01:00
|
|
|
/* To calculate ticks from mm of rain, divide by 0.2794 mm */
|
2016-01-17 02:22:19 +01:00
|
|
|
printf("*** Rain gauge over threshold (%u ticks)\n", value);
|
|
|
|
weather_meter.configure(WEATHER_METER_RAIN_GAUGE_INT_OVER,
|
|
|
|
(value + RAIN_GAUGE_THRESHOLD_TICK));
|
2016-01-17 00:04:32 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
static void
|
2016-01-17 02:22:19 +01:00
|
|
|
wind_speed_callback(uint16_t value)
|
2016-01-17 00:04:32 +01:00
|
|
|
{
|
2016-01-17 14:56:12 +01:00
|
|
|
/* This checks for instant wind speed values (over a second), the minimum
|
|
|
|
* value is 1.2 Km/h (one tick), as the reference is 2.4KM/h per rotation, and
|
|
|
|
* the anemometer makes 2 ticks per rotation. Instant speed is calculated as
|
|
|
|
* multiples of this, so if you want to check for 16Km/h, then it would be 13
|
|
|
|
* ticks
|
|
|
|
*/
|
2016-01-17 02:22:19 +01:00
|
|
|
printf("*** Wind speed over threshold (%u ticks)\n", value);
|
2016-01-17 00:04:32 +01:00
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
PROCESS_THREAD(test_weather_meter_sensors, ev, data)
|
|
|
|
{
|
|
|
|
PROCESS_BEGIN();
|
|
|
|
|
|
|
|
static uint32_t rain;
|
|
|
|
static uint16_t wind_speed;
|
|
|
|
static uint16_t wind_dir;
|
2016-01-17 21:34:52 +01:00
|
|
|
static uint16_t wind_dir_avg_2m;
|
2016-01-17 14:56:12 +01:00
|
|
|
static uint16_t wind_speed_avg;
|
|
|
|
static uint16_t wind_speed_avg_2m;
|
|
|
|
static uint16_t wind_speed_max;
|
2016-01-17 00:04:32 +01:00
|
|
|
|
2016-01-19 18:00:18 +01:00
|
|
|
printf("Weather meter test example, integration period %u\n",
|
|
|
|
WEATHER_METER_AVG_PERIOD);
|
|
|
|
|
2016-01-17 00:04:32 +01:00
|
|
|
/* Register the callback handler when thresholds are met */
|
|
|
|
WEATHER_METER_REGISTER_ANEMOMETER_INT(wind_speed_callback);
|
|
|
|
WEATHER_METER_REGISTER_RAIN_GAUGE_INT(rain_callback);
|
|
|
|
|
2016-01-17 02:22:19 +01:00
|
|
|
/* Enable the sensors, this has to be called before any of the interrupt calls
|
|
|
|
* like the ones below
|
|
|
|
*/
|
2016-01-17 00:04:32 +01:00
|
|
|
SENSORS_ACTIVATE(weather_meter);
|
|
|
|
|
2016-01-17 02:22:19 +01:00
|
|
|
/* And the upper threshold value to compare and generate an interrupt */
|
|
|
|
weather_meter.configure(WEATHER_METER_ANEMOMETER_INT_OVER,
|
|
|
|
ANEMOMETER_THRESHOLD_TICK);
|
|
|
|
weather_meter.configure(WEATHER_METER_RAIN_GAUGE_INT_OVER,
|
|
|
|
RAIN_GAUGE_THRESHOLD_TICK);
|
|
|
|
|
2016-01-17 00:04:32 +01:00
|
|
|
etimer_set(&et, READ_SENSOR_PERIOD);
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
PROCESS_WAIT_EVENT_UNTIL(etimer_expired(&et));
|
|
|
|
|
|
|
|
rain = weather_meter.value(WEATHER_METER_RAIN_GAUGE);
|
|
|
|
wind_speed = weather_meter.value(WEATHER_METER_ANEMOMETER);
|
|
|
|
wind_dir = weather_meter.value(WEATHER_METER_WIND_VANE);
|
2016-01-19 18:00:18 +01:00
|
|
|
wind_dir_avg_2m = weather_meter.value(WEATHER_METER_WIND_VANE_AVG_X);
|
2016-01-17 14:56:12 +01:00
|
|
|
wind_speed_avg = weather_meter.value(WEATHER_METER_ANEMOMETER_AVG);
|
2016-01-19 18:00:18 +01:00
|
|
|
wind_speed_avg_2m = weather_meter.value(WEATHER_METER_ANEMOMETER_AVG_X);
|
2016-01-17 14:56:12 +01:00
|
|
|
wind_speed_max = weather_meter.value(WEATHER_METER_ANEMOMETER_MAX);
|
2016-01-17 00:04:32 +01:00
|
|
|
|
2016-01-19 18:03:16 +01:00
|
|
|
#if WEATHER_METER_RAIN_RETURN_TICKS
|
|
|
|
rain *= WEATHER_METER_AUX_RAIN_MM;
|
|
|
|
if(rain > (WEATHER_METER_AUX_RAIN_MM * 3)) {
|
|
|
|
printf("Rain: %lu.%lu mm, ", (rain / 10000), (rain % 10000));
|
|
|
|
#else
|
|
|
|
if(rain >= 10) {
|
|
|
|
printf("Rain: %u.%u mm, ", (rain / 10), (rain % 10));
|
|
|
|
#endif
|
2016-01-17 00:04:32 +01:00
|
|
|
} else {
|
2016-01-17 02:22:19 +01:00
|
|
|
printf("Rain: 0.%lu mm, ", rain);
|
2016-01-17 00:04:32 +01:00
|
|
|
}
|
|
|
|
|
2016-01-17 21:34:52 +01:00
|
|
|
printf("Wind dir: %u.%01u deg, ", (wind_dir / 10), (wind_dir % 10));
|
|
|
|
printf("(%u.%01u deg avg)\n", (wind_dir_avg_2m / 10), (wind_dir_avg_2m % 10));
|
2016-01-17 14:56:12 +01:00
|
|
|
printf("Wind speed: %u m/h ", wind_speed);
|
|
|
|
printf("(%u m/h avg, %u m/h 2m avg, %u m/h max)\n\n", wind_speed_avg,
|
|
|
|
wind_speed_avg_2m,
|
|
|
|
wind_speed_max);
|
2016-01-17 00:04:32 +01:00
|
|
|
etimer_reset(&et);
|
|
|
|
}
|
|
|
|
|
|
|
|
PROCESS_END();
|
|
|
|
}
|
|
|
|
/*---------------------------------------------------------------------------*/
|
|
|
|
/**
|
|
|
|
* @}
|
|
|
|
* @}
|
|
|
|
*/
|
2016-09-01 16:08:01 +02:00
|
|
|
|