Initial commit, getting all the stuff from PlatformIO

This commit is contained in:
2025-11-02 17:55:41 +00:00
commit 4b4b816a8c
3003 changed files with 1213319 additions and 0 deletions

View File

@@ -0,0 +1,121 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Client mqtt("broker.hivemq.com");
// The example will also work with a broker -- just replace the line above with:
// PicoMQTT::Server mqtt;
void setup() {
// 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.println("WiFi connected.");
mqtt.subscribe("picomqtt/string_payload", [](const char * payload) {
// Use this type of handler to work with messages which should be strings. PicoMQTT will always add an extra
// zero terminator at the end of the payload, so it's safe to treat it as a string.
// Note however, that in the general case, payload can be binary (so it can contain any data, including zero
// bytes).
Serial.printf("Received message in topic 'picomqtt/string_payload': %s\n", payload);
});
mqtt.subscribe("picomqtt/binary_payload", [](const void * payload, size_t payload_size) {
// Here the payload is in a void * buffer and payload_size tells us its size.
Serial.printf("Received message in topic 'picomqtt/binary_payload', size: %u\n", payload_size);
});
mqtt.subscribe("picomqtt/string_payload/+/wildcard", [](const char * topic, const char * payload) {
// Use this type of callback to capture both the topic and the payload as string
// If the topic contains wildcards, their values can be extracted easily too
String wildcard_value = mqtt.get_topic_element(topic, 2);
Serial.printf("Received message in topic '%s' (wildcard = '%s'): %s\n", topic, wildcard_value.c_str(), payload);
});
mqtt.subscribe("picomqtt/binary_payload/+/wildcard", [](const char * topic, const void * payload, size_t payload_size) {
// Here the payload is in a void * buffer and payload_size tells us its size.
// If the topic contains wildcards, their values can be extracted easily too
String wildcard_value = mqtt.get_topic_element(topic, 2);
Serial.printf("Received message in topic '%s' (wildcard = '%s'), size: %u\n",
topic, wildcard_value.c_str(), payload_size);
});
mqtt.subscribe("picomqtt/big_message", [](const char * topic, PicoMQTT::IncomingPacket & packet) {
// This is the most advanced handler type. The packet parameter is a subclass of Stream and can be read from
// in small chunks using Stream's typical API. This is useful for handling messages too big to fit in RAM.
// Retrieve payload size
size_t payload_size = packet.get_remaining_size();
size_t digit_count = 0;
while (payload_size--) {
int val = packet.read();
if ((val >= '0') && (val <= '9')) {
// the byte is a digit!
++digit_count;
}
}
// Note that it's OK to not read the entire content of the message. Reading beyond the end of the packet
// is an error.
Serial.printf("Received message in topic '%s', it contained %u digits\n", topic, digit_count);
});
// Arduino Strings can be used instead of const char *. Note that this can be inefficient, especially with big
// payloads.
mqtt.subscribe("picomqtt/arduino_string/payload", [](const String & payload) {
Serial.printf("Received message in topic 'picomqtt/arduino_string/payload': %s\n", payload.c_str());
});
mqtt.subscribe("picomqtt/arduino_string/+/topic_and_payload", [](const String & topic, const String & payload) {
// If the topic contains wildcards, their values can be extracted easily too
String wildcard_value = mqtt.get_topic_element(topic, 2);
Serial.printf("Received message in topic '%s' (wildcard = '%s'): %s\n", topic.c_str(), wildcard_value.c_str(), payload.c_str());
});
// Different types of strings can be mixed too
mqtt.subscribe("picomqtt/arduino_string/mix/1", [](const String & topic, const char * payload) {
Serial.printf("Received message in topic '%s': %s\n", topic.c_str(), payload);
});
mqtt.subscribe("picomqtt/arduino_string/mix/2", [](const char * topic, const String & payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload.c_str());
});
// const and reference can be skipped too
mqtt.subscribe("picomqtt/arduino_string/mix/3", [](char * topic, String payload) {
Serial.printf("Received message in topic '%s': %s\n", topic, payload.c_str());
});
mqtt.begin();
}
void loop() {
mqtt.loop();
}

