#include #include #include #include #include #include #include // Aşağıdaki kimlik bilgilerini değiştirin, böylece ESP8266'nız Wifi ağınıza bağlanır //const char* ssid = "TurkTelekom_TB2D3"; //const char* password = "PFvTEbfW"; //const char* ssid = "TP-Link_AAB6"; //const char* password = "91270917"; const char* ssid = "MET_İleri_Teknoloji_Sistemleri"; const char* password = "Met2016**"; // Aşağıdaki verileri IPv4 adresinize göre değiştirin MQTT broker oluşturacak. Cmd'ye ipconfig yazın ve Wireless LAN adapter Wi-Fi: Kısmındaki IPv4 adresinizi girin. const char* mqtt_server = "192.168.1.99"; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, "pool.ntp.org"); // espClient'i başlatır. Ev otomasyon sisteminizde çalışan birden fazla ESP varsa, espClient adını değiştirmelisiniz. WiFiClient espClient3; PubSubClient client(espClient3); //const int garaj_haraket_pin = 16; //const int bahce_sulama_ac_pin = 14; //const int bahce_lamba_pin = 5; //const int garaj_lamba_pin = 4; #define arka_taraf_lambalar_out D0; #define METWave_lambalar_out D1; #define talaslı_imalat_lambalar_out D2; #define mekanik_imalat_lambalar_out D3; #define elektrik_montaj_lambalar_out D10; #define klima_ofis_pin_out D4; #define klima_toplantı_pin_out D12; #define kompresor_pin_out D5; #define arka_taraf_lambalar_in D6; #define METWave_lambalar_in D7; #define talaslı_imalat_lambalar_in D8; #define mekanik_imalat_lambalar_in D9; #define elektrik_montaj_lambalar_in D11; //INPUT VE OUTPUTLAR 3 AYRI NODEMCU İÇİN AYRILACAK int a = 0; int b = 0; int c = 0; int d = 0; int e = 0; int f = 0; int g = 0; //int garaj_haraket; //int boot_pin_resetter; //String garaj_haraket_str; //see last code block below use these to convert the float that you get back from DHT to a string =str //char garaj_haraket_char[50]; //Week Days String weekDays[7]={"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //Month names String months[12]={"January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"}; long now = millis(); long lastMeasure = 0; //long lastMeasure = 0; void setup_wifi() { delay(10); // Wifi ağına bağlanıyoruz. Serial.println(); Serial.print("Baglaniyor ?!: "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.print("WiFi baglandi - ESP IP adres: "); Serial.println(WiFi.localIP()); } void callback(String topic, byte* message, unsigned int length) { Serial.print("Mesaj topic'e ulaşti: "); Serial.print(topic); Serial.print(". Mesaj: "); String messageTemp; for (int i = 0; i < length; i++) { Serial.print((char)message[i]); messageTemp += (char)message[i]; } Serial.println(); if (topic == "lambalar/talaslilambalar") || if (talaslı_imalat_lambalar_in == HIGH) { if (messageTemp == "true") { digitalWrite(talaslı_imalat_lambalar_out, LOW); Serial.print("Talaşlı İmalat Lambalar Açık"); } if (messageTemp == "false") || if (talaslı_imalat_lambalar_in == LOW) { digitalWrite(talaslı_imalat_lambalar_out, HIGH); Serial.print("Talaşlı İmalat Lambalar Kapalı"); } } if (topic == "lambalar/mekaniklambalar") || if (mekanik_imalat_lambalar_in == HIGH) { if (messageTemp == "true") { digitalWrite(mekanik_imalat_lambalar_out, LOW); Serial.print("Mekanik İmalat Lambalar Açık"); } if (messageTemp == "false") || if (mekanik_imalat_lambalar_in == LOW) { digitalWrite(mekanik_imalat_lambalar_out, HIGH); Serial.print("Mekanik İmalat Lambalar Kapalı"); } } if (topic == "lambalar/elektriklambalar") || if (elektrik_montaj_lambalar_in == HIGH) { if (messageTemp == "true") { digitalWrite(elektrik_montaj_lambalar_out, LOW); Serial.print("Elektrik Montaj Lambalar Açık"); } if (messageTemp == "false") || if (elektrik_montaj_lambalar_in == LOW) { digitalWrite(elektrik_montaj_lambalar_out, HIGH); Serial.print("Elektrik Montaj Lambalar Kapalı"); } } if (topic == "lambalar/arkalambalar") || if (arka_taraf_lambalar_in == HIGH) { if (messageTemp == "true") { digitalWrite(arka_taraf_lambalar_out, LOW); Serial.print("Arka Taraf Lambalar Açık"); } if (messageTemp == "false") || if (arka_taraf_lambalar_in == LOW) { digitalWrite(arka_taraf_lambalar_out, HIGH); Serial.print("Arka Taraf Lambalar Kapalı"); } } if (topic == "lambalar/metwavelambalar") || if (METWave_lambalar_in == HIGH) { if (messageTemp == "true") { digitalWrite(METWave_lambalar_out, LOW); Serial.print("METWave Lambalar Açık"); } if (messageTemp == "false") || if (METWave_lambalar_in == LOW) { digitalWrite(METWave_lambalar_out, HIGH); Serial.print("METWave Lambalar Kapalı"); } } if (topic == "klima/ofisklima") { if (messageTemp == "true") { digitalWrite(klima_ofis_pin_out, LOW); Serial.print("Ofis Kliması Açık"); } if (messageTemp == "false") { digitalWrite(klima_ofis_pin_out, HIGH); Serial.print("Ofis Kliması Kapalı"); } } if (topic == "klima/toplantıklima") { if (messageTemp == "true") { digitalWrite(klima_toplantı_pin_out, LOW); Serial.print("Toplantı Salonu Açık"); } if (messageTemp == "false") { digitalWrite(klima_toplantı_pin_out, HIGH); Serial.print("Toplantı Salonu Kliması Kapalı"); } } } void reconnect() { while (!client.connected()) { Serial.print("MQTT baglantisi kurulmaya calisiyor..."); // bağlanmayı dene if (client.connect("ESP8266Client3")) { Serial.println("MQTT baglantisi kuruldu"); client.subscribe("lambalar/talaslilambalar"); client.subscribe("lambalar/mekaniklambalar"); client.subscribe("lambalar/elektriklambalar"); client.subscribe("lambalar/arkalambalar"); client.subscribe("lambalar/metwavelambalar"); client.subscribe("klima/ofisklima"); client.subscribe("klima/toplantıklima"); } else { Serial.print("basarısız oldu, rc="); Serial.print(client.state()); Serial.println(" 5 saniye içerisinde yeniden deneniyor"); //yukarıdaki 5 saniye aşağıda delay içerisinde milisaniye cinsinden değiştirebilirsiniz delay(5000); } } } void setup() { pinMode(arka_taraf_lambalar_out, OUTPUT); pinMode(METWave_lambalar_out, OUTPUT); pinMode(talaslı_imalat_lambalar_out, OUTPUT); pinMode(mekanik_imalat_lambalar_out, OUTPUT); pinMode(klima_pin_out, OUTPUT); pinMode(kompresor_pin_out, OUTPUT); pinMode(arka_taraf_lambalar_in, INPUT); pinMode(METWave_lambalar_in, INPUT); pinMode(talaslı_imalat_lambalar_in, INPUT); pinMode(mekanik_imalat_lambalar_in, INPUT); //pinMode(bahce_lamba_pin, OUTPUT); //pinMode(garaj_lamba_pin, OUTPUT); //pinMode(bahce_sulama_ac_pin, OUTPUT); //pinMode(garaj_haraket_pin, INPUT); // Initialize a NTPClient to get time timeClient.begin(); // Set offset time in seconds to adjust for your timezone, for example: // GMT +1 = 3600 // GMT +8 = 28800 // GMT -1 = -3600 // GMT 0 = 0 timeClient.setTimeOffset(0); if (c == 0) { //digitalWrite(bahce_lamba_pin, HIGH); //digitalWrite(garaj_lamba_pin, HIGH); //digitalWrite(bahce_sulama_ac_pin, HIGH); c = 1; } Serial.begin(9600); WiFiManager wifiManager; IPAddress _ip = IPAddress(192, 168, 1, 103); IPAddress _gw = IPAddress(192, 168, 1, 1); IPAddress _sn = IPAddress(255, 255, 255, 0); //end-block2 wifiManager.setSTAStaticIPConfig(_ip, _gw, _sn); setup_wifi(); setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback); } void loop() { //pinMode(garaj_haraket_pin, INPUT); //garaj_haraket = digitalRead(garaj_haraket_pin); timeClient.update(); unsigned long epochTime = timeClient.getEpochTime(); // Serial.print("Epoch Time: "); // Serial.println(epochTime); String formattedTime = timeClient.getFormattedTime(); // Serial.print("Formatted Time: "); // Serial.println(formattedTime); int currentHour = timeClient.getHours(); // Serial.print("Hour: "); // Serial.println(currentHour); int currentMinute = timeClient.getMinutes(); // Serial.print("Minutes: "); // Serial.println(currentMinute); int currentSecond = timeClient.getSeconds(); // Serial.print("Seconds: "); // Serial.println(currentSecond); String weekDay = weekDays[timeClient.getDay()]; // Serial.print("Week Day: "); // Serial.println(weekDay); //Get a time structure struct tm *ptm = gmtime ((time_t *)&epochTime); int monthDay = ptm->tm_mday; // Serial.print("Month day: "); // Serial.println(monthDay); int currentMonth = ptm->tm_mon+1; // Serial.print("Month: "); // Serial.println(currentMonth); String currentMonthName = months[currentMonth-1]; // Serial.print("Month name: "); // Serial.println(currentMonthName); int currentYear = ptm->tm_year+1900; // Serial.print("Year: "); // Serial.println(currentYear); //Print complete date: String currentDate = String(currentYear) + "-" + String(currentMonth) + "-" + String(monthDay); // Serial.print("Current date: "); // Serial.println(currentDate); Serial.println(""); if (!client.connected()) { reconnect(); } if (!client.loop()) client.connect("ESP8266Client3"); //garaj_haraket_str.toCharArray(garaj_haraket_char, garaj_haraket_str.length() + 1); //packaging up the data to publish to mqtt whoa... //client.publish("bahce/garajharaket", garaj_haraket_char); }