Arduino教學-使用ESP8266 wifi模組+DHT 溫溼度感測器上傳thingspeak

這是透過Arduino+ESP8266(wifi)+DHT(溫溼度感測器),實現每60秒上傳溫溼度到thingspeak網站的記錄。
一、上 Thingspeak 申請帳號,然後建立一個Channel後,然後取得Write API Key。(https://thingspeak.com)
二、DHT連線
DHT → Arduino
S → A0
+ → 5V (VCC)
– → GND
三、ESP8266
ESP8266 → Arduino
GND → GND
UTXD → RX(0)
CH_PD → 3.3V (VCC 注意:連接USB時不要接上,不然會抓不到,程式上傳後,直接用變壓器)
URXD → TX(1)
VCC → 3.3V (VCC)
四、USB轉TTL (作為監測用)
USBTTL → Arduino
TXD(綠)→ 2
RXD(白)→ 3
l_140820105419
腳位圖
20691_10152621979171541_6306184665770254413_n

11024717_10152621978966541_3990729037825987143_n

未命名 - 5
程式碼

#include <SoftwareSerial.h>
#define DEBUG
#define _baudrate   9600

// USB TTL 偵錯用
#define _rxpin      2
#define _txpin      3
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX

// DHT溫溼度感測器
#define dht_dpin A0
byte bGlobalErr;
byte dht_dat[5];

// ESP8266 Wifi晶片 
#define SSID "[無線分享器名稱]" 
#define PASS "[無線分享器密碼]"
#define IP "184.106.153.149" // ThingSpeak的IP
// 使用 GET 傳送資料的格式
String GET = "GET /update?key=[Write API Key]";

void setup() {
    InitDHT();
    Serial.begin( _baudrate );
    debug.begin( _baudrate );
    delay(300);
    sendDebug("AT");
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("RECEIVED: OK\nData ready to sent!");
        connectWiFi();
    }
    delay(700);
}

void loop() {
  ReadDHT();
  switch (bGlobalErr){
     case 0:
        String TT,HH;
        HH = String(dht_dat[0])+'.'+String(dht_dat[1]);
        TT = String(dht_dat[2])+'.'+String(dht_dat[3]);
        updateDHT11( TT, HH );        
        #ifdef DEBUG
            debug.print("Humidity: "); 
            debug.print( HH );
            debug.print(" %\t");
            debug.print("Temperature: "); 
            debug.print( TT );
            debug.println(" *C\t");
        #endif
    
  }
  delay(60000);   // 一分鐘一次
}

void updateDHT11( String T, String H )
{
    // 使用AT指令上傳
    String cmd = "AT+CIPSTART=\"TCP\",\"";
    cmd += IP;
    cmd += "\",80";
    sendDebug(cmd);
    delay(2000);
    if( Serial.find( "Error" ) )
    {
        debug.print( "RECEIVED: Error\nExit1" );
        return;
    }

    cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
    Serial.print( "AT+CIPSEND=" );
    Serial.println( cmd.length() );
    if(Serial.find( ">" ) )
    {
        debug.print(">");
        debug.print(cmd);
        Serial.print(cmd);
    }
    else
    {
        sendDebug( "AT+CIPCLOSE" );
    }
    if( Serial.find("OK") )
    {
        debug.println( "RECEIVED: OK" );
    }
    else
    {
        debug.println( "RECEIVED: Error\nExit2" );
    }
}

void sendDebug(String cmd)
{
    // 傳到 USB TTL
    debug.print("SEND: ");
    debug.println(cmd);
    Serial.println(cmd);
} 
 
boolean connectWiFi()
{
    // 連線到Wifi
    Serial.println("AT+CWMODE=1");
    delay(2000);
    String cmd="AT+CWJAP=\"";
    cmd+=SSID;
    cmd+="\",\"";
    cmd+=PASS;
    cmd+="\"";
    sendDebug(cmd);
    delay(5000);
    if(Serial.find("OK"))
    {
        debug.println("RECEIVED: OK");
        return true;
    }
    else
    {
        debug.println("RECEIVED: Error");
        return false;
    }

    cmd = "AT+CIPMUX=0";
    sendDebug( cmd );
    if( Serial.find( "Error") )
    {
        debug.print( "RECEIVED: Error" );
        return false;
    }
}
void InitDHT(){
   // 初始化DHT
   pinMode(dht_dpin,OUTPUT);
   digitalWrite(dht_dpin,HIGH);
}

void ReadDHT(){
bGlobalErr=0;
byte dht_in;
byte i;
digitalWrite(dht_dpin,LOW);
delay(20);

digitalWrite(dht_dpin,HIGH);
delayMicroseconds(40);
pinMode(dht_dpin,INPUT);

dht_in=digitalRead(dht_dpin);

if(dht_in){
   bGlobalErr=1;
   return;
   }
delayMicroseconds(80);
dht_in=digitalRead(dht_dpin);

if(!dht_in){
   bGlobalErr=2;
   return;
   }
delayMicroseconds(80);
for (i=0; i<5; i++)
   dht_dat[i] = read_dht_dat();
pinMode(dht_dpin,OUTPUT);
digitalWrite(dht_dpin,HIGH);
byte dht_check_sum =
       dht_dat[0]+dht_dat[1]+dht_dat[2]+dht_dat[3];
if(dht_dat[4]!= dht_check_sum)
   {bGlobalErr=3;}
};

byte read_dht_dat(){
  byte i = 0;
  byte result=0;
  for(i=0; i< 8; i++){
      while(digitalRead(dht_dpin)==LOW);
      delayMicroseconds(30);
      if (digitalRead(dht_dpin)==HIGH)
     result |=(1<<(7-i));
    while (digitalRead(dht_dpin)==HIGH);
    }
  return result;
}