View File

@@ -0,0 +1,73 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Client mqtt("broker.hivemq.com");
// The example will also work with a broker -- just replace the line above with:
// PicoMQTT::Server mqtt;
unsigned long last_publish_time = 0;
static const char flash_string[] PROGMEM = "This is a string stored in flash.";
void setup() {
// 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.println("WiFi connected.");
mqtt.begin();
}
void loop() {
mqtt.loop();
// Publish a greeting message every 10 seconds.
if (millis() - last_publish_time >= 10000) {
// We're publishing to a topic, which we're subscribed too.
// The broker should deliver the messages back to us.
// publish a literal flash string
mqtt.publish("picomqtt/flash_string/literal", F("Literal PGM string"));
// publish a string from a PGM global
mqtt.publish_P("picomqtt/flash_string/global", flash_string);
// The topic can be an F-string too:
mqtt.publish_P(F("picomqtt/flash_string/global"), flash_string);
// publish binary data
const char binary_payload[] = "This string could contain binary data including a zero byte";
size_t binary_payload_size = strlen(binary_payload);
mqtt.publish("picomqtt/binary_payload", (const void *) binary_payload, binary_payload_size);
// Publish a big message in small chunks
auto publish = mqtt.begin_publish("picomqtt/chunks", 1000);
// Here we're writing 10 bytes 100 times
for (int i = 0; i < 1000; i += 10) {
publish.write((const uint8_t *) "1234567890", 10);
}
// In case of chunked published, an explicit call to send() is required.
publish.send();
last_publish_time = millis();
}
}

View File

@@ -0,0 +1,69 @@
// dependencies: bblanchon/ArduinoJson
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
#include <ArduinoJson.h>
PicoMQTT::Client mqtt("broker.hivemq.com");
unsigned long last_publish_time = 0;
int greeting_number = 1;
void setup() {
// 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.println("WiFi connected.");
// Subscribe to a topic and attach a callback
mqtt.subscribe("picomqtt/json/#", [](const char * topic, Stream & stream) {
Serial.printf("Received message in topic '%s':\n", topic);
JsonDocument json;
if (deserializeJson(json, stream)) {
Serial.println("Json parsing failed.");
return;
}
serializeJsonPretty(json, Serial);
Serial.println();
});
mqtt.begin();
}
void loop() {
mqtt.loop();
// Publish a greeting message every 3 seconds.
if (millis() - last_publish_time >= 3000) {
String topic = "picomqtt/json/esp-" + WiFi.macAddress();
Serial.printf("Publishing in topic '%s'...\n", topic.c_str());
// build JSON document
JsonDocument json;
json["foo"] = "bar";
json["millis"] = millis();
// publish using begin_publish()/send() API
auto publish = mqtt.begin_publish(topic, measureJson(json));
serializeJson(json, publish);
publish.send();
last_publish_time = millis();
}
}

View File

