温度と湿度を測るプログラムです。
結果は、シリアルモニタに出ます。
運転中ということが分かるように、LED を点滅させます。
temperature_humidity/temperature_humidity.ino
// ---------------------------------------------------------------
/*
temperature_humidity.ino
Apr/05/2021
*/
// ---------------------------------------------------------------
#include <WioLTEforArduino.h>
#define SENSOR_PIN (WIOLTE_D38)
WioLTE Wio;
int TemperatureAndHumidityPin;
int count = 0;
// ---------------------------------------------------------------
void setup()
{
Wio.Init();
TemperatureAndHumidityBegin(SENSOR_PIN);
}
// ---------------------------------------------------------------
void loop()
{
float temp;
float humi;
SerialUSB.println("*** loop *** aaa *** count = " + String(count));
if (!TemperatureAndHumidityRead(&temp, &humi))
{
SerialUSB.println("ERROR! *** TemperatureAndHumidityRead");
}
else
{
SerialUSB.print("Current humidity = ");
SerialUSB.print(humi);
SerialUSB.print("% ");
SerialUSB.print("temperature = ");
SerialUSB.print(temp);
SerialUSB.println("C");
}
delay(2000);
if (count == 0)
{
SerialUSB.println("*** temperature_humidity *** Aug/05/2021 ***");
}
if ((count % 2) == 0)
{
Wio.LedSetRGB(0, 0, 1);
}
else
{
Wio.LedSetRGB(0, 0, 0);
}
count++;
}
// ---------------------------------------------------------------
void TemperatureAndHumidityBegin(int pin)
{
TemperatureAndHumidityPin = pin;
DHT11Init(TemperatureAndHumidityPin);
}
// ---------------------------------------------------------------
bool TemperatureAndHumidityRead(float* temperature, float* humidity)
{
byte data[5];
DHT11Start(TemperatureAndHumidityPin);
for (int i = 0; i < 5; i++) data[i] = DHT11ReadByte(TemperatureAndHumidityPin);
DHT11Finish(TemperatureAndHumidityPin);
if(!DHT11Check(data, sizeof (data))) return false;
if (data[1] >= 10) return false;
if (data[3] >= 10) return false;
*humidity = (float)data[0] + (float)data[1] / 10.0f;
*temperature = (float)data[2] + (float)data[3] / 10.0f;
return true;
}
// ---------------------------------------------------------------
void DHT11Init(int pin)
{
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
}
// ---------------------------------------------------------------
void DHT11Start(int pin)
{
// Host the start of signal
digitalWrite(pin, LOW);
delay(18);
// Pulled up to wait for
pinMode(pin, INPUT);
while (!digitalRead(pin)) ;
// Response signal
while (digitalRead(pin)) ;
// Pulled ready to output
while (!digitalRead(pin)) ;
}
// ---------------------------------------------------------------
byte DHT11ReadByte(int pin)
{
byte data = 0;
for (int i = 0; i < 8; i++) {
while (digitalRead(pin)) ;
while (!digitalRead(pin)) ;
unsigned long start = micros();
while (digitalRead(pin)) ;
unsigned long finish = micros();
if ((unsigned long)(finish - start) > 50) data |= 1 << (7 - i);
}
return data;
}
// ---------------------------------------------------------------
void DHT11Finish(int pin)
{
// Releases the bus
while (!digitalRead(pin)) ;
digitalWrite(pin, HIGH);
pinMode(pin, OUTPUT);
}
// ---------------------------------------------------------------
bool DHT11Check(const byte* data, int dataSize)
{
if (dataSize != 5) return false;
byte sum = 0;
for (int i = 0; i < dataSize - 1; i++) {
sum += data[i];
}
return data[dataSize - 1] == sum;
}
// ---------------------------------------------------------------