/*
To view the data:
1. Go to http://www.hivemq.com/demos/websocket-client/
2. Click "Connect"
3. Under Subscriptions, click "Add New Topic Subscription"
4. In the Topic field, type "/swa/temperature" then click "Subscribe"
5. Under Publish, set the topic field "/swa/commands"
6. In the field "Message" type "temp" to get temperature value
*/
#include <WiFi.h>
#include <PubSubClient.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <CommandParser.h>
#define oneWireBus 5
#define BUFF_SIZE 64
#define INTERVAL 60000
const char *ssid = "Wokwi-GUEST";
const char *pass = "";
// MQTT Server Parameters
const char *mqttServer = "broker.hivemq.com";
const char *mqtt_topic = "/swa/temperature";
const char *mqtt_topic_subscribed = "/swa/commands";
const int mqttPort = 1883;
unsigned long last_time = 0;
float temp;
char temp_str[5];
char buffer[BUFF_SIZE];
bool buffer_rdy = false;
// MQTT client
WiFiClient wifiClient;
PubSubClient mqttClient(wifiClient);
OneWire oneWire(oneWireBus);
DallasTemperature sensors(&oneWire);
typedef CommandParser<> MyCommandParser;
MyCommandParser parser;
void connectWiFi() {
Serial.print("Connectiog to ");
WiFi.begin(ssid, pass);
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.print("Connected.");
}
void setupMQTT() {
mqttClient.setServer(mqttServer, mqttPort);
// set the callback function
mqttClient.setCallback(callback);
}
void reconnect() {
Serial.println("Connecting to MQTT Broker...");
while (!mqttClient.connected()) {
Serial.println("Reconnecting to MQTT Broker..");
String clientId = "ESP32Client-";
clientId += String(random(0xffff), HEX);
if (mqttClient.connect(clientId.c_str())) {
Serial.println("Connected.");
// subscribe to topic
mqttClient.subscribe(mqtt_topic_subscribed);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Callback - ");
Serial.print("Message:");
unsigned int len = length;
if(len > BUFF_SIZE){
len = BUFF_SIZE;
}
for (int i = 0; i < len; i++) {
buffer[i] = (char)payload[i];
}
buffer[len] = '\0';
Serial.println(buffer);
buffer_rdy = true;
}
void cmd_temperature(MyCommandParser::Argument *args, char *response) {
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);
dtostrf(temp, 4, 1, temp_str);
snprintf(response, MyCommandParser::MAX_RESPONSE_SIZE, "Temperature %s C",temp_str);
}
void setup() {
Serial.begin(9600);
sensors.begin();
parser.registerCommand("temp", "", &cmd_temperature);
connectWiFi();
setupMQTT();
}
void loop() {
if (!mqttClient.connected())
reconnect();
mqttClient.loop();
long now = millis();
if (now - last_time >= INTERVAL) {
sensors.requestTemperatures();
temp = sensors.getTempCByIndex(0);
dtostrf(temp, 4, 1, temp_str);
// Publishing data throgh MQTT
Serial.println(temp_str);
mqttClient.publish(mqtt_topic, temp_str);
last_time = now;
}
if(buffer_rdy) {
char response[MyCommandParser::MAX_RESPONSE_SIZE];
parser.processCommand(buffer, response);
// Publishing data throgh MQTT
mqttClient.publish(mqtt_topic, response);
Serial.println(response);
buffer_rdy = false;
}
}