#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <ArduinoJson.h>
#include <SimpleTimer.h>
#include "time.h"
#include <FastBot.h>

//https://github.com/GyverLibs/FastBot
//https://mqtt.org/
//https://github.com/knolleary/pubsubclient

#define BOT_TOKEN "6780587077:AAGfuAjlpo__OTIEb-kCSRe8dQCd1jEZPis"
#define CHAT_ID "460540516"

FastBot bot(BOT_TOKEN);

SimpleTimer timer_ds18b20(2000);
SimpleTimer timer_main(5000);

const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* mqtt_server = "test.mosquitto.org";
const int mqtt_port = 1884;
const char* mqtt_topic = "TOIP/lab4/boiler/temperature"; // MQTT topic to subscribe and publish
const char* mqtt_username = "wo";
const char* mqtt_password = "writeonly";

const int DS18D20_PIN = 33;        // GPIO pin where DHT22 is connected

OneWire oneWire(DS18D20_PIN);
DallasTemperature sensors(&oneWire);

WiFiClient espClient;
PubSubClient client(espClient);

const long  gmtOffset_sec = 3 * 3600;
const int   daylightOffset_sec = 0;

String temperature;

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);

  WiFi.mode(WIFI_STA);
  WiFi.begin(ssid, password, 5);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  Serial.println("");
  Serial.println("WiFi connected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
}

void setup() {
  Serial.begin(115200);

  bot.setChatID(CHAT_ID);

  bot.attach(messageParser);

  setup_wifi();
  client.setServer(mqtt_server, mqtt_port);

  sensors.begin();
  // Initialize and get the time
  configTime(gmtOffset_sec, daylightOffset_sec, "pool.ntp.org");

  temperature = getTemperature();
  Serial.println("Temperature: " + temperature);
}

void messageParser(FB_msg& msg) {
  if (msg.text == "/current_temperature") {
    bot.sendMessage("Current temperature: " + temperature);
  }
}

void loop() {
  bot.tick();
  if (!client.connected()) {
    reconnect();
  }
  client.loop();

  if (timer_ds18b20.isReady()) {
    temperature = getTemperature();
    timer_ds18b20.reset();
  }

  if(timer_main.isReady()){
    client.publish(mqtt_topic, temperature.c_str());
    Serial.println("Temperature published: " + temperature);
    timer_main.reset();
  } 
}

float getTemperature() {
  sensors.requestTemperatures();
  return sensors.getTempCByIndex(0);
}

String printLocalTime(){
  struct tm timeinfo;
  if(!getLocalTime(&timeinfo)){
    Serial.println("Failed to obtain time");
    return "Failed to obtain time";
  }
  
  char output[30];
  strftime(output, 30, "%d, %m, %Y %H:%M:%S", &timeinfo);
  
  return output;
}

void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Create a random client ID
    String clientId = "ESPClient-";
    clientId += String(random(0xffff), HEX);
    // Attempt to connect
    if (client.connect(clientId.c_str(), mqtt_username, mqtt_password)) {
      Serial.println("connected");
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying
      delay(5000);
    }
  }
}
Loading
esp32-s2-devkitm-1
Loading
ds18b20