同时读取两个传感器的数据,并上传至云端存储-Arduino中文社区 - Powered by Discuz!

Arduino中文社区

 找回密码
 立即注册

QQ登录

只需一步,快速开始

查看: 3027|回复: 0

[分享] 同时读取两个传感器的数据,并上传至云端存储

[复制链接]
发表于 2019-9-16 00:34 | 显示全部楼层 |阅读模式
一个示例,仅供参考,可以自己更换传感器,或者连接更多传感器。


开发板:
Wifiduino(esp8266)
传感器:DHT11、BME280

界面:
QQ截图20190916003736.jpg

例程:
[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]

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|Archiver|手机版|Arduino中文社区

GMT+8, 2024-11-28 03:34 , Processed in 0.301003 second(s), 18 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表