// Date and time functions using a DS1307 RTC connected via I2C and Wire lib #include "RTClib.h" #include #include #include #include #include "EspMQTTClient.h" #include const char* ssid = "Ati"; const char* password = "ati@1987"; WebServer server(80); RTC_DS1307 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; Pwm pwm = Pwm(); const int extruder = 3; EspMQTTClient client( "Ati", "ati@1987", "3.128.220.209", // MQTT Broker server ip "fabacademy", // Can be omitted if not needed "fabacademy", // Can be omitted if not needed "fabacademy", // Client name that uniquely identify your device 1883 // The MQTT port, default to 1883. this line can be omitted ); DateTime now; String datetime; void onConnectionEstablished() { now = rtc.now(); datetime = "The time is: "+String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " ( "+ daysOfTheWeek[now.dayOfTheWeek()] +" ) " + String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC); client.subscribe("fabacademy/input", [](const String & payload) { now = rtc.now(); datetime = String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " ( "+ daysOfTheWeek[now.dayOfTheWeek()] +" ) " + String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC); if(payload.toInt() >= 0){ pwm.write(extruder, payload.toInt()); } client.publish("fabacademy/datetime", datetime.c_str()); // You can activate the retain flag by setting the third parameter to true }); client.subscribe("fabacademy/update", [](const String & payload) { now = rtc.now(); datetime = String(now.year(), DEC) + '/' + String(now.month(), DEC) + '/' + String(now.day(), DEC) + " ( "+ daysOfTheWeek[now.dayOfTheWeek()] +" ) " + String(now.hour(), DEC) + ':' + String(now.minute(), DEC) + ':' + String(now.second(), DEC); client.publish("fabacademy/datetime", datetime.c_str()); // You can activate the retain flag by setting the third parameter to true }); client.publish("fabacademy/datetime", datetime.c_str()); // You can activate the retain flag by setting the third parameter to true } void setup () { Serial.begin(115200); pwm.write(extruder, 0); if (! rtc.begin()) { Serial.println("Couldn't find RTC"); Serial.flush(); while (1) delay(10); } if (! rtc.isrunning()) { Serial.println("RTC is NOT running, let's set the time!"); // When time needs to be set on a new device, or after a power loss, the // following line sets the RTC to the date & time this sketch was compiled rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); } client.enableDebuggingMessages(); // Enable debugging messages sent to serial output client.enableHTTPWebUpdater(); // Enable the web updater. User and password default to values of MQTTUsername and MQTTPassword. These can be overridded with enableHTTPWebUpdater("user", "password"). client.enableOTA(); // Enable OTA (Over The Air) updates. Password defaults to MQTTPassword. Port is the default OTA port. Can be overridden with enableOTA("password", port). client.enableLastWillMessage("fabacademy/lastwill", "I am going offline"); // You can activate the retain flag by setting the third parameter to true } void loop () { client.loop(); }