1. 硬件準備工作
DHT11共有四隻腳,左邊第一隻腳接+5V,第二隻腳接Arduino Digitail Pin,第四隻腳接GND,比較特別的是第一、二隻腳還要用一個4.7K的電阻連接(注意:Vcc 和GND 接反會燒掉) (4.7K的電阻不接也行)
2. Install Arduino IDE
3. 在 Arduino IDE 加入 ESP8266 開發板
在 Arduino \ Preferences \
在 Additional Boards Manager URLs 框框輸入
http://arduino.esp8266.com/stable/package_esp8266com_index.json
REF: https://randomnerdtutorials.com/how-to-install-esp8266-board-arduino-ide/
Arduino IDE -> boards manager. -> Tools > Board > Boards Manager -> Scroll down, select the ESP8266 board menu and install “esp8266 platform”
4. Arduino IDE 加入需要的 Library
在 Arduino \ Sketch \ Include Library \ Manage Libraries
5. 選則開發板
在 Arduino \Tools\Board (例如 WeMos D1)
6. 程式碼:
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <DHT.h>
#include <BlynkSimpleEsp8266.h>
#define DHTPIN 5 // D4 = GPIO 2
#define DHTTYPE DHT11
int SOUND_VOLUME_PIN = A0; /* Input pin for the potentiometer */
int SOUND_THRESHOLD = 500; /* Sound level threshold */
int SOUND_MAX = 600; /* Sound maximum level */
int soundValue;
const int RainAnalogInPin = A0;
const int LightDigitalInPin = 3;
const int LEDRDigitalInPin = 13;
const int LEDGDigitalInPin = 12;
const int LEDBDigitalInPin = 14;
int RainsensorValue = 0;
int RainMapValue = 0;
// your thingspeak channel API key and your WiFi SSID and password
String apiKey = "xxx";
const char* server = "api.thingspeak.com";
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxx";
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "xxx";
char pass[] = "xxx";
DHT dht(DHTPIN, DHTTYPE);
WiFiClient client;
void setup()
{
Serial.begin(9600);
pinMode(LightDigitalInPin,INPUT);
pinMode(LEDRDigitalInPin,OUTPUT);
pinMode(LEDGDigitalInPin,OUTPUT);
pinMode(LEDBDigitalInPin,OUTPUT);
dht.begin();
Blynk.begin(auth, ssid, pass);
}
void loop()
{
Blynk.run();
if (WiFi.status() == WL_CONNECTED) {
Serial.println("WiFi connected");
Serial.print("WiFi LocalIP : ");
Serial.println(WiFi.localIP());
}
soundValue = analogRead(SOUND_VOLUME_PIN);
if (soundValue > SOUND_THRESHOLD)
{
Serial.print("Sound Sensor Volume = ");
Serial.println(soundValue);
} else {
Serial.print("Sound Sensor Volume = ");
Serial.println(soundValue);
}
SendData_To_ThinkSpeak_Blynk();
}
void SendData_To_ThinkSpeak_Blynk() {
// read the rain analog in value:
RainsensorValue = analogRead(RainAnalogInPin);
// map the 1024 to from 0 ~ 1024
RainMapValue = map(RainsensorValue, 0, 1023, 1024, 1);
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//int chk = DHT.read11(dht_dpin);
Blynk.virtualWrite(V1, t);
Blynk.virtualWrite(V2, h);
if (client.connect(server,80)) {
String postStr = apiKey;
postStr +="&field1=";
postStr += String(t);
postStr +="&field2=";
postStr += String(h);
postStr +="&field3=";
postStr += String(RainMapValue);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
Serial.print("Temperature: ");
Serial.print(t );
Serial.print("Humidity:");
Serial.print(h );
Serial.print(" Rain : (1-1024) ");
//Serial.print(RainsensorValue );
Serial.print(RainMapValue );
Serial.print(" light : ");
//int sensorValue = analogRead(A1);
//int LDI = 0;
//LDI = digitalRead(LightDigitalInPin);
//Serial.print(LDI);
Serial.println(", Sending data to Thingspeak");
client.stop();
Serial.println("Waiting 6 secs");
//delay(1000);
LEDTest();
}
}
void LEDTest(){
digitalWrite(LEDRDigitalInPin,HIGH);
delay(1000);
digitalWrite(LEDRDigitalInPin,LOW);
delay(1000);
digitalWrite(LEDGDigitalInPin,HIGH);
delay(1000);
digitalWrite(LEDGDigitalInPin,LOW);
delay(1000);
digitalWrite(LEDBDigitalInPin,HIGH);
delay(1000);
digitalWrite(LEDBDigitalInPin,LOW);
delay(1000);
}
Debian 9.3 with Arduino 1.8.5 serial port issue :
tail -f /var/log/syslog:
arduino-arduinoide.desktop[19409]: Error opening serial port '/dev/ttyUSB0'. Try consulting the documentation at http://playground.arduino.cc/Linux/All#Permission
solved by:
sudo chown jimmy:jimmy /dev/ttyUSB0