Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: mlesniew/PicoMQTT
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: master
Choose a base ref
...
head repository: mhaberler/PicoMQTT
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: master
Choose a head ref
Can’t automatically merge. Don’t worry, you can still create the pull request.
  • 2 commits
  • 5 files changed
  • 1 contributor

Commits on Mar 22, 2024

  1. WORKS - loop() needed

    Michael Haberler committed Mar 22, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    57dd14d View commit details
  2. cleanup

    Michael Haberler committed Mar 22, 2024
    Copy the full SHA
    c4df2fd View commit details
Showing with 207 additions and 3 deletions.
  1. +1 −0 .gitignore
  2. +31 −3 platformio.ini
  3. +73 −0 src/ProxyWebSocketsServer.cpp
  4. +42 −0 src/ProxyWebSocketsServer.h
  5. +60 −0 src/basic_server.cpp
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -2,3 +2,4 @@
*.orig
*.tar.gz
*.sh
.vscode
34 changes: 31 additions & 3 deletions platformio.ini
Original file line number Diff line number Diff line change
@@ -1,7 +1,35 @@
[env:wemos]
platform = espressif8266
board = d1_mini
; [env:wemos]
; platform = espressif8266
; board = d1_mini
; framework = arduino
; monitor_speed = 115200
; upload_speed = 921600

[env]
platform = espressif32 @ ^6.5.0
framework = arduino
monitor_filters = esp32_exception_decoder
monitor_speed = 115200
upload_speed = 921600
upload_protocol = esptool
debug_speed = 5000
build_type = debug

[env:broker_s3]
board = m5stack-stamps3
debug_tool = esp-builtin
build_flags =
; -DPICOMQTT_DEBUG_TRACE_FUNCTIONS
-DARDUINO_USB_CDC_ON_BOOT=1
; -DCORE_DEBUG_LEVEL=ARDUHAL_LOG_LEVEL_INFO
-DWIFI_SSID=\"${sysenv.WIFI_SSID}\"
-DWIFI_PASSWORD=\"${sysenv.WIFI_PASSWORD}\"
; -DDEBUG_ESP_PORT=Serial
-O0 -ggdb -g
lib_deps =
https://github.com/mhaberler/arduinoWebSockets.git#callback-instance

build_src_filter =
+<PicoMQTT/**.*>
+<**.*>
+<../examples/basic_server/**.*>
73 changes: 73 additions & 0 deletions src/ProxyWebSocketsServer.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
#include <ProxyWebSocketsServer.h>

void ProxyWebSocketsServer::_loop(void) {
for (int i = 0; i < WEBSOCKETS_SERVER_CLIENT_MAX; i++) {
WiFiClient *d = _dest[i];

if (d && d->connected()) {
while (d->available()) {
uint8_t buffer[TOWS_SIZE];
int res = d->read(buffer, sizeof(buffer));
if ((res > 0) && (_clients[i].status == WSC_CONNECTED)) {
this->sendBIN(i, buffer, res);
}
}
}
}
}


void _proxyWebSocketEvent(WebSocketsServerCore *server, uint8_t num, WStype_t type, uint8_t * payload, size_t length) {
ProxyWebSocketsServer *pws = (ProxyWebSocketsServer*)server;
WSclient_t *wsc = &pws->_clients[num];
WiFiClient *wc = pws->_dest[num];

switch(type) {
case WStype_DISCONNECTED:
// Serial.printf("[%u] Disconnected\n", num);
if (wc && wc->connected()) {
wc->stop();
delete wc;
pws->_dest[num] = nullptr;
}
break;
case WStype_CONNECTED: {

IPAddress ip = wsc->tcp->remoteIP() ;
uint16_t port = wsc->tcp->remotePort() ;
// Serial.printf("[%u] Connected from %d.%d.%d.%d port %u url: %s len=%u\n", num, ip[0], ip[1], ip[2], ip[3], port, payload, length);

pws->_dest[num] = new WiFiClient;
WiFiClient *cp = pws->_dest[num];

if (!cp->connect(pws->_destHost.c_str(), pws->_destPort, pws->_timeout_ms)) {
delete pws->_dest[num] ;
pws->_dest[num] = nullptr;
// Serial.printf("[%u] proxy connect failed\n", num);
break;
}
// Serial.printf("[%u] proxy connect success connected=%d wc=%p\n", num, cp->connected(), cp);
pws->loop(); // handle new client
}
break;
case WStype_TEXT:
// Serial.printf("[%u] get Text: %s\n", num, payload);
if (wc && wc->connected()) {
wc->write(payload, length);
}
break;
case WStype_BIN:
// Serial.printf("[%u] get binary length: %u wc=%p wc->connected=%d payload=%s\n", num, length, wc, wc && wc->connected(), payload);
if (wc && wc->connected()) {
wc->write(payload, length);
}
break;
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
Serial.printf("wsmessage %u\n", type);
break;
}
}
42 changes: 42 additions & 0 deletions src/ProxyWebSocketsServer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
#include "WebSocketsServer.h"
#include <WiFi.h>

#define TOWS_SIZE 2048


void _proxyWebSocketEvent(WebSocketsServerCore *server, uint8_t num, WStype_t type, uint8_t * payload, size_t length);
bool validateHttpHeader(String headerName, String headerValue);

class ProxyWebSocketsServer : public WebSocketsServer {

friend void _proxyWebSocketEvent(WebSocketsServerCore *server, uint8_t num, WStype_t type, uint8_t * payload, size_t length);

public:

ProxyWebSocketsServer(uint16_t port,
const String &destHost,
uint16_t destPort,
int32_t timeout_ms = 500,
const String & origin = "",
const String & protocol = "MQTT") :
_destHost(destHost),
_destPort(destPort),
_timeout_ms(timeout_ms),
WebSocketsServer(port, origin, protocol) {
begin();
onEvent(_proxyWebSocketEvent);
}

void loop(void) {
_loop();
WebSocketsServer::loop();
}

private:
void _loop(void);
String _destHost;
uint16_t _destPort;
int32_t _timeout_ms;
WiFiClient *_dest[WEBSOCKETS_SERVER_CLIENT_MAX] = {nullptr};
};

60 changes: 60 additions & 0 deletions src/basic_server.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
#include <PicoMQTT.h>

#if __has_include("config.h")
#include "config.h"
#endif

#include <ProxyWebSocketsServer.h>

#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif

#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
#define INTERVAL 5000
PicoMQTT::Server mqtt(1883);
// PicoMQTT::Client client("127.0.0.1", 1883, "loccl");

#define PROXY_DEST "127.0.0.1"
ProxyWebSocketsServer *webSocket;

void setup() {
delay(3000);
// Setup serial
Serial.begin(115200);

// Connect to WiFi
Serial.printf("Connecting to WiFi %s\n", WIFI_SSID);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
}
Serial.printf("WiFi connected, IP: %s\n", WiFi.localIP().toString().c_str());


mqtt.subscribe("#", [](const char * topic, const char * payload) {
// payload might be binary, but PicoMQTT guarantees that it's zero-terminated
Serial.printf("Received message in topic '%s': %s\n", topic, payload);
});
mqtt.begin();

webSocket = new ProxyWebSocketsServer(8883, PROXY_DEST, 1883, 500);
// webSocket->begin();

}
uint32_t last;

void loop() {
mqtt.loop();
// client.loop();
if (webSocket)
webSocket->loop();

// if (millis() - last > INTERVAL) {
// last = millis();
// mqtt.publish("blah", "fasel");
// }
}