Comments (2)
In order to receive messages you need to subscribe to topics after connecting to the broker:
client.subscribe(outTopic);
Only then are the messages forwarded to the device by the broker.
from arduino-mqtt.
thanks! but it still not working... can you help me one more time?
#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>
/* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */
//Only edit the settings in this section
/* WIFI Settings /
// Name of wifi network
const char ssid = "Casa 2.4";
// Password to wifi network
const char* password = "%Cvap1992";
/* Web Updater Settings /
// Host Name of Device
const char host = "MK-DoorSensor1";
// Path to access firmware update page (Not Neccessary to change)
const char* update_path = "/firmware";
// Username to access the web update page
const char* update_username = "gmail.com";
// Password to access the web update page
const char* update_password = "";
/* MQTT Settings /
// Topic which listens for commands
char outTopic = "MK-SmartHouse/security/MK-DoorSensor1";
//MQTT Server IP Address
const char* server = "192.168.15.208";
//Unique device ID
const char* mqttDeviceID = "MK-SmartHouseDevice1";
/* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */
//the time when the sensor outputs a low impulse
long unsigned int lowIn;
//the amount of milliseconds the sensor has to be low
//before we assume all detection has stopped
long unsigned int pause = 100;
//sensor variables
boolean lockLow = true;
boolean takeLowTime;
//the digital pin connected to the door sensor's output
int sensorPin = 12;
//webserver
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;
//MQTT
WiFiClient net;
MQTTClient client;
//Time Variable
unsigned long lastMillis = 0;
int channel1 = 13;
int channel3 = 16;
//Connect to WiFI and MQTT
void connect();
//Setup pins, wifi, webserver and MQTT
void setup()
{
pinMode(sensorPin, INPUT);
digitalWrite(sensorPin, LOW);
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
client.begin(server, net);
client.subscribe(outTopic);
client.onMessage(messageReceived);
connect();
MDNS.begin(host);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
MDNS.addService("http", "tcp", 80);
}
//Connect to wifi and MQTT
void connect()
{
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}
while (!client.connect(mqttDeviceID))
{
delay(1000);
}
}
void loop()
{
// MQTT Loop
client.loop();
delay(10);
// Make sure device is connected
if(!client.connected())
{
connect();
}
httpServer.handleClient();
//Sensor Detection
if(digitalRead(sensorPin) == HIGH)
{
if(lockLow)
{
//makes sure we wait for a transition to LOW before any further output is made:
lockLow = false;
client.publish(outTopic, "OPEN");
delay(50);
}
takeLowTime = true;
}
if(digitalRead(sensorPin) == LOW)
{
if(takeLowTime)
{
lowIn = millis(); //save the time of the transition from high to LOW
takeLowTime = false; //make sure this is only done at the start of a LOW phase
}
//if the sensor is low for more than the given pause,
//we assume that no more detection is going to happen
if(!lockLow && millis() - lowIn > pause)
{
//makes sure this block of code is only executed again after
//a new detection sequence has been detected
lockLow = true;
client.publish(outTopic, "CLOSED");
delay(50);
}
}
}
void messageReceived(String &topic, String &payload)
{
String msgString = payload;
Serial.println("incoming: " + topic + " - " + payload);
if (msgString == "Z1ON")
{
digitalWrite(channel1,1);
delay(250);
}
else if (msgString == "Z1OFF")
{
digitalWrite(channel1,0);
delay(250);
}
else if (msgString == "Z2ON")
{
digitalWrite(LED_BUILTIN,HIGH);
delay(250);
}
else if (msgString == "Z2OFF")
{
digitalWrite(LED_BUILTIN,LOW);
delay(250);
}
else if (msgString == "Z3ON")
{
digitalWrite(channel3,1);
delay(250);
}
else if (msgString == "Z3OFF")
{
digitalWrite(channel3,0);
delay(250);
} //This sensor does not recieve anything from MQTT Server so this is blank
}
from arduino-mqtt.
Related Issues (20)
- MQTT reconnection when not disconnected. ESP8266 HOT 6
- Unreliable behaviour with reconnections HOT 3
- MQTT HOT 1
- Websockets support HOT 3
- MQTT SSL connection issue with QoS1 and QoS2 messages HOT 1
- DCU: cannot connect to local MQTT broker HOT 1
- This library uses some posix functions that are not provided for Uno R4 HOT 3
- Subscribe query HOT 1
- Release version `v2.5.1` different of master version HOT 1
- 0
- Error connect MQTT SSL/TLS with module CBU use platform libretiny
- RP2040 seems to work fine HOT 2
- payload length / reconnect HOT 1
- Random error in MQTTClient::loop() function HOT 2
- Cosmetics only in MQTTClient.h / MQTTClient(...)
- Wrong version in README.md?
- ESP32 PSRAM for MQTT buffer?
- if available bytes on the network is available, close connection. HOT 1
- MQTT5 HOT 1
- subscribe fails HARD
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from arduino-mqtt.