Added MQTT with DHT11 Sensor
This commit is contained in:
		
							
								
								
									
										3
									
								
								mqtt-dht11-sensor/ardunio_secrets.h
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										3
									
								
								mqtt-dht11-sensor/ardunio_secrets.h
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,3 @@
 | 
			
		||||
//arduino_secrets.h header file
 | 
			
		||||
#define SECRET_SSID "xxxx"
 | 
			
		||||
#define SECRET_PASS "xxxx"
 | 
			
		||||
							
								
								
									
										98
									
								
								mqtt-dht11-sensor/mqtt.ino
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										98
									
								
								mqtt-dht11-sensor/mqtt.ino
									
									
									
									
									
										Normal file
									
								
							@@ -0,0 +1,98 @@
 | 
			
		||||
 | 
			
		||||
#include <ArduinoJson.h>
 | 
			
		||||
#include <ArduinoJson.hpp>
 | 
			
		||||
#include <WiFiS3.h>
 | 
			
		||||
#include <PubSubClient.h>
 | 
			
		||||
#include <Wire.h>
 | 
			
		||||
#include <DHT.h> // Replace "Adafruit_BME680.h" with "DHT.h"
 | 
			
		||||
#include "arduino_secrets.h"
 | 
			
		||||
 | 
			
		||||
#define DHTPIN 8
 | 
			
		||||
#define DHTTYPE DHT11
 | 
			
		||||
DHT dht(DHTPIN, DHTTYPE);
 | 
			
		||||
 | 
			
		||||
// please enter your sensitive data in the Secret tab/arduino_secrets.h
 | 
			
		||||
char ssid[] = SECRET_SSID; // your network SSID (name)
 | 
			
		||||
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
 | 
			
		||||
const char* mqtt_server = "192.168.10.7"; //IP of your MQTT Broker
 | 
			
		||||
 | 
			
		||||
WiFiClient espClient;
 | 
			
		||||
PubSubClient client(espClient);
 | 
			
		||||
unsigned long lastMsg = 0;
 | 
			
		||||
#define MSG_BUFFER_SIZE (50)
 | 
			
		||||
char msg[MSG_BUFFER_SIZE];
 | 
			
		||||
int value = 0;
 | 
			
		||||
 | 
			
		||||
void setup_wifi() {
 | 
			
		||||
  delay(100);
 | 
			
		||||
  Serial.println();
 | 
			
		||||
  Serial.print("Connecting to ");
 | 
			
		||||
  Serial.println(ssid);
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
 | 
			
		||||
  while (WiFi.status() != WL_CONNECTED) {
 | 
			
		||||
    delay(500);
 | 
			
		||||
    Serial.print(".");
 | 
			
		||||
  }
 | 
			
		||||
 | 
			
		||||
  Serial.println("");
 | 
			
		||||
  Serial.println("WiFi connected");
 | 
			
		||||
  Serial.println("IP address: ");
 | 
			
		||||
  Serial.println(WiFi.localIP());
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void callback(char* topic, byte* payload, unsigned int length) {
 | 
			
		||||
  // Handle incoming MQTT messages if needed
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void reconnect() {
 | 
			
		||||
  while (!client.connected()) {
 | 
			
		||||
    Serial.print("Attempting MQTT connection...");
 | 
			
		||||
    String clientId = "ESP8266Client-";
 | 
			
		||||
    clientId += String(random(0xffff), HEX);
 | 
			
		||||
    if (client.connect(clientId.c_str())) {
 | 
			
		||||
      Serial.println("connected");
 | 
			
		||||
      client.publish("outTopic", "hello world");
 | 
			
		||||
      client.subscribe("inTopic");
 | 
			
		||||
    } else {
 | 
			
		||||
      Serial.print("failed, rc=");
 | 
			
		||||
      Serial.print(client.state());
 | 
			
		||||
      Serial.println(" try again in 5 seconds");
 | 
			
		||||
      delay(5000);
 | 
			
		||||
    }
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void setup() {
 | 
			
		||||
  Serial.begin(115200);
 | 
			
		||||
  while (!Serial);
 | 
			
		||||
  Serial.println(F("DHT11 test"));
 | 
			
		||||
 | 
			
		||||
  dht.begin();
 | 
			
		||||
 | 
			
		||||
  setup_wifi();
 | 
			
		||||
  client.setServer(mqtt_server, 1883);
 | 
			
		||||
  client.setCallback(callback);
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void loop() {
 | 
			
		||||
  if (!client.connected()) {
 | 
			
		||||
    reconnect();
 | 
			
		||||
  }
 | 
			
		||||
  client.loop();
 | 
			
		||||
  StaticJsonDocument<32> doc;
 | 
			
		||||
  char output[55];
 | 
			
		||||
 | 
			
		||||
  long now = millis();
 | 
			
		||||
  if (now - lastMsg > 5000) {
 | 
			
		||||
    lastMsg = now;
 | 
			
		||||
    float temp = dht.readTemperature();
 | 
			
		||||
    float humidity = dht.readHumidity();
 | 
			
		||||
    doc["t"] = temp;
 | 
			
		||||
    doc["h"] = humidity;
 | 
			
		||||
 | 
			
		||||
    serializeJson(doc, output);
 | 
			
		||||
    client.publish("/home/sensors", output);
 | 
			
		||||
  }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user