能源管控程序
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
pry c4b6029f3d 添加ESPMQTT库 пре 2 година
..
examples 添加ESPMQTT库 пре 2 година
src 添加ESPMQTT库 пре 2 година
.piopm 添加ESPMQTT库 пре 2 година
README.md 添加ESPMQTT库 пре 2 година
library.json 添加ESPMQTT库 пре 2 година

README.md

ESP32 > MQTT

ESP32 foundation library for MQTT based HomeIOT

#include <Arduino.h>
using std::string;

#include <freertos/queue.h>
QueueHandle_t mqttQueue;
typedef struct {
  string name;
  string metric;
} mqttMessage;

// EspMqtt.
#include "EspMQTT.h"
EspMQTT mqtt;
void mqtt_callback(std::string param, std::string value);

void mqttSetup() {
  uint16_t debugLevel = 0;
  if (debugLevel) {
    mqtt.debugLevel = debugLevel;
    mqtt.setAvailabilityInterval(5);
  }
  // MqTT:
  mqtt.setWiFi("wifi-name", "wifi-pass", "hostname");
  mqtt.setMqtt("mqtt-server", "mqtt-user", "mqtt-pass");
  mqtt.setCommonTopics("my/root/topic/dir", "ds18b20");
  mqtt.setCallback(mqtt_callback);
  mqtt.start(true);
}


// Main.cpp Setup.
void setup() {
  mqttSetup();
  mqttQueue = xQueueCreate(10, sizeof(mqttMessage));
}

// Main.cpp Loop.
float counter = 1.1;
void loop() {
  if (xQueueReceive(mqttQueue, &message, 100 / portTICK_PERIOD_MS) == pdFALSE) {
    mqtt.publishMetric(message.name, message.metric);
    // printf("mqtt [%s] push = %s\n", msg.name.c_str(), msg.metric.c_str());
  }
  else {
    counter += 0.1;
    char data[10];
    sprintf(data, "%.2f", counter);
    string metric = std::string(data);
    mqttMessage msg = {"temperature", metric};
    xQueueSend(mqttQueue, &msg, portMAX_DELAY);
  }
  vTaskDelay(5000 / portTICK_PERIOD_MS);
}


void mqtt_callback(std::string param, std::string value) {
  uint16_t val = atoi(value.c_str());
  printf("%s=%s\n", param.c_str(), message.c_str());
}

Ref: