一个示例,仅供参考,可以自己更换传感器,或者连接更多传感器。
开发板:
Wifiduino(esp8266)
传感器:DHT11、BME280
界面:
例程:
[mw_shl_code=arduino,true]
#define BLINKER_WIFI
#include <Blinker.h>
char auth[] = "xxxxxxxxxxxx";
char ssid[] = "xxxxxx";
char pswd[] = "xxxxxx";
// BME280
BlinkerNumber TEMP("temp"); // Temperature
BlinkerNumber HUMI("humi"); // Humidity
BlinkerNumber PRES("pres"); // Pressure
BlinkerNumber ALTI("alti"); // Altitude
int tempValue = 0;
int humiValue = 0;
int presValue = 0;
int altiValue = 0;
// DHT11
BlinkerNumber DHTTEMP("dht-temp"); // Temperature
BlinkerNumber DHTHUMI("dht-humi"); // Humidity
int dhtTempValue = 0;
int dhtHumiValue = 0;
// RUNTIME
uint32 startTime;
uint32 runTime;
// Download Adafruit-BMP085-Library library here:
// https://github.com/adafruit/Adafruit_BME280_Library
// https://github.com/adafruit/Adafruit_Sensor
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme;
#include <DHT.h>
#define DHTPIN D7
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
void dataRead(const String &data)
{
BLINKER_LOG("Blinker readString: ", data);
Blinker.vibrate();
uint32_t BlinkerTime = millis();
Blinker.print("millis", BlinkerTime);
}
void dataStorage()
{
readSensor();
Blinker.dataStorage("temp", tempValue);
Blinker.dataStorage("humi", humiValue);
Blinker.dataStorage("pres", altiValue);
Blinker.dataStorage("dht-temp", dhtTempValue);
Blinker.dataStorage("dht-humi", dhtHumiValue);
}
void heartbeat()
{
readSensor();
TEMP.print(tempValue);
HUMI.print(humiValue);
PRES.print(presValue);
ALTI.print(altiValue);
DHTTEMP.print(dhtTempValue);
DHTHUMI.print(dhtHumiValue);
}
void readSensor()
{
tempValue = bme.readTemperature();
humiValue = bme.readHumidity();
presValue = bme.readPressure();
altiValue = bme.readAltitude(SEALEVELPRESSURE_HPA);
dhtTempValue = dht.readTemperature();
dhtHumiValue = dht.readHumidity();
if (isnan(dhtTempValue) || isnan(dhtHumiValue))
{
dhtTempValue = 0;
dhtHumiValue = 0;
}
}
void setup()
{
Serial.begin(115200);
BLINKER_DEBUG.stream(Serial);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, LOW);
Blinker.begin(auth, ssid, pswd);
if (!bme.begin())
{
BLINKER_LOG("Could not find a valid BME280 sensor, check wiring!");
while (1)
{
delay(100);
}
}
dht.begin();
readSensor();
Blinker.attachData(dataRead);
Blinker.attachHeartbeat(heartbeat);
Blinker.attachDataStorage(dataStorage);
}
void loop()
{
Blinker.run();
}[/mw_shl_code]
|