Best Resoults I’ve got with these libraryes
WIFiEsp
MQTT
Here you have my code … Fell free to try it and give me a little feedback please.
I wanna prepare you also the fitzing sketch but I’ve no so many time, sorry
…
Anycase you need:
Arduino Uno
WifiESP8266 model 01 (PIN 8 <–to-LLC-to–> TX ; PIN 9 <–to-LLC-to–> RX )
Logic Level Converter (LLC) 5 to 3.3v
Temperature Sensor (DT11 keyes in my case) (PIN 2)
PIR (PIN 3)
ARDUINO SKETCH
/*
MQTT DHT22, when "temperature, c" is sent it returns the temperature in celcius
when "humidity" is sent it returns the humidity as measured by the DHT22
the signal pin is connected to a pull-ip resistor and GPIO 2
*/
#include <WiFiEsp.h>
#include <WiFiEspClient.h>
#include <WiFiEspUdp.h>
#include <PubSubClient.h>
#include <SoftwareSerial.h>
#include <DHT.h>
#define DHTTYPE DHT11
#define DHTPIN 2
// Update these with values suitable for your network.
char* ssid = "YOURSSID";
char* password = "YOURPASSWORD";
const char* mqtt_server = "YOURmqttSERVERip";
const char* clientID = "YOURCLIENTID";
const char* outTopic = "YOURCLIENTOUTTOPIC";
const char* inTopic = "YOURCLIENTINTOPIC";
int status = WL_IDLE_STATUS; // the Wifi radio's status
// Initialize DHT sensor
// NOTE: For working with a faster than ATmega328p 16 MHz Arduino chip, like an ESP8266,
// you need to increase the threshold for cycle counts considered a 1 or 0.
// You can do this by passing a 3rd parameter for this threshold. It's a bit
// of fiddling to find the right value, but in general the faster the CPU the
// higher the value. The default for a 16mhz AVR is a value of 6. For an
// Arduino Due that runs at 84mhz a value of 30 works.
// This is for the ESP8266 processor on ESP-01
DHT dht(DHTPIN, DHTTYPE, 11); // 11 works fine for ESP8266
float humidity, temp_c; // Values read from sensor
// Generally, you should use "unsigned long" for variables that hold time
unsigned long previousMillis = 0; // will store last temp was read
const long interval = 2000; // interval at which to read sensor
WiFiEspClient espClient;
PubSubClient client(espClient);
char msg[50];
SoftwareSerial soft(8, 9); // RX, TX
const int PIR_pin = 3;
int Previous_Signal = LOW;
void setup() {
Serial.begin(115200);
soft.begin(9600);
// initialize ESP module
WiFi.init(&soft);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
dht.begin(); // initialize temperature sensor
pinMode(PIR_pin, INPUT);
}
void loop() {
int PIR_signal = digitalRead(PIR_pin);
if (PIR_signal == HIGH && Previous_Signal == LOW)
{
Previous_Signal = PIR_signal;
Serial.println("Presence Revealed");
client.publish(outTopic, "Presence");
}
else if (PIR_signal == LOW && Previous_Signal == HIGH)
{
Previous_Signal = PIR_signal;
}
if (!client.connected()) {
reconnect();
}
delay(100);
client.loop();
}
void setup_wifi() {
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, password);
}
// you're connected now, so print out the data
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
// Conver the incoming byte array to a string
payload[length] = '\0'; // Null terminator used to terminate the char array
String message = (char*)payload;
Serial.print("Message arrived on topic: [");
Serial.print(topic);
Serial.print("], ");
Serial.println(message);
if(message == "temperature"){
gettemperature();
Serial.print("Sending temperature:");
Serial.println(temp_c);
dtostrf(temp_c , 2, 2, msg);
client.publish(outTopic, msg);
} else if (message == "humidity"){
gettemperature();
Serial.print("Sending humidity:");
Serial.println(humidity);
dtostrf(humidity , 2, 2, msg);
client.publish(outTopic, msg);
}
}
void reconnect() {
// Loop until we're reconnected
delay(100);
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(clientID)) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish(outTopic, clientID);
// ... and resubscribe
client.subscribe(inTopic);
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void gettemperature() {
// Wait at least 2 seconds seconds between measurements.
// if the difference between the current time and last time you read
// the sensor is bigger than the interval you set, read the sensor
// Works better than delay for things happening elsewhere also
unsigned long currentMillis = millis();
if(currentMillis - previousMillis >= interval) {
// save the last time you read the sensor
previousMillis = currentMillis;
// Reading temperature for humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (it's a very slow sensor)
humidity = dht.readHumidity(); // Read humidity (percent)
delay(20);
temp_c = dht.readTemperature(); // Read temperature as Celcius
// Check if any reads failed and exit early (to try again).
if (isnan(humidity) || isnan(temp_c)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
}
}
For now I work with MQTTFX to recognise the pub and sub, not already write down a role for OH2.