-
Notifications
You must be signed in to change notification settings - Fork 0
/
basic_server.cpp
64 lines (52 loc) · 1.59 KB
/
basic_server.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#include <ProxyWebSocketsServer.h>
TaskHandle_t wsProxyTask_handle;
PicoMQTT::Server mqtt(1883);
#define INTERVAL 3000
#define PROXY_DEST "127.0.0.1"
ProxyWebSocketsServer *webSocket;
void webserver_setup(void);
void webserver_loop(void);
void wsProxyTask(void* pvParameters) {
log_i("wsProxyTask");
webSocket = new ProxyWebSocketsServer(8883, PROXY_DEST, 1883, 2000);
for (;;) {
webSocket->loop();
yield();
delay(1);
}
}
void setup() {
#ifdef ARDUINO_USB_CDC_ON_BOOT
delay(3000);
#endif
Serial.begin(115200);
webserver_setup();
mqtt.subscribe("#", [](const char * topic, const char * payload) {
log_i("Received message in topic '%s': %s", topic, payload);
});
mqtt.begin();
xTaskCreate(wsProxyTask,"wsproxy", 16384, nullptr, 1, &wsProxyTask_handle);
Serial.printf("sizeof WiFiClient = %u sizeof WSclient_t %u\n", sizeof(WiFiClient), sizeof(WSclient_t));
}
uint32_t last;
void loop() {
mqtt.loop();
webserver_loop();
// periodically publish CPU temperature
if (millis() - last > INTERVAL) {
float t = temperatureRead();
String topic = "picomqtt/esp-cpu-temperature";
String message = String(t);
// log_d("Publishing message in topic '%s': %s", topic.c_str(), message.c_str());
mqtt.publish(topic, message);
mqtt.loop();
topic = "esp32/free-heap";
message = String(esp_get_free_heap_size());
mqtt.publish(topic, message);
last = millis();
}
}