Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * Library:
- * - DHT sensor library
- * - Adafruit SSD1306 Wemos Mini OLED
- *
- * D1mini DHT OLED MQ135
- * ------ ---- ---- ----
- * 3V3 VCC VCC
- * 5V VCC
- * GND GND GND GND
- * D1 SCL
- * D2 SDA
- * D3 Data *)DHT shield use D4 (LED_BUILTIN)
- * A0 AO
- */
- #include <DHT.h>
- #include <Adafruit_SSD1306.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266HTTPClient.h>
- #define SSID "Chicken_Behaviour"
- #define PASS "chicken1p8"
- #define N 12
- DHT dht(D3, DHT22);
- Adafruit_SSD1306 oled(-1);
- int i = 0;
- float Tc_ = 0.0;
- float RH_ = 0.0;
- int AQ_ = 0;
- void setup() {
- pinMode(LED_BUILTIN, OUTPUT);
- dht.begin();
- oled.begin();
- oled.dim(true);
- oled.setTextColor(WHITE, BLACK);
- WiFi.begin(SSID, PASS);
- while (WiFi.status() != WL_CONNECTED) delay(500);
- }
- void loop() {
- float Tc = dht.readTemperature();
- float RH = dht.readHumidity();
- int AQ = analogRead(A0);
- Tc_ += Tc; RH_ += RH; AQ_ += AQ; i++;
- show_temp(Tc, RH, AQ);
- if (i == N) {
- if (WiFi.status() == WL_CONNECTED)
- send_data(Tc_/N, RH_/N, AQ_/N);
- Tc_ = 0.0; RH_ = 0.0; AQ_ = 0; i = 0;
- }
- delay(10000);
- }
- void send_data(float Tc, float RH, int AQ) {
- WiFiClient client;
- HTTPClient http;
- digitalWrite(LED_BUILTIN, LOW);
- http.begin(client, "http://api.thingspeak.com/update");
- http.addHeader("Content-Type", "application/x-www-form-urlencoded");
- int resp_code = http.POST("api_key=FQZY5FOX5H9XXXXX&field1=" + String(Tc) + "&field2=" + RH + "&field3=" + AQ + "&field8=" + WiFi.RSSI());
- http.end();
- oled.setCursor(0, 36);
- oled.print(resp_code);
- oled.display();
- digitalWrite(LED_BUILTIN, HIGH);
- }
- void show_temp(float Tc, float RH, int AQ) {
- oled.clearDisplay();
- oled.setCursor(0, 0);
- oled.setTextSize(2);
- oled.print(int(Tc));
- oled.setTextSize(1);
- oled.print("\"");
- oled.setCursor(24, 8);
- oled.print(".");
- oled.print(int((Tc - int(Tc)) * 10));
- oled.setCursor(30, 32);
- oled.setTextSize(2);
- oled.print(RH, 0);
- oled.setTextSize(1);
- oled.setCursor(54, 40);
- oled.print("%");
- oled.setCursor(0, 40);
- oled.print(AQ);
- oled.display();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement