Subversion Repositories ESP8266_P1_Meter

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
2 raymond 1
#include "credentials.h"
2
#include <MQTTRemote.h>
3
#include <driver/gpio.h>
4
#include <esp_log.h>
5
#include <freertos/FreeRTOS.h>
6
#include <freertos/task.h>
7
 
8
#define TAG "example"
9
 
10
#define PIN_LED GPIO_NUM_14
11
 
12
MQTTRemote _mqtt_remote(mqtt_client_id, mqtt_host, 1883, mqtt_username, mqtt_password,
13
                        {.rx_buffer_size = 2048, .tx_buffer_size = 2048, .keep_alive_s = 10});
14
 
15
void blinkAndSerialTask(void *pvParameters) {
16
  bool swap = false;
17
  while (1) {
18
    gpio_set_level(PIN_LED, swap);
19
    swap = !swap;
20
    ESP_LOGI(TAG, "Hello");
21
    vTaskDelay(1000 / portTICK_PERIOD_MS);
22
  }
23
}
24
 
25
void mqttMessageTask(void *pvParameters) {
26
  bool retain = false;
27
  uint8_t qos = 0;
28
  while (1) {
29
    _mqtt_remote.publishMessageVerbose(_mqtt_remote.clientId() + "/hello", "world", retain, qos);
30
    vTaskDelay(10000 / portTICK_PERIOD_MS);
31
  }
32
}
33
 
34
extern "C" {
35
void app_main();
36
}
37
 
38
void app_main(void) {
39
  // Setup led and blinking led task
40
  gpio_set_direction(PIN_LED, GPIO_MODE_OUTPUT);
41
  gpio_set_level(PIN_LED, 1);
42
  xTaskCreate(blinkAndSerialTask, "blinkAndSerialTask", 2048, NULL, 15, NULL);
43
 
44
  // TODO (you): You need to connect to WiFi here first.
45
  // For a simple one line utility, see https://github.com/johboh/ConnectionHelper
46
  // Once connected to wifi, continue with below.
47
 
48
  // Connect to WIFI
49
  auto connected = true; // TODO (you): You need to connect to WiFi here first.
50
  if (connected) {
51
    // Connected to WIFI.
52
 
53
    // Subscribe to to the /set topic under our client ID.
54
    _mqtt_remote.subscribe(_mqtt_remote.clientId() + "/set", [](const std::string &topic, const std::string &message) {
55
      ESP_LOGI(TAG, "Topic: %s, Message: %s", topic.c_str(), message.c_str());
56
    });
57
 
58
    // Start MQTT
59
    _mqtt_remote.start([](bool connected) {
60
      _mqtt_remote.publishMessageVerbose(_mqtt_remote.clientId() + "/initial_message", "oh hello!");
61
    });
62
 
63
    // Start task for periodically publishing messages.
64
    xTaskCreate(mqttMessageTask, "mqttMessageTask", 2048, NULL, 15, NULL);
65
 
66
  } else {
67
    ESP_LOGE(TAG, "Failed to connect");
68
  }
69
 
70
  // Run forever.
71
  while (1) {
72
    vTaskDelay(500 / portTICK_PERIOD_MS);
73
    fflush(stdout);
74
  }
75
}