#include <DHT.h> //เรียกใช้ library DHT.h
#define DHTPIN 14 // เลือกขา GPIO14
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE, 15);
void setup() {
Serial.begin(9600);
dht.begin(); //เริ่มเรียกการใช้งาน dht จาก library
}
void sendJsonString(float temperature, float humidity, String dataStatus, String reason){
/*
{
"data" : {
"temperature" : 0.0, // 0,NaN
"humidity" : 0.0
},
"status" : "OK", //OK, Error
"reason" : ""
}
*/
String s = "";
s += String("{");
s += String("\"data\" : {");
s += String("\"temperature\" : " + String(temperature) + "," );
s += String("\"humidity\" : " + String(humidity) );
s += String("} ," );
s += String("\"status\" : \"" + dataStatus + "\"" + "," );
s += String("\"reason\" : \"" + error + "\"");
s += String("}");
Serial.println(s);
}
void loop() {
// Reading temperature or humidity takes about 250 milliseconds!
delayMicroseconds(250);
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
delay(2000);
// Set up data
float h = dht.readHumidity();
float t = dht.readTemperature();
// check if returns are valid, if they are NaN (not a number) then something went wrong!
float temperature = t;
float humidity = h;
String dataStatus = "OK";
String reason = "";
if (isnan(t) || isnan(h)) {
temperature = 0;
humidity = 0;
dataStatus = "Error";
reason = "Failed to read from DHT";
}
// Send data
sendJsonString(temperature, humidity, dataStatus, reason);
วันอาทิตย์ที่ 24 กุมภาพันธ์ พ.ศ. 2562
Arduino กับ Nodejs
สมัครสมาชิก:
ส่งความคิดเห็น (Atom)
มินิโปรเจค Arduino Calculator (แก้ไข)
https://www.electronicshub.org/arduino-calculator/ #include <LiquidCrystal.h> ...
-
void setup() { DDRD = 0xFF; // เซ็ตให้ขา 0 - 7 เป็นเอาต์พุต PORTD = 0xF9 ; // เซ็ตให้แสดงค่า 0xF9 ออกไป (แสดงเลข 1 ใน 7 Segment C...
-
ขาดิจิตอลของ Arduino สามารถส่งค่าที่เป็นดิจิตอลออกมาได้ โดยสัญญาณดิจิตอลนั้นถ้าอธิบายให้เข้าใจง่ายๆ มีอยู่ 2 รูปแบบคือ HIGH (มีไฟ) และ L...
ไม่มีความคิดเห็น:
แสดงความคิดเห็น