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

82 lines
2.2 KiB
Plaintext
Raw Normal View History

2017-02-22 14:53:05 +01: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_Sensor.h>
2017-02-23 22:22:40 +01:00
#include <Adafruit_BMP280.h>
2017-02-22 14:53:05 +01:00
extern "C" {
#include "arduino-process.h"
#include "rest-engine.h"
extern resource_t res_bmptemp,res_bmppress,res_bmpatm,res_bmpalt, res_battery;
float bmptemp;
float bmppress;
float bmpatm;
float bmpalt;
char bmptemp_s[8];
2017-02-23 22:22:40 +01:00
char bmppress_s[10];
2017-02-22 14:53:05 +01:00
char bmpatm_s[8];
2017-02-23 22:36:32 +01:00
char bmpalt_s[8];;
2017-02-22 14:53:05 +01:00
#define SEALEVELPRESSURE_HPA (1013.25)
2017-02-23 22:22:40 +01:00
Adafruit_BMP280 bme; // I2C
2017-02-22 14:53:05 +01:00
#define LED_PIN 4
}
void setup (void)
{
2017-02-23 22:36:32 +01:00
bool status;
2017-02-22 14:53:05 +01:00
// switch off the led
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, HIGH);
// default settings
2017-02-23 22:22:40 +01:00
status = bme.begin(0x76);
2017-02-22 14:53:05 +01:00
if (!status) {
2017-02-23 22:22:40 +01:00
printf("Could not find a valid BMP280 sensor, check wiring!");
2017-02-22 14:53:05 +01:00
}
// init coap resourcen
rest_init_engine ();
#pragma GCC diagnostic ignored "-Wwrite-strings"
rest_activate_resource (&res_bmptemp, "s/temp");
rest_activate_resource (&res_bmppress, "s/press");
rest_activate_resource (&res_bmpatm, "s/atm");
rest_activate_resource (&res_bmpalt, "s/alt");
rest_activate_resource (&res_battery, "s/battery");
#pragma GCC diagnostic pop
}
// at project-conf.h
// LOOP_INTERVAL (10 * CLOCK_SECOND)
void loop (void)
{
bmptemp = bme.readTemperature();
bmppress = bme.readPressure();
bmpalt = bme.readAltitude(SEALEVELPRESSURE_HPA);
bmpatm = bme.readPressure() / 100.0F;
2017-03-03 11:24:32 +01:00
dtostrf(bmptemp , 0, 2, bmptemp_s );
dtostrf(bmppress , 0, 2, bmppress_s );
dtostrf(bmpalt , 0, 2, bmpalt_s );
dtostrf(bmpatm , 0, 2, bmpatm_s );
2017-02-23 22:22:40 +01:00
2017-02-22 14:53:05 +01:00
// Debug Print
2017-03-03 11:24:32 +01:00
printf("Temp: %s\n",bmptemp_s);
2017-02-23 08:59:49 +01:00
printf("Press: %s\n",bmppress_s);
printf("Altitude: %s\n",bmpalt_s);
2017-02-23 22:22:40 +01:00
printf("atm: %s\n",bmpatm_s);
2017-02-22 14:53:05 +01:00
}