Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Example MQTT implementation scketch for an ESP8266 board and a DS18B20 temperature probe sensor
#include <ESP8266WiFi.h> // Library for WiFi
#include <OneWire.h> // Library for DS18B20 sensor
#include <DallasTemperature.h> // Library for DS18B20 sensor
#include <Adafruit_MQTT.h> // MQTT client
#include <Adafruit_MQTT_Client.h> // MQTT client
#include "credentials.h"
// Content - credentials.h ##########################################################
// ###################################################################################
// WiFi Credentials
// #define WLAN_SSID "YOUR SSID"
// #define WLAN_PASSWORD "YOUR PASSWORD"
// MQTT Credentials
// #define MQTT_SERVER "YOUR MQTT BROKER IP"
// #define MQTT_USER "MQTT USER"
// #define MQTT_PASSWORD "MQTT PASSWORD"
// ###################################################################################
// ###################################################################################
// Network Configuration
IPAddress ip(192, 168, 0, 102); // Your device IP
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
// MQTT client configuration
WiFiClient esp_client;
Adafruit_MQTT_Client mqtt(&esp_client, MQTT_SERVER, 1883, MQTT_USER, MQTT_PASSWORD);
Adafruit_MQTT_Publish current_temp = Adafruit_MQTT_Publish(&mqtt, "current_temp", MQTT_QOS_1); // Define MQTT topic
// DS18B20 Sensor Libraries Configuration
OneWire ourWire(4); // Set D2(GPIO-4) pin as OneWire bus
DallasTemperature sensors(&ourWire); // Declare sensor object
// Global variables definition
float temp; //Define variable to store temp readings
unsigned long previousMillis = 0; //Temporal variable
const long interval = 2000; //Set minimal interval between readings
void MQTT_connect();
void setup() {
Serial.begin(115200);
delay(10);
// Connect to WiFi network
Serial.println(); Serial.println();
Serial.print("Connecting to ");
Serial.println(WLAN_SSID);
WiFi.config(ip, gateway, subnet, dns1, dns2);
WiFi.mode(WIFI_STA);
WiFi.begin(WLAN_SSID, WLAN_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Initialize sensor
sensors.begin(); // Initialize DS18B20 sensor
}
void loop() {
// Send reading via MQTT message
MQTT_connect();
read_sensor();
// ping the server to keep the mqtt connection alive
if (! mqtt.ping()) {
mqtt.disconnect();
}
}
// FUNCTIONS #########################################################################
// ###################################################################################
// Get sensor readings
void read_sensor() {
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
sensors.requestTemperatures(); //Request temp readings
temp = sensors.getTempCByIndex(0); //Request temp in C by index
Serial.print(F("\nSending reading: "));
Serial.print(temp);
Serial.print("...");
if (! current_temp.publish(temp)) {
Serial.println(F("Failed"));
} else {
Serial.println(F("OK!"));
}
}
}
// MQTT functions
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
Serial.println("MQTT Connected!");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.