Homemade weather station: Arduino Uno R3 + DHT 11 + ESP-01

Temp and monitor and alarm system for your environment.
I have a small server room need to monitor 24/7 and alarm when humidity, temperature go over a fixed value: like 25 C degree and 70%.  
Arduino Uno R3 may be the most popular open source hardware with lots of modules following. ESP-01 is the cheapest wifi module, DHT 11 .
Here is my codes:

/*--------------------------------------
 * Getting temperature and humidity from DHT 11 and upload to thingspeak.com
 * You get this code from https://lotus-vp.blogspot.com,
 * please keep these comments when copying these code, thank you!
 * Connections
 * DHT         | Arduino Uno
 * ---------------------------
 * VCC(1)    |  5V
 * DATA(2)  |  2
 * NC(3)       |  x
 * GND(4)    |  GND
 -------------------------------------*/
#include <SoftwareSerial.h>
#include "DHT.h"

#define DHTTYPE DHT11             //DHT 11, DHT 12 or whatever DHT types you are using
#define DHTPIN  2                 //DATA to PD2
#define IP      "184.106.153.149" //Thingspeak.com IP address

//Sensor variables
int   humDHT;
int   tempDHT;

//Timer variables
long  sampleTimingSeconds = 5; //Time for sensors to read input (seconds)
long  startTiming = 0;
long  elapsedTime = 0;

//ESP-01 communication variables
char  msg[] = "GET /update?key=XXXXXXXXXXXX"; //Your thingspeak.com API
char  cmd[100];
char  aux_str[100];
int   legth;

//Initialization
SoftwareSerial espSerial(10, 11); //RX, TX
DHT dht(DHTPIN, DHTTYPE);

void setup() {

  Serial.begin(115200); //Initializing Serial Baud 115200
  espSerial.begin(9600);
  Serial.println("http://lotus-vp.blogspot.com Bot!");
  dht.begin();
  connectWiFi();
  readSensors();
  startTiming = millis();
}

void loop() {
  elapsedTime = millis() - startTiming;

  if (elapsedTime > (sampleTimingSeconds * 1000))
  {
    readSensors();
    printData();
    updateDataThingSpeak();
    startTiming = millis();
  }
}

void readSensors(void)
{
  tempDHT = dht.readTemperature();
  humDHT = dht.readHumidity();
}

void printData(void) {
  Serial.print("Humidity: ");
  Serial.print(humDHT);
  Serial.print(" %\t");
  Serial.print("Temprature: ");
  Serial.print(tempDHT);
  Serial.print(" *C\t");
  }

/***************************************************
  WIFI
****************************************************/
void connectWiFi(void) {
  sendATcommand("AT", "OK", 5000);
  sendATcommand("AT+CWMODE=1", "OK", 5000);
  sendATcommand("AT+CWJAP=\"your_ssid\",\"ssid_password\"", "OK", 5000);
  sendATcommand("AT+CIPMUX=1", "OK", 5000);
  sendATcommand("AT+CIFSR", "OK", 5000);
  Serial.println("ESP8266 Connected");
}

/***************************************************
  Connect ThingsSpeak.com
****************************************************/
void startThingSpeakCmd(void) {
  memset(aux_str, '\0', 100);
  snprintf(aux_str, sizeof(aux_str), "AT+CIPSTART=1,\"TCP\",\"%s\",80", IP);
  if (sendATcommand(aux_str, "OK", 20000) == 1) {
    Serial.println("OK, Thingspeak connected!");
  }
}

/***************************************************
  Send to ThingsSpeak.com
****************************************************/
void sendThingSpeakCmd(void) {
  memset(aux_str, '\0', 100);
  sprintf(aux_str, "AT+CIPSEND=1,%d", legth);
  if (sendATcommand(aux_str, ">", 20000) == 1) {
    Serial.println(cmd);
    sendATcommand(cmd, "SEND OK", 30000);
  }
}

/***************************************************
  Update to thingspeak.com and close
****************************************************/
void updateDataThingSpeak(void) {
  startThingSpeakCmd();
  sprintf(cmd, "%s&field1=%d&field2=%d", msg, tempDHT, humDHT);
  legth = strlen(cmd) + 2;
  sendThingSpeakCmd();
  sendATcommand("AT+CIPCLOSE=1", "OK", 5000);
}

/*------------------------------------------
 * Original Arduino AT command function from https://gist.github.com/recyclerobot/0a11aeea8f0a84a4e992
 * and customized by https://lotus-vp.blogspot.com
 -------------------------------------------*/
int8_t sendATcommand(char* ATcommand, char* expected_answer, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  char response[100];
  unsigned long previous;
 
  memset(response, '\0', 100);  //Clean receving buffer
  delay(100); //Delaying to avoid  interfering command passing
 
  while (espSerial.available()>0) { //Wait for cleaning input buffer
    espSerial.read();
  }
 
  espSerial.println(ATcommand);    //Send AT command
  previous = millis();

  do { //This loop wait for the answer
    if (espSerial.available() != 0) { //If there are data in UART input buffer, read it
      response[x] = espSerial.read();
      x++;
     
      if (strstr(response, expected_answer) != NULL) { //Check for the answer inside the response
        answer=1;
      }
     
    }
  } while ((answer==0)&&((millis()-previous)<timeout)); //Wait for the answer untill time out
 
  Serial.println(response);   //For debug purpose
  return answer;
}
The weather station at my PC before I put it in server room
Thank for reading!

Comments

Popular posts from this blog

OpenWRT là gì? Khám phá lợi ích cho router không dây

Hướng dẫn cài đặt OpenWrt trên Router – Tối ưu hiệu năng mạng

Khám Phá Xu Hướng Đầu Tư 2025: Tài Chính, Bất Động Sản, và Công Nghệ