Advertisement
auriza

wemosd1-dht22.ino

Sep 13th, 2021
122
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * Library:
  3.  * - DHT sensor library
  4.  * - Adafruit SSD1306 Wemos Mini OLED
  5.  *
  6.  * D1mini   DHT   OLED  MQ135  
  7.  * ------   ----  ----  ----
  8.  * 3V3      VCC   VCC
  9.  * 5V                   VCC
  10.  * GND      GND   GND   GND
  11.  * D1             SCL
  12.  * D2             SDA
  13.  * D3       Data                 *)DHT shield use D4 (LED_BUILTIN)
  14.  * A0                   AO
  15.  */
  16.  
  17. #include <DHT.h>
  18. #include <Adafruit_SSD1306.h>
  19. #include <ESP8266WiFi.h>
  20. #include <ESP8266HTTPClient.h>
  21.  
  22. #define SSID      "Chicken_Behaviour"
  23. #define PASS      "chicken1p8"
  24. #define N         12
  25.  
  26. DHT               dht(D3, DHT22);
  27. Adafruit_SSD1306  oled(-1);
  28. int   i   = 0;
  29. float Tc_ = 0.0;
  30. float RH_ = 0.0;
  31. int   AQ_ = 0;
  32.  
  33. void setup() {
  34.   pinMode(LED_BUILTIN, OUTPUT);
  35.   dht.begin();
  36.   oled.begin();
  37.   oled.dim(true);
  38.   oled.setTextColor(WHITE, BLACK);
  39.   WiFi.begin(SSID, PASS);
  40.   while (WiFi.status() != WL_CONNECTED) delay(500);
  41. }
  42.  
  43. void loop() {
  44.   float Tc = dht.readTemperature();
  45.   float RH = dht.readHumidity();
  46.   int   AQ = analogRead(A0);
  47.   Tc_ += Tc; RH_ += RH; AQ_ += AQ; i++;
  48.   show_temp(Tc, RH, AQ);
  49.   if (i == N) {
  50.     if (WiFi.status() == WL_CONNECTED)
  51.       send_data(Tc_/N, RH_/N, AQ_/N);
  52.     Tc_ = 0.0; RH_ = 0.0; AQ_ = 0; i = 0;
  53.   }
  54.   delay(10000);
  55. }
  56.  
  57. void send_data(float Tc, float RH, int AQ) {
  58.   WiFiClient client;
  59.   HTTPClient http;
  60.  
  61.   digitalWrite(LED_BUILTIN, LOW);
  62.   http.begin(client, "http://api.thingspeak.com/update");
  63.   http.addHeader("Content-Type", "application/x-www-form-urlencoded");
  64.   int resp_code = http.POST("api_key=FQZY5FOX5H9XXXXX&field1=" + String(Tc) + "&field2=" + RH  + "&field3=" + AQ  + "&field8=" + WiFi.RSSI());
  65.   http.end();
  66.  
  67.   oled.setCursor(0, 36);
  68.   oled.print(resp_code);
  69.   oled.display();
  70.   digitalWrite(LED_BUILTIN, HIGH);
  71. }
  72.  
  73. void show_temp(float Tc, float RH, int AQ) {
  74.   oled.clearDisplay();
  75.   oled.setCursor(0, 0);
  76.   oled.setTextSize(2);
  77.   oled.print(int(Tc));
  78.   oled.setTextSize(1);
  79.   oled.print("\"");
  80.   oled.setCursor(24, 8);
  81.   oled.print(".");
  82.   oled.print(int((Tc - int(Tc)) * 10));
  83.   oled.setCursor(30, 32);
  84.   oled.setTextSize(2);
  85.   oled.print(RH, 0);
  86.   oled.setTextSize(1);
  87.   oled.setCursor(54, 40);
  88.   oled.print("%");
  89.   oled.setCursor(0, 40);
  90.   oled.print(AQ);
  91.   oled.display();
  92. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement