NodeMCU / 2x DTH / 1x OLED / Thinkspeak

Načítá teploty ze 2 čidel, posílá na thinkspeak a zobrazuje na OLED displeji

 

#include <Wire.h>
#include <DHT.h>
#include <ESP8266WiFi.h>
#include <ACROBOTIC_SSD1306.h>
 
// replace with your channel's thingspeak API key, 
String apiKey = "DTQ**********";
String apiKey2 = "HE5**********";
const char* ssid = "Brno4";
const char* password = "**********";
 
const char* server = "api.thingspeak.com";
#define DHTPIN  D3  // D1 pin on Nodemcu
#define DHTPIN2 D4  // D2 pin on Nodemcu
 
DHT dht(DHTPIN, DHT22);
DHT dht2(DHTPIN2, DHT22);

WiFiClient client;

void setup() { 
      
  Wire.begin();                     // OLED wiring
  oled.init();                      // Initialze SSD1306 OLED display
  oled.clearDisplay();              // Clear screen 
  
  Serial.begin(115200);
  delay(10);
  dht.begin();
  delay(10);
  dht2.begin();
  
  WiFi.begin(ssid, password);
 
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  oled.setTextXY(1,0);              // Set cursor position, start of line 1
  oled.putString("Wi-Fi:");
   
  WiFi.begin(ssid, password);
   
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
    oled.putString(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  oled.setTextXY(4,0);              // Set cursor position, start of line 4
  oled.putString("PRIPOJENO");
  
}
 
 
void loop() {   
  
  oled.clearDisplay();              // Clear screen 
  

  
  float h = dht.readHumidity();     // read-1- humidity
  float t = dht.readTemperature();  // read-1- temperature in celsius

  if (isnan(h) || isnan(t)) {
    Serial.println("Failed to read from DHT sensor!");
    //return;
  }

  oled.setTextXY(1,0);              // Set cursor position, start of line 1
  oled.putString("Venku: ");
  oled.putString(String(t));
  oled.putString(" C"); 

  oled.setTextXY(2,0);              // Set cursor position, start of line 1
  oled.putString("       ");
  oled.putString(String(h));
  oled.putString(" %");
  
  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr = apiKey;
           postStr +="&field1=";
           postStr += String(t); // prevadi na int-zruseno
           postStr +="&field2=";
           postStr += String((int)h); // převadi na int-ponechano
 
     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(" degrees Celcius Humidity: "); 
     Serial.print(h);
     Serial.println("% send to Thingspeak");    

    
  }
  client.stop();



  float h2 = dht2.readHumidity();     // read-2- humidity
  float t2 = dht2.readTemperature();  // read-2- temperature in celsius
  
  if (isnan(h2) || isnan(t2)) {
    Serial.println("Failed to read from DHT2 sensor!");
    //return;
  }

  oled.setTextXY(4,0);              // Set cursor position, start of line 1
  oled.putString("Doma: ");
  oled.putString(String(t2));
  oled.putString(" C"); 

  oled.setTextXY(5,0);              // Set cursor position, start of line 1
  oled.putString("      ");
  oled.putString(String(h2));
  oled.putString(" %");

  if (client.connect(server,80)) {  //   "184.106.153.149" or api.thingspeak.com
    String postStr2 = apiKey2;
           postStr2 +="&field1=";
           postStr2 += String(t2); // prevadi na int-zruseno
           postStr2 +="&field2=";
           postStr2 += String((int)h2); // převadi na int-ponechano
 
     client.print("POST /update HTTP/1.1\n"); 
     client.print("Host: api.thingspeak.com\n"); 
     client.print("Connection: close\n"); 
     client.print("X-THINGSPEAKAPIKEY: "+apiKey2+"\n"); 
     client.print("Content-Type: application/x-www-form-urlencoded\n"); 
     client.print("Content-Length: "); 
     client.print(postStr2.length()); 
     client.print("\n\n"); 
     client.print(postStr2);
           
 
     Serial.print("Temperature: ");
     Serial.print(t2);
     Serial.print(" degrees Celcius Humidity: "); 
     Serial.print(h2);
     Serial.println("% send to Thingspeak");    

    
  }
  client.stop();


   
  Serial.println("Waiting...");    
  // time between updates
  delay(120000); // 2 mins
}

 

9 komentářů u „NodeMCU / 2x DTH / 1x OLED / Thinkspeak“

    • Dobrý den,
      jde o OSX? Dle popisu co jsem dohledal, tak může jít o cybu ovladače, nebo že není zvolený správní PORT v Tools > Port menu v Arduino IDE.
      Tedy ani jiné kódy by vám tam neměli jít aktuálně uploadovat.

      Odpovědět
      • Je to tak, před tím než jsem nahrával tenhle projekt tak jsem na desce zkoušel OTA a ono to v ní zůstalo a ja jsem si to neuvědomil.

        Odpovědět
  1. Už jsem to rozchodil, děkuji za hezký příklad. Nevíš náhodou jak otočit zobrazování na oled? (vzhuru nohama) děkuji

    Odpovědět
    • oled.rotate(); nebo něco takového to tuším bylo – ale používal jsem více knihoven a nejsem si teď úplně jistý. JEstli dohledáš, tak prosím napiš 😉

      Odpovědět

Napsat komentář