mqtt works
This commit is contained in:
parent
b4f71f60fa
commit
bf6c241c04
@ -29,6 +29,7 @@ lib_deps =
|
|||||||
devyte/ESPAsyncDNSServer@^1.0.0
|
devyte/ESPAsyncDNSServer@^1.0.0
|
||||||
me-no-dev/ESPAsyncUDP
|
me-no-dev/ESPAsyncUDP
|
||||||
makuna/NeoPixelBus @ ^2.8.0
|
makuna/NeoPixelBus @ ^2.8.0
|
||||||
|
knolleary/PubSubClient@^2.8
|
||||||
lib_ldf_mode = chain+
|
lib_ldf_mode = chain+
|
||||||
monitor_filters =
|
monitor_filters =
|
||||||
default
|
default
|
||||||
@ -37,23 +38,10 @@ board_build.filesystem = littlefs
|
|||||||
board_build.f_cpu = 80000000L
|
board_build.f_cpu = 80000000L
|
||||||
|
|
||||||
[env:esp32c3_supermini]
|
[env:esp32c3_supermini]
|
||||||
; platform = espressif32
|
|
||||||
; board = esp32-c3-devkitm-1
|
|
||||||
|
|
||||||
; platform = https://github.com/Jason2866/platform-espressif32.git#Arduino/IDF5
|
|
||||||
; board = lolin_c3_mini
|
|
||||||
|
|
||||||
board = dfrobot_beetle_esp32c3
|
board = dfrobot_beetle_esp32c3
|
||||||
platform = espressif32
|
platform = espressif32
|
||||||
framework = arduino
|
framework = arduino
|
||||||
; board_build.f_cpu = 80000000L
|
|
||||||
monitor_speed = 460800
|
monitor_speed = 460800
|
||||||
lib_deps =
|
lib_deps =
|
||||||
fastled/FastLED@^3.7.0
|
fastled/FastLED@^3.7.0
|
||||||
|
knolleary/PubSubClient@^2.8
|
||||||
; [env:dfrobot_beetle_esp32c3]
|
|
||||||
; platform = espressif32
|
|
||||||
; board = dfrobot_beetle_esp32c3
|
|
||||||
; framework = arduino
|
|
||||||
; lib_deps =
|
|
||||||
; fastled/FastLED@^3.7.0
|
|
||||||
|
|||||||
@ -8,7 +8,7 @@
|
|||||||
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
|
#define _TASK_SLEEP_ON_IDLE_RUN // Enable 1 ms SLEEP_IDLE powerdowns between runs if no callback methods were invoked during the pass
|
||||||
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
#define _TASK_STATUS_REQUEST // Compile with support for StatusRequest functionality - triggering tasks on status change events in addition to time only
|
||||||
|
|
||||||
// #define ENABLE_MQTT
|
#define ENABLE_MQTT
|
||||||
#define ENABLE_WIFI
|
#define ENABLE_WIFI
|
||||||
// #define ENABLE_FS
|
// #define ENABLE_FS
|
||||||
// #define ENABLE_PINFINDER
|
// #define ENABLE_PINFINDER
|
||||||
@ -146,6 +146,9 @@ void newBrightness(){
|
|||||||
delay(100);
|
delay(100);
|
||||||
FastLED.setBrightness(newBrightness);
|
FastLED.setBrightness(newBrightness);
|
||||||
FastLED.show();
|
FastLED.show();
|
||||||
|
#ifdef ENABLE_MQTT
|
||||||
|
MQTT_publish("hanglamp/brightness", String(newBrightness));
|
||||||
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
void controlsCallback(ControlEvent event){
|
void controlsCallback(ControlEvent event){
|
||||||
@ -265,10 +268,6 @@ void setup() {
|
|||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
|
|
||||||
#ifdef ENABLE_MQTT
|
|
||||||
MQTT_loop();
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifdef ENABLE_OTA
|
#ifdef ENABLE_OTA
|
||||||
OTA_loop();
|
OTA_loop();
|
||||||
#endif
|
#endif
|
||||||
@ -303,9 +302,6 @@ void loop() {
|
|||||||
|
|
||||||
delay(1000);
|
delay(1000);
|
||||||
|
|
||||||
// Led_loop();
|
|
||||||
|
|
||||||
|
|
||||||
String command;
|
String command;
|
||||||
|
|
||||||
while(Serial.available()) {
|
while(Serial.available()) {
|
||||||
|
|||||||
70
src/MQTT.cpp
70
src/MQTT.cpp
@ -1,13 +1,9 @@
|
|||||||
#ifdef ENABLE_MQTT
|
|
||||||
#include "MQTT.h"
|
#include "MQTT.h"
|
||||||
|
|
||||||
#include <PubSubClient.h>
|
#include <PubSubClient.h>
|
||||||
|
|
||||||
|
|
||||||
#include <ESP8266WiFi.h>
|
|
||||||
#include <WiFiUdp.h>
|
|
||||||
|
|
||||||
|
|
||||||
WiFiClient espClient;
|
WiFiClient espClient;
|
||||||
PubSubClient client(espClient);
|
PubSubClient client(espClient);
|
||||||
|
|
||||||
@ -17,12 +13,23 @@ std::vector<SubscribedChannel> * channels = new std::vector<SubscribedChannel>()
|
|||||||
char mqtt_server[40] = "manus";
|
char mqtt_server[40] = "manus";
|
||||||
char mqtt_port[6] = "1883";
|
char mqtt_port[6] = "1883";
|
||||||
|
|
||||||
|
|
||||||
|
uint32_t chipId = 0;
|
||||||
|
|
||||||
void connect() {
|
void connect() {
|
||||||
|
|
||||||
Serial.print("Attempting MQTT connection...");
|
|
||||||
|
if (WiFi.status() != WL_CONNECTED){
|
||||||
|
Serial.println("Wifi not connected");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.println("Attempting MQTT connection...");
|
||||||
// Create a random client ID
|
// Create a random client ID
|
||||||
String clientId = "Konijntje-";
|
String clientId = "Hanglamp-";
|
||||||
clientId += String(ESP.getChipId());
|
clientId += String(chipId, 16);
|
||||||
|
Serial.printf("MQTT: Connecting as %s\n", clientId.c_str());
|
||||||
|
|
||||||
// Attempt to connect
|
// Attempt to connect
|
||||||
if (client.connect(clientId.c_str())) {
|
if (client.connect(clientId.c_str())) {
|
||||||
Serial.println("connected");
|
Serial.println("connected");
|
||||||
@ -81,7 +88,11 @@ void MQTT_callback(char* topic, uint8_t * payload, unsigned int length){
|
|||||||
|
|
||||||
void MQTT_setup(){
|
void MQTT_setup(){
|
||||||
|
|
||||||
Serial.println("MQTT_init");
|
for (int i = 0; i < 17; i = i + 8) {
|
||||||
|
chipId |= ((ESP.getEfuseMac() >> (40 - i)) & 0xff) << i;
|
||||||
|
}
|
||||||
|
|
||||||
|
Serial.printf("MQTT_init: %06x\n", chipId);
|
||||||
|
|
||||||
client.setServer(mqtt_server, String(mqtt_port).toInt());
|
client.setServer(mqtt_server, String(mqtt_port).toInt());
|
||||||
client.setCallback(MQTT_callback);
|
client.setCallback(MQTT_callback);
|
||||||
@ -91,29 +102,32 @@ void MQTT_setup(){
|
|||||||
// sprintf(buff, "%08x", _pCallback);
|
// sprintf(buff, "%08x", _pCallback);
|
||||||
// Serial.println(buff);
|
// Serial.println(buff);
|
||||||
|
|
||||||
connect();
|
TaskHandle_t taskMqtt = NULL;
|
||||||
|
xTaskCreate(MQTT_loop, "Wifi_loop", 10000, NULL, tskIDLE_PRIORITY, &taskMqtt);
|
||||||
}
|
}
|
||||||
|
|
||||||
void MQTT_loop(){
|
void MQTT_loop(void* params){
|
||||||
|
|
||||||
#if DEBUG_GENERAL
|
while(true){
|
||||||
Serial.println("MQTT_loop()");
|
#if DEBUG_GENERAL
|
||||||
#endif
|
Serial.println("MQTT_loop()");
|
||||||
int reconnectCounter = 0;
|
#endif
|
||||||
while(!client.connected() && reconnectCounter < 3) {
|
int reconnectCounter = 0;
|
||||||
Serial.println("MQTT: connecting...");
|
while(!client.connected() && reconnectCounter < 3) {
|
||||||
reconnectCounter++;
|
Serial.println("MQTT: connecting...");
|
||||||
connect();
|
reconnectCounter++;
|
||||||
|
connect();
|
||||||
|
|
||||||
if(client.connected()){
|
if(client.connected()){
|
||||||
Serial.println("MQTT: succes!");
|
Serial.println("MQTT: succes!");
|
||||||
break;
|
break;
|
||||||
}else{
|
}else{
|
||||||
Serial.println("MQTT: connection failed, try again in 5 seconds");
|
Serial.println("MQTT: connection failed, try again in 5 seconds");
|
||||||
// Wait 5 seconds before retrying
|
// Wait 5 seconds before retrying
|
||||||
delay(5000);
|
vTaskDelay(5000/portTICK_PERIOD_MS);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
client.loop();
|
||||||
|
vTaskDelay(100/portTICK_PERIOD_MS);
|
||||||
}
|
}
|
||||||
client.loop();
|
|
||||||
}
|
}
|
||||||
#endif
|
|
||||||
@ -3,6 +3,8 @@
|
|||||||
|
|
||||||
#include "Arduino.h"
|
#include "Arduino.h"
|
||||||
|
|
||||||
|
#include <WiFi.h>
|
||||||
|
|
||||||
using namespace std;
|
using namespace std;
|
||||||
|
|
||||||
|
|
||||||
@ -17,5 +19,5 @@ struct SubscribedChannel {
|
|||||||
|
|
||||||
void MQTT_publish(const char * topic, String str);
|
void MQTT_publish(const char * topic, String str);
|
||||||
void MQTT_publish(const char * topic, const char * msg);
|
void MQTT_publish(const char * topic, const char * msg);
|
||||||
void MQTT_loop();
|
void MQTT_loop(void* params);
|
||||||
void MQTT_setup();
|
void MQTT_setup();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user