mqtt works
This commit is contained in:
parent
b4f71f60fa
commit
bf6c241c04
@ -29,6 +29,7 @@ lib_deps =
|
||||
devyte/ESPAsyncDNSServer@^1.0.0
|
||||
me-no-dev/ESPAsyncUDP
|
||||
makuna/NeoPixelBus @ ^2.8.0
|
||||
knolleary/PubSubClient@^2.8
|
||||
lib_ldf_mode = chain+
|
||||
monitor_filters =
|
||||
default
|
||||
@ -37,23 +38,10 @@ board_build.filesystem = littlefs
|
||||
board_build.f_cpu = 80000000L
|
||||
|
||||
[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
|
||||
platform = espressif32
|
||||
framework = arduino
|
||||
; board_build.f_cpu = 80000000L
|
||||
monitor_speed = 460800
|
||||
lib_deps =
|
||||
fastled/FastLED@^3.7.0
|
||||
|
||||
; [env:dfrobot_beetle_esp32c3]
|
||||
; platform = espressif32
|
||||
; board = dfrobot_beetle_esp32c3
|
||||
; framework = arduino
|
||||
; lib_deps =
|
||||
; fastled/FastLED@^3.7.0
|
||||
knolleary/PubSubClient@^2.8
|
||||
|
||||
@ -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_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_FS
|
||||
// #define ENABLE_PINFINDER
|
||||
@ -146,6 +146,9 @@ void newBrightness(){
|
||||
delay(100);
|
||||
FastLED.setBrightness(newBrightness);
|
||||
FastLED.show();
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTT_publish("hanglamp/brightness", String(newBrightness));
|
||||
#endif
|
||||
}
|
||||
|
||||
void controlsCallback(ControlEvent event){
|
||||
@ -265,10 +268,6 @@ void setup() {
|
||||
|
||||
void loop() {
|
||||
|
||||
#ifdef ENABLE_MQTT
|
||||
MQTT_loop();
|
||||
#endif
|
||||
|
||||
#ifdef ENABLE_OTA
|
||||
OTA_loop();
|
||||
#endif
|
||||
@ -302,10 +301,7 @@ void loop() {
|
||||
#endif
|
||||
|
||||
delay(1000);
|
||||
|
||||
// Led_loop();
|
||||
|
||||
|
||||
|
||||
String command;
|
||||
|
||||
while(Serial.available()) {
|
||||
|
||||
70
src/MQTT.cpp
70
src/MQTT.cpp
@ -1,13 +1,9 @@
|
||||
#ifdef ENABLE_MQTT
|
||||
|
||||
#include "MQTT.h"
|
||||
|
||||
#include <PubSubClient.h>
|
||||
|
||||
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
|
||||
WiFiClient espClient;
|
||||
PubSubClient client(espClient);
|
||||
|
||||
@ -17,12 +13,23 @@ std::vector<SubscribedChannel> * channels = new std::vector<SubscribedChannel>()
|
||||
char mqtt_server[40] = "manus";
|
||||
char mqtt_port[6] = "1883";
|
||||
|
||||
|
||||
uint32_t chipId = 0;
|
||||
|
||||
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
|
||||
String clientId = "Konijntje-";
|
||||
clientId += String(ESP.getChipId());
|
||||
String clientId = "Hanglamp-";
|
||||
clientId += String(chipId, 16);
|
||||
Serial.printf("MQTT: Connecting as %s\n", clientId.c_str());
|
||||
|
||||
// Attempt to connect
|
||||
if (client.connect(clientId.c_str())) {
|
||||
Serial.println("connected");
|
||||
@ -81,7 +88,11 @@ void MQTT_callback(char* topic, uint8_t * payload, unsigned int length){
|
||||
|
||||
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.setCallback(MQTT_callback);
|
||||
@ -91,29 +102,32 @@ void MQTT_setup(){
|
||||
// sprintf(buff, "%08x", _pCallback);
|
||||
// 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
|
||||
Serial.println("MQTT_loop()");
|
||||
#endif
|
||||
int reconnectCounter = 0;
|
||||
while(!client.connected() && reconnectCounter < 3) {
|
||||
Serial.println("MQTT: connecting...");
|
||||
reconnectCounter++;
|
||||
connect();
|
||||
while(true){
|
||||
#if DEBUG_GENERAL
|
||||
Serial.println("MQTT_loop()");
|
||||
#endif
|
||||
int reconnectCounter = 0;
|
||||
while(!client.connected() && reconnectCounter < 3) {
|
||||
Serial.println("MQTT: connecting...");
|
||||
reconnectCounter++;
|
||||
connect();
|
||||
|
||||
if(client.connected()){
|
||||
Serial.println("MQTT: succes!");
|
||||
break;
|
||||
}else{
|
||||
Serial.println("MQTT: connection failed, try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
delay(5000);
|
||||
if(client.connected()){
|
||||
Serial.println("MQTT: succes!");
|
||||
break;
|
||||
}else{
|
||||
Serial.println("MQTT: connection failed, try again in 5 seconds");
|
||||
// Wait 5 seconds before retrying
|
||||
vTaskDelay(5000/portTICK_PERIOD_MS);
|
||||
}
|
||||
}
|
||||
client.loop();
|
||||
vTaskDelay(100/portTICK_PERIOD_MS);
|
||||
}
|
||||
client.loop();
|
||||
}
|
||||
#endif
|
||||
@ -3,6 +3,8 @@
|
||||
|
||||
#include "Arduino.h"
|
||||
|
||||
#include <WiFi.h>
|
||||
|
||||
using namespace std;
|
||||
|
||||
|
||||
@ -17,5 +19,5 @@ struct SubscribedChannel {
|
||||
|
||||
void MQTT_publish(const char * topic, String str);
|
||||
void MQTT_publish(const char * topic, const char * msg);
|
||||
void MQTT_loop();
|
||||
void MQTT_loop(void* params);
|
||||
void MQTT_setup();
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user