#include //Software Serial library #include "DHT.h" // připojení knihovny DHT #include #include #include #include SoftwareSerial espSerial(10, 11); //Pin 2 and 3 act as RX and TX. Connect them to TX and RX of ESP8266 #define DEBUG true #define pinDHT 5 #define typDHT11 DHT11 // DHT 11 #define SCREEN_WIDTH 128 // OLED display width, in pixels #define SCREEN_HEIGHT 64 // OLED display height, in pixels // Declaration for SSD1306 display connected using software SPI (default case): #define OLED_MOSI 9 #define OLED_CLK 10 #define OLED_DC 11 #define OLED_CS 12 #define OLED_RESET 13 Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, OLED_MOSI, OLED_CLK, OLED_DC, OLED_RESET, OLED_CS); String mySSID = "******"; // WiFi SSID String myPWD = "*******"; // WiFi Password String myAPI = "*********"; // API Key String myHOST = "api.thingspeak.com"; String myPORT = "80"; String tepFIELD = "field1"; String humField = "field2"; int teplota; int vlhkost; DHT mojeDHT(pinDHT, typDHT11); void setup() { Serial.begin(9600); // SSD1306_SWITCHCAPVCC = generate display voltage from 3.3V internally if(!display.begin(SSD1306_SWITCHCAPVCC)) { Serial.println(F("SSD1306 allocation failed")); for(;;); // Don't proceed, loop forever } mojeDHT.begin(); espSerial.begin(115200); vypsani_hodnot_na_display(); // Draw 'stylized' characters espData("AT+RST", 1000, DEBUG); //Reset the ESP8266 module espData("AT+CWMODE=1", 1000, DEBUG); //Set the ESP mode as station mode espData("AT+CWJAP=\""+ mySSID +"\",\""+ myPWD +"\"", 1000, DEBUG); //Connect to WiFi network /*while(!esp.find("OK")) { //Wait for connection }*/ delay(1000); } void loop() { /* Here, I'm using the function random(range) to send a random value to the ThingSpeak API. You can change this value to any sensor data so that the API will show the sensor data */ // cidloDHT11 float tep = mojeDHT.readTemperature(); float vlh = mojeDHT.readHumidity(); // kontrola, jestli jsou načtené hodnoty čísla pomocí funkce isnan if (isnan(tep) || isnan(vlh)) { // při chybném čtení vypiš hlášku Serial.println("Chyba při čtení z DHT senzoru!"); } else { // pokud jsou hodnoty v pořádku, // vypiš je po sériové lince Serial.print("Teplota: "); Serial.print(tep); Serial.print(" stupnu Celsia, "); Serial.print("vlhkost: "); Serial.print(vlh); Serial.println(" %"); } // pauza pro přehlednější výpis delay(2000); // cidlo DHT11 teplota = (tep); // Send a random number between 1 and 1000 vlhkost = (vlh); String sendData = "GET /update?api_key="+ myAPI +"&"+ tepFIELD +"="+String(teplota) +","+ "&field2" +"=" +String(vlhkost); // String sendData = "GET /update?api_key="+ myAPI + "&field1" +"="+String(t) +","+ "&field2" +"=" +String(h); Serial.print(sendData); espData("AT+CIPMUX=1", 1000, DEBUG); //Allow multiple connections espData("AT+CIPSTART=0,\"TCP\",\""+ myHOST +"\","+ myPORT, 1000, DEBUG); espData("AT+CIPSEND=0," +String(sendData.length()+4),1000,DEBUG); espSerial.find(">"); espSerial.println(sendData); Serial.print("Value to be sent: "); Serial.println(teplota); Serial.println(vlhkost); espData("AT+CIPCLOSE=0",1000,DEBUG); delay(10000); } void vypsani_hodnot_na_display(void) { display.clearDisplay(); display.setTextSize(3); // Normal 1:1 pixel scale display.setTextColor(SSD1306_WHITE); // Draw white text display.setCursor(0,0); // Start at top-left corner display.println(F("We Can Do it!")); display.setTextSize(1); // Draw 2X-scale text display.setTextColor(SSD1306_BLACK, SSD1306_WHITE); // Draw 'inverse' text display.println(3.141592); display.setTextSize(1); // Draw 2X-scale text display.setTextColor(SSD1306_WHITE); display.print(F("0x")); display.println(0xDEADBEEF, HEX); display.display(); delay(2000); } String espData(String command, const int timeout, boolean debug) { Serial.print("AT Command ==> "); Serial.print(command); Serial.println(" "); String response = ""; espSerial.println(command); long int time = millis(); while ( (time + timeout) > millis()) { while (espSerial.available()) { char c = espSerial.read(); response += c; } } if (debug) { //Serial.print(response); } return response; }