osd-contiki/examples/osd/arduino-climate3/sketch.pde

159 lines
4.5 KiB
Text
Raw Normal View History

2015-09-04 14:04:11 +02:00
/*
* Sample arduino sketch using contiki features.
* We turn the LED off
* We allow read the moisture sensor
* Unfortunately sleeping for long times in loop() isn't currently
* possible, something turns off the CPU (including PWM outputs) if a
* Proto-Thread is taking too long. We need to find out how to sleep in
* a Contiki-compatible way.
* Note that for a normal arduino sketch you won't have to include any
* of the contiki-specific files here, the sketch should just work.
*/
#include <Wire.h>
#include "Adafruit_HTU21DF.h"
2015-11-29 22:31:59 +01:00
#include <OneWire.h>
#include "DallasTemperature.h"
2015-09-04 14:04:11 +02:00
extern "C" {
2016-04-12 10:34:40 +02:00
#include "arduino-process.h"
2015-09-04 14:04:11 +02:00
#include "rest-engine.h"
2015-11-29 22:31:59 +01:00
#include "sketch.h"
2015-09-04 14:04:11 +02:00
2015-11-29 22:31:59 +01:00
// Data wire is plugged into port 2 on the Arduino
#define ONE_WIRE_BUS 3
#define TEMPERATURE_PRECISION 9
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature dsensors(&oneWire);
// arrays to hold device addresses
DeviceAddress outsideThermometer;
Adafruit_HTU21DF htu = Adafruit_HTU21DF();
2015-11-29 22:31:59 +01:00
extern resource_t res_htu21dtemp, res_htu21dhum, res_dtemp1, res_battery;
float htu21d_hum;
float htu21d_temp;
2015-09-14 10:51:16 +02:00
char htu21d_hum_s[8];
char htu21d_temp_s[8];
2015-09-04 14:04:11 +02:00
2015-11-29 22:31:59 +01:00
float d_temp;
char d_temp_s[8];
// sketch.h
struct dstemp ds1820[7];
2015-09-04 14:04:11 +02:00
#define LED_PIN 4
}
2015-11-29 22:31:59 +01:00
// main functions to print information about a device
void printAddress(uint8_t* adress)
{
printf("%02X",adress[0]);
printf("%02X",adress[1]);
printf("%02X",adress[2]);
printf("%02X",adress[3]);
printf("%02X",adress[4]);
printf("%02X",adress[5]);
printf("%02X",adress[6]);
printf("%02X",adress[7]);
}
// function to print the temperature for a device
void printTemperature(DeviceAddress deviceAddress,int index)
{
d_temp = dsensors.getTempC(deviceAddress);
dtostrf(d_temp , 6, 2, d_temp_s );
printf("Temp C: ");
printf("%s",d_temp_s);
// copy to structure
ds1820[index].ftemp=d_temp;
strcpy(ds1820[index].stemp, d_temp_s);
}
void printData(DeviceAddress deviceAddress, int index)
{
printf("Device Address: ");
printAddress(deviceAddress);
printf(" ");
printTemperature(deviceAddress,index);
printf("\n");
}
2015-09-04 14:04:11 +02:00
void setup (void)
{
// switch off the led
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
2015-11-29 22:31:59 +01:00
// ds1820 sensor
printf("Dallas Temperature IC Control Library Demo");
// Start up the library
dsensors.begin();
// locate devices on the bus
printf("Locating devices...\n");
printf("Found ");
printf("%d",dsensors.getDeviceCount());
printf(" devices.\n");
// report parasite power requirements
printf("Parasite power is: ");
if (dsensors.isParasitePowerMode()) printf("ON\n");
else printf("OFF\n");
if (!dsensors.getAddress(outsideThermometer, 0)) printf("Unable to find address for Device 0\n");
// show the addresses we found on the bus
printf("Device 0 Address: ");
printAddress(outsideThermometer);
printf("\n");
// set the resolution to 9 bit
dsensors.setResolution(outsideThermometer, 9);
printf("Device 0 Resolution: ");
printf("%d",dsensors.getResolution(outsideThermometer));
printf("\n");
// htu21d sensor
if (!htu.begin()) {
printf("Couldn't find sensor!");
}
2015-09-04 14:04:11 +02:00
// init coap resourcen
rest_init_engine ();
rest_activate_resource (&res_htu21dtemp, "s/temp");
rest_activate_resource (&res_htu21dhum, "s/hum");
2015-11-29 22:31:59 +01:00
rest_activate_resource (&res_dtemp1, "s/t1/temp");
2015-09-04 14:04:11 +02:00
rest_activate_resource (&res_battery, "s/battery");
}
// at project-conf.h
2016-01-06 21:39:35 +01:00
// LOOP_INTERVAL (20 * CLOCK_SECOND)
2015-09-04 14:04:11 +02:00
void loop (void)
{
2016-04-12 10:34:40 +02:00
mcu_sleep_off();
2015-11-29 22:31:59 +01:00
// call sensors.requestTemperatures() to issue a global temperature
// request to all devices on the bus
2016-01-06 21:39:35 +01:00
// printf("Requesting temperatures...");
2015-11-29 22:31:59 +01:00
dsensors.requestTemperatures();
2016-01-06 21:39:35 +01:00
// printf("DONE\n");
2015-11-29 22:31:59 +01:00
// print the device information
printData(outsideThermometer,0);
htu21d_temp = htu.readTemperature();
htu21d_hum = htu.readHumidity();
2016-01-06 21:39:35 +01:00
2015-09-14 10:51:16 +02:00
dtostrf(htu21d_temp , 6, 2, htu21d_temp_s );
dtostrf(htu21d_hum , 6, 2, htu21d_hum_s );
// remove space
if(htu21d_temp_s[0]==' '){
memcpy (htu21d_temp_s,htu21d_temp_s+1,strlen(htu21d_temp_s)+1);
}
if(htu21d_hum_s[0]==' '){
memcpy (htu21d_hum_s,htu21d_hum_s+1,strlen(htu21d_hum_s)+1);
}
// debug only
2015-11-29 22:31:59 +01:00
printf("Temp: %s",htu21d_temp_s);
printf("\t\tHum: %s\n",htu21d_hum_s);
2016-01-06 21:39:35 +01:00
2016-04-12 10:34:40 +02:00
mcu_sleep_on();
2015-09-04 14:04:11 +02:00
}