@@ -0,0 +1,53 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Client mqtt("broker.hivemq.com");
unsigned long last_publish_time = 0;
int greeting_number = 1;
void setup() {
// 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.println("WiFi connected.");
// Subscribe to a topic and attach a callback
mqtt.subscribe("picomqtt/#", [](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();
}
void loop() {
mqtt.loop();
// Publish a greeting message every 3 seconds.
if (millis() - last_publish_time >= 3000) {
// We're publishing to a topic, which we're subscribed too.
// The broker should deliver the messages back to us.
String topic = "picomqtt/esp-" + WiFi.macAddress();
String message = "Hello #" + String(greeting_number++);
Serial.printf("Publishing message in topic '%s': %s\n", topic.c_str(), message.c_str());
mqtt.publish(topic, message);
last_publish_time = millis();
}
}

View File

@@ -0,0 +1,33 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Server mqtt;
void setup() {
// 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.begin();
}
void loop() {
mqtt.loop();
}

View File

@@ -0,0 +1,57 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Client mqtt(
"broker.hivemq.com", // broker address (or IP)
1883, // broker port (defaults to 1883)
"esp-client", // Client ID
"username", // MQTT username
"password" // MQTT password
);
// PicoMQTT::Client mqtt; // This will work too, but configuration will have to be set later (e.g. in setup())
void setup() {
// 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.println("WiFi connected.");
// MQTT settings can be changed or set here instead
mqtt.host = "broker.hivemq.com";
mqtt.port = 1883;
mqtt.client_id = "esp-" + WiFi.macAddress();
mqtt.username = "username";
mqtt.password = "secret";
// Subscribe to a topic and attach a callback
mqtt.subscribe("picomqtt/#", [](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();
}
void loop() {
mqtt.loop();
// Changing host, port, client_id, username or password here is OK too. Changes will take effect after next
// reconnect. To force reconnection, explicitly disconnect the client by calling mqtt.disconnect().
}

View File

@@ -0,0 +1,44 @@
// dependencies: mlesniew/PicoWebsocket
#include <PicoMQTT.h>
#include <PicoWebsocket.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
// regular tcp server on port 1883
::WiFiServer tcp_server(1883);
// websocket server on tcp port 80
::WiFiServer websocket_underlying_server(80);
PicoWebsocket::Server<::WiFiServer> websocket_server(websocket_underlying_server);
// MQTT server using the two server instances
PicoMQTT::Server mqtt(tcp_server, websocket_server); // NOTE: this constructor can take any number of server parameters
void setup() {
// 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.begin();
}
void loop() {
mqtt.loop();
}

View File

@@ -0,0 +1,54 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Client mqtt("broker.hivemq.com");
unsigned long last_subscribe_time = 0;
int resubscribe = 1;
void handler_foo(const char * topic, const char * payload) {
Serial.printf("Handler foo received message in topic '%s': %s\n", topic, payload);
}
void handler_bar(const char * topic, const char * payload) {
Serial.printf("Handler bar received message in topic '%s': %s\n", topic, payload);
}
void setup() {
// 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.println("WiFi connected.");
mqtt.begin();
}
void loop() {
mqtt.loop();
// Resubscribe every 5 seconds
if (millis() - last_subscribe_time >= 5000) {
if (++resubscribe % 2 == 0) {
mqtt.subscribe("picomqtt/#", handler_foo);
} else {
mqtt.subscribe("picomqtt/#", handler_bar);
}
last_subscribe_time = millis();
}
}

View File

@@ -0,0 +1,57 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
class MQTT: public PicoMQTT::Server {
protected:
PicoMQTT::ConnectReturnCode auth(const char * client_id, const char * username, const char * password) override {
// only accept client IDs which are 3 chars or longer
if (String(client_id).length() < 3) { // client_id is never NULL
return PicoMQTT::CRC_IDENTIFIER_REJECTED;
}
// only accept connections if username and password are provided
if (!username || !password) { // username and password can be NULL
// no username or password supplied
return PicoMQTT::CRC_NOT_AUTHORIZED;
}
// accept two user/password combinations
if (
((String(username) == "alice") && (String(password) == "secret"))
|| ((String(username) == "bob") && (String(password) == "password"))) {
return PicoMQTT::CRC_ACCEPTED;
}
// reject all other credentials
return PicoMQTT::CRC_BAD_USERNAME_OR_PASSWORD;
}
} mqtt;
void setup() {
// 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.begin();
}
void loop() {
mqtt.loop();
}

View File

@@ -0,0 +1,33 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Server mqtt(9000); // listening port: TCP 9000
void setup() {
// 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.begin();
}
void loop() {
mqtt.loop();
}

View File

@@ -0,0 +1,52 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::Server mqtt;
unsigned long last_publish_time = 0;
int greeting_number = 1;
void setup() {
// 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());
// Subscribe to a topic and attach a callback
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();
}
void loop() {
mqtt.loop();
// Publish a greeting message every 3 seconds.
if (millis() - last_publish_time >= 3000) {
// We're publishing to a topic, which we're subscribed too, but these message will *not* be delivered locally.
String topic = "picomqtt/esp-" + WiFi.macAddress();
String message = "Hello #" + String(greeting_number++);
Serial.printf("Publishing message in topic '%s': %s\n", topic.c_str(), message.c_str());
mqtt.publish(topic, message);
last_publish_time = millis();
}
}

View File

@@ -0,0 +1,57 @@
#include <PicoMQTT.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
PicoMQTT::ServerLocalSubscribe mqtt;
unsigned long last_publish;
static const char flash_string[] PROGMEM = "Hello from the broker's flash.";
void setup() {
// 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.begin();
mqtt.subscribe("picomqtt/foo", [](const char * payload) {
Serial.printf("Message received: %s\n", payload);
});
}
void loop() {
mqtt.loop();
if (millis() - last_publish >= 5000) {
// this will publish the message to subscribed clients and fire the local callback
mqtt.publish("picomqtt/foo", "hello from broker!");
// this will also work as usual and fire the callback
mqtt.publish_P("picomqtt/foo", flash_string);
// begin_publish will publish to clients, but it will NOT fire the local callback
auto publish = mqtt.begin_publish("picomqtt/foo", 33);
publish.write((const uint8_t *) "Message delivered to clients only", 33);
publish.send();
last_publish = millis();
}
}

View File

@@ -0,0 +1,48 @@
// Platform compatibility: espressif8266
#include <Arduino.h>
#include <Ethernet.h>
#include <PicoMQTT.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
PicoMQTT::Client mqtt(client, "broker.hivemq.com");
unsigned long last_publish_time = 0;
int greeting_number = 1;
void setup() {
Serial.begin(115200);
Serial.println("Connecting to network...");
Ethernet.init(5); // ss pin
while (!Ethernet.begin(mac)) {
Serial.println("Failed, retrying...");
}
Serial.println(Ethernet.localIP());
// Subscribe to a topic and attach a callback
mqtt.subscribe("picomqtt/#", [](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();
}
void loop() {
mqtt.loop();
// Publish a greeting message every 3 seconds.
if (millis() - last_publish_time >= 3000) {
// We're publishing to a topic, which we're subscribed too.
// The broker should deliver the messages back to us.
String topic = "picomqtt/esp-" + WiFi.macAddress();
String message = "Hello #" + String(greeting_number++);
Serial.printf("Publishing message in topic '%s': %s\n", topic.c_str(), message.c_str());
mqtt.publish(topic, message);
last_publish_time = millis();
}
}

View File

@@ -0,0 +1,28 @@
// Platform compatibility: espressif8266
#include <Arduino.h>
#include <Ethernet.h>
#include <PicoMQTT.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetServer server(1883);
PicoMQTT::Server mqtt(server);
void setup() {
Serial.begin(115200);
Serial.println("Connecting to network...");
Ethernet.init(5); // ss pin
while (!Ethernet.begin(mac)) {
Serial.println("Failed, retrying...");
}
Serial.println(Ethernet.localIP());
mqtt.begin();
}
void loop() {
mqtt.loop();
}

View File

@@ -0,0 +1,38 @@
// dependencies: mlesniew/PicoWebsocket@1.2.0
#include <PicoMQTT.h>
#include <PicoWebsocket.h>
#if __has_include("config.h")
#include "config.h"
#endif
#ifndef WIFI_SSID
#define WIFI_SSID "WiFi SSID"
#endif
#ifndef WIFI_PASSWORD
#define WIFI_PASSWORD "password"
#endif
WiFiServer server(80);
PicoWebsocket::Server<::WiFiServer> websocket_server(server);
PicoMQTT::Server mqtt(websocket_server);
void setup() {
// 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.begin();
}
void loop() {
mqtt.loop();
}