Compare commits

..

14 Commits
0.7.5 ... 0.7.7

Author SHA1 Message Date
hsaturn
9c7f3b6b8e Fix of decode length 2021-08-09 10:37:46 +02:00
hsaturn
fab242e212 Version 0.7.6 2021-08-09 03:04:21 +02:00
hsaturn
56a2be621f Fix message length error 2021-08-09 03:00:28 +02:00
hsaturn
c4cf39ab68 Fix aunit tests 2021-08-09 02:33:01 +02:00
hsaturn
90dea36ab0 Attempt to write more readable examples 2021-05-13 03:21:03 +02:00
hsaturn
25a721e06a Attempt to write more readable examples 2021-05-13 03:21:03 +02:00
hsaturn
b228d35ab0 Attempt to write more readable examples 2021-05-13 03:21:03 +02:00
hsaturn
c70716a595 Attempt to write more readable examples 2021-05-13 03:21:03 +02:00
hsaturn
8d5cad5fec Added overload of Client::publish, fix compilation 2021-05-13 03:21:03 +02:00
hsaturn
d5430228e5 Fix cr/lf bug
Added echo on|off for later vt100 terminal
Added rnd(min,max) function
Simplied every function
Better parsing for getword (accept strings)
Fix for blink command
2021-05-13 03:21:03 +02:00
hsaturn
91c1f96146 Update README.md
links updates
2021-05-08 15:14:59 +02:00
hsaturn
f04a2a07c0 Fix aunit.yml 2021-04-28 21:22:07 +00:00
hsaturn
38f306c170 Fix compilation esp8266 esp32 2021-04-28 23:16:33 +02:00
hsaturn
024e80c9dc Perpare for future ESP32 aunit tests 2021-04-28 21:00:31 +00:00
17 changed files with 269 additions and 98 deletions

View File

@@ -24,5 +24,11 @@ jobs:
git clone https://github.com/hsaturn/EspMock
- name: Verify tests
run: |
make -C tests
# Run tests for ESP8266
make -C tests ESP_LIBS="ESP8266WiFi ESPAsyncTCP" tests
make -C tests runtests
make -C tests clean
# Run tests for ESP32
#make -C tests ESP_LIBS="ESP8266WiFi ESPAsyncTCP" tests
#make -C tests runtests
#make -C tests clean

View File

@@ -1,12 +1,12 @@
# TinyMqtt
![Release](https://img.shields.io/github/v/release/hsaturn/TinyMqtt)
[![Release](https://img.shields.io/github/v/release/hsaturn/TinyMqtt)](https://github.com/hsaturn/TinyMqtt/releases)
[![AUnit Tests](https://github.com/hsaturn/TinyMqtt/actions/workflows/aunit.yml/badge.svg)](https://github.com/hsaturn/TinyMqtt/actions/workflows/aunit.yml)
![Issues](https://img.shields.io/github/issues/hsaturn/TinyMqtt)
![Esp8266](https://img.shields.io/badge/platform-ESP8266-green)
![Esp32](https://img.shields.io/badge/platform-ESP32-green)
![Gpl 3.0](https://img.shields.io/github/license/hsaturn/TinyMqtt)
![Mqtt 3.1.1](https://img.shields.io/badge/Mqtt-%203.1.1-yellow)
[![Issues](https://img.shields.io/github/issues/hsaturn/TinyMqtt)](https://github.com/hsaturn/TinyMqtt/issues)
[![Esp8266](https://img.shields.io/badge/platform-ESP8266-green)](https://www.espressif.com/en/products/socs/esp8266)
[![Esp32](https://img.shields.io/badge/platform-ESP32-green)](https://www.espressif.com/en/products/socs/esp32)
[![Gpl 3.0](https://img.shields.io/github/license/hsaturn/TinyMqtt)](https://www.gnu.org/licenses/gpl-3.0.fr.html)
[![Mqtt 3.1.1](https://img.shields.io/badge/Mqtt-%203.1.1-yellow)](https://docs.oasis-open.org/mqtt/mqtt/v3.1.1/errata01/os/mqtt-v3.1.1-errata01-os-complete.html#_Toc442180822)
TinyMqtt is a small, fast and capable Mqtt Broker and Client for Esp8266 / Esp32 / Esp WROOM

View File

@@ -31,7 +31,8 @@
*
*/
#include <my_credentials.h>
const char *ssid = "";
const char *password = "";
std::string topic="sensor/temperature";
@@ -40,11 +41,11 @@ MqttBroker broker(1883);
MqttClient mqtt_a(&broker);
MqttClient mqtt_b(&broker);
void onPublishA(const MqttClient* source, const Topic& topic, const char* payload, size_t length)
{ Serial << endl << "---------> A Received " << topic.c_str() << endl; }
void onPublishA(const MqttClient* /* source */, const Topic& topic, const char* payload, size_t /* length */)
{ Serial << "--> client A received " << topic.c_str() << ", " << payload << endl; }
void onPublishB(const MqttClient* source, const Topic& topic, const char* payload, size_t length)
{ Serial << endl << "---------> B Received " << topic.c_str() << endl; }
void onPublishB(const MqttClient* /* source */, const Topic& topic, const char* payload, size_t /* length */)
{ Serial << "--> client B Received " << topic.c_str() << ", " << payload << endl; }
void setup()
{
@@ -52,6 +53,9 @@ void setup()
delay(500);
Serial << "Clients with wifi " << endl;
if (strlen(ssid)==0)
Serial << "****** PLEASE EDIT THE EXAMPLE AND MODIFY ssid/password *************" << endl;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
@@ -76,14 +80,14 @@ void loop()
mqtt_b.loop();
// ============= client A publish ================
static const int intervalA = 5000; // publishes every 5s
static const int intervalA = 5000; // publishes every 5s (please avoid usage of delay())
static uint32_t timerA = millis() + intervalA;
if (millis() > timerA)
{
Serial << "A is publishing " << topic.c_str() << endl;
timerA += intervalA;
mqtt_a.publish(topic);
mqtt_a.publish(topic, " sent by A");
}
// ============= client B publish ================
@@ -92,8 +96,9 @@ void loop()
if (millis() > timerB)
{
static int temperature;
Serial << "B is publishing " << topic.c_str() << endl;
timerB += intervalB;
mqtt_b.publish(topic, std::string(String(15+millis()%10).c_str()));
mqtt_b.publish(topic, " sent by B: "+std::string(String(16+temperature++%6).c_str()));
}
}

View File

@@ -32,11 +32,11 @@ MqttBroker broker(1883);
MqttClient mqtt_a(&broker);
MqttClient mqtt_b(&broker);
void onPublishA(const MqttClient* srce, const Topic& topic, const char* payload, size_t length)
{ Serial << "--> A Received " << topic.c_str() << endl; }
void onPublishA(const MqttClient* /* srce */, const Topic& topic, const char* payload, size_t /* length */)
{ Serial << "--> Client A received msg on topic " << topic.c_str() << ", " << payload << endl; }
void onPublishB(const MqttClient* srce, const Topic& topic, const char* payload, size_t length)
{ Serial << "--> B Received " << topic.c_str() << endl; }
void onPublishB(const MqttClient* /* srce */, const Topic& topic, const char* payload, size_t /* length */)
{ Serial << "--> Client B received msg on topic " << topic.c_str() << ", " << payload << endl; }
void setup()
{
@@ -53,7 +53,7 @@ void setup()
void loop()
{
broker.loop();
broker.loop(); // Don't forget to call loop() for all brokers and clients
mqtt_a.loop();
mqtt_b.loop();
@@ -65,7 +65,7 @@ void loop()
{
Serial << "A is publishing " << topic.c_str() << endl;
timerA += intervalA;
mqtt_a.publish(topic);
mqtt_a.publish(topic, "sent by A");
}
// ============= client B publish ================
@@ -76,6 +76,6 @@ void loop()
{
Serial << "B is publishing " << topic.c_str() << endl;
timerB += intervalB;
mqtt_b.publish(topic);
mqtt_b.publish(topic, "sent by B");
}
}

View File

@@ -1,7 +1,5 @@
#include "TinyMqtt.h" // https://github.com/hsaturn/TinyMqtt
#include <my_credentials.h>
#define PORT 1883
MqttBroker broker(PORT);
@@ -14,11 +12,22 @@ MqttBroker broker(PORT);
* | +--------+ |
* | |
* +-----------------------------+
*
* Your ESP will become a MqttBroker.
* You can test it with any client such as mqtt-spy for example
*
*/
const char* ssid = "";
const char* password = "";
void setup()
{
Serial.begin(115200);
if (strlen(ssid)==0)
Serial << "****** PLEASE MODIFY ssid/password *************" << endl;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);

View File

@@ -16,8 +16,10 @@
* | |
* +-----------------+
*
* 1 - edit my_credentials.h to setup wifi essid/password
* 1 - change the ssid/password
* 2 - change BROKER values (or keep emqx.io test broker)
* 3 - you can use mqtt-spy to connect to the same broker and
* see the sensor/temperature updated by the client.
*
* pro - small memory footprint (both ram and flash)
* - very simple to setup and use
@@ -30,7 +32,8 @@
const char* BROKER = "broker.emqx.io";
const uint16_t BROKER_PORT = 1883;
#include <my_credentials.h>
const char* ssid = "";
const char* password = "";
static float temp=19;
static MqttClient client;
@@ -39,7 +42,10 @@ void setup()
{
Serial.begin(115200);
delay(500);
Serial << "Simple clients with wifi " << endl;
if (strlen(ssid)==0)
Serial << "****** PLEASE MODIFY ssid/password *************" << endl;
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
@@ -54,15 +60,28 @@ void setup()
void loop()
{
client.loop();
client.loop(); // Don't forget to call loop() for each broker and client
delay(1000);
// delay(1000); please avoid usage of delay (see below how this done using next_send and millis())
static auto next_send = millis();
auto rnd=random(100);
if (millis() > next_send)
{
next_send += 1000;
if (rnd > 66) temp += 0.1;
else if (rnd < 33) temp -= 0.1;
if (not client.connected())
{
Serial << millis() << ": Not connected to broker" << endl;
return;
}
client.publish("sensor/temperature", String(temp));
auto rnd=random(100);
if (rnd > 66) temp += 0.1;
else if (rnd < 33) temp -= 0.1;
Serial << "--> Publishing a new sensor/temperature value: " << temp << endl;
client.publish("sensor/temperature", String(temp));
}
}

View File

@@ -3,6 +3,7 @@
#if defined(ESP8266)
#include <ESP8266mDNS.h>
#elif defined(ESP32)
#include <WiFi.h>
#include <ESPmDNS.h>
#else
#error Unsupported platform
@@ -11,8 +12,11 @@
#include <sstream>
#include <map>
bool echo_on = true;
/** Very complex example
* Console allowing to make any kind of test.
* Console allowing to make any kind of test,
* even some stress tests.
*
* Upload the sketch, the use the terminal.
* Press H for mini help.
@@ -21,7 +25,8 @@
* TODO examples of scripts
*/
#include <my_credentials.h>
const char* ssid = "";
const char* password = "";
std::string topic="sensor/temperature";
@@ -46,17 +51,32 @@ void setup()
WiFi.persistent(false); // https://github.com/esp8266/Arduino/issues/1054
Serial.begin(115200);
delay(500);
Serial << endl << endl << endl
<< "Connecting to '" << ssid << "' ";
Serial << endl << endl;
Serial << "***************************************************************" << endl;
Serial << "* Welcome to the TinyMqtt console" << endl;
Serial << endl;
Serial << "* The console allows to test all features of the libraries." << endl;
Serial << endl;
if (strlen(ssid)==0)
Serial << "* WARNING: You may want to modify ssid/password in order" << endl
<< " to reflect your Wifi configuration." << endl;
Serial << endl;
Serial << "* Enter help to view the list of commands." << endl;
Serial << "***************************************************************" << endl;
Serial << endl;
Serial << "Connecting to '" << ssid << "' ";
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{ Serial << '-'; delay(500); }
{ Serial << '-'; delay(500); }
Serial << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
Serial << "Type help for more..." << endl;
Serial << endl << "Connected to " << ssid << "IP address: " << WiFi.localIP() << endl;
const char* name="tinytest";
Serial << "Starting MDNS, name= " << name;
@@ -71,27 +91,67 @@ void setup()
brokers["broker"] = broker;
}
std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' ');
int getint(std::string& str, const int if_empty=0)
{
std::string sword;
while(str.length() && str[0]>='0' && str[0]<='9')
std::string str2=str;
std::string sword = getword(str);
if (sword[0] and isdigit(sword[0]))
{
sword += str[0]; str.erase(0,1);
int ret=atoi(sword.c_str());
while(isdigit(sword[0]) or sword[0]==' ') sword.erase(0,1);
if (sword.length()) str = sword+' '+str;
return ret;
}
while(str[0]==' ') str.erase(0,1);
if (if_empty and sword.length()==0) return if_empty;
return atoi(sword.c_str());
str=str2;
return if_empty;
}
std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' ')
std::string getword(std::string& str, const char* if_empty/*=nullptr*/, char sep/*=' '*/)
{
char quote=(str[0]=='"' or str[0]=='\'' ? str[0] : 0);
if (quote) str.erase(0,1);
std::string sword;
while(str.length() && str[0]!=sep)
while(str.length() and (str[0]!=sep or quote))
{
sword += str[0]; str.erase(0,1);
if (str[0]==quote)
{
str.erase(0,1);
break;
}
sword += str[0];
str.erase(0,1);
}
while(str[0]==sep) str.erase(0,1);
if (if_empty and sword.length()==0) return if_empty;
if (quote==false and sword.length()>=4 and sword.substr(0,3)=="rnd")
{
sword.erase(0,3);
if (sword[0]=='(')
{
int to = 100;
sword.erase(0,1);
int from=getint(sword);
if (sword[0]==',')
{
sword.erase(0,1);
to = getint(sword);
if (sword[0]!=')') Serial << "Missing ')'" << endl;
}
else
{
to=from;
from=0;
}
return String(random(from,to)).c_str();
}
else
{
Serial << "Missing '('" << endl;
}
}
while(str[0]==' ') str.erase(0,1);
return sword;
}
@@ -140,7 +200,7 @@ std::set<std::string> commands = {
"auto", "broker", "blink", "client", "connect",
"create", "delete", "help", "interval",
"ls", "ip", "off", "on", "set",
"publish", "reset", "subscribe", "unsubscribe", "view", "every"
"publish", "reset", "subscribe", "unsubscribe", "view", "echo", "every"
};
void convertToCommand(std::string& search)
@@ -179,7 +239,7 @@ void replace(const char* d, std::string& str, std::string srch, std::string to)
{
str.erase(pos, srch.length());
str.insert(pos, to);
pos += to.length();
pos += to.length()-1;
}
}
}
@@ -197,6 +257,7 @@ void replaceVars(std::string& cmd)
}
cmd.erase(0, cmd.find_first_not_of(" "));
cmd.erase(cmd.find_last_not_of(" ")+1);
}
// publish at regular interval
@@ -463,9 +524,7 @@ void eval(std::string& cmd)
}
else if (compare(s,"publish"))
{
while (cmd[0]==' ') cmd.erase(0,1);
retval = client->publish(getword(cmd, topic.c_str()), cmd.c_str(), cmd.length());
cmd.clear(); // remove payload
retval = client->publish(getword(cmd, topic.c_str()), getword(cmd));
}
else if (compare(s,"subscribe"))
{
@@ -497,6 +556,22 @@ void eval(std::string& cmd)
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
else if (compare(s, "echo"))
{
s=getword(cmd);
if (s=="on")
echo_on = true;
else if (s=="off")
echo_on = false;
else
{
Serial << s << ' ';
while(cmd.length())
{
Serial << getword(cmd) << ' ';
}
}
}
else if (compare(s, "every"))
{
uint32_t ms = getint(cmd, 0);
@@ -517,36 +592,20 @@ void eval(std::string& cmd)
else if (compare(cmd, "off") or compare(cmd, "on"))
{
bool active=getword(cmd)=="on";
uint8_t ever;
if (compare(cmd, "all"))
ever=100;
else
ever=getint(cmd, 99);
uint8_t ever=getint(cmd, 100);
uint8_t count=0;
if (ever == 99)
for(auto& every: everies)
{
Serial << "Missing every number" << endl;
}
else
{
for(auto& every: everies)
if (count==ever or (ever==100))
{
if (count==ever or (ever==100))
if (every.active != active)
{
if (every.active != active)
{
every.active = active;
every.underrun = 0;
}
ever = 99;
break;
Serial << "every #" << count << (active ? " on" :" off") << endl;
every.active = active;
every.underrun = 0;
}
count++;
}
if (ever != 99)
{
Serial << "Every not found" << endl;
}
count++;
}
}
else if (compare(cmd, "list") or cmd.length()==0)
@@ -725,9 +784,11 @@ void eval(std::string& cmd)
Serial << " set [name][value]" << endl;
Serial << " ! repeat last command" << endl;
Serial << endl;
Serial << " every ms [command]; every list; every remove [nr|all], every [on|off] #" << endl;
Serial << " echo [on|off] or strings" << endl;
Serial << " every ms [command]; every list; every remove [nr|all], every (on|off) [#]" << endl;
Serial << " on {output}; off {output}" << endl;
Serial << " $id : name of the client." << endl;
Serial << " rnd[(min[,max])] random number." << endl;
Serial << " default topic is '" << topic.c_str() << "'" << endl;
Serial << endl;
}
@@ -811,8 +872,10 @@ void loop()
{
static std::string cmd;
char c=Serial.read();
if (echo_on)
Serial << c;
if (c==10 or c==14)
if (c==10 or c==13)
{
Serial << "----------------[ " << cmd.c_str() << " ]--------------" << endl;
static std::string last_cmd;

View File

@@ -6,7 +6,7 @@
"type": "git",
"url": "https://github.com/hsaturn/TinyMqtt.git"
},
"version": "0.7.5",
"version": "0.7.7",
"exclude": "",
"examples": "examples/*/*.ino",
"frameworks": "arduino",

View File

@@ -1,5 +1,5 @@
name=TinyMqtt
version=0.7.5
version=0.7.7
author=Francois BIOT, HSaturn, <hsaturn@gmail.com>
maintainer=Francois BIOT, HSaturn, <hsaturn@gmail.com>
sentence=A tiny broker and client library for MQTT messaging.

View File

@@ -665,7 +665,12 @@ void MqttMessage::incoming(char in_byte)
state = Length;
break;
case Length:
size = (size<<7) + (in_byte & 0x3F);
if (size==0)
size = in_byte & 0x7F;
else if (size<128)
size += static_cast<uint16_t>(in_byte & 0x7F)<<7;
else
state = Error; // Really don't want to handle msg with length > 16k
if (size > MaxBufferLength)
{
state = Error;

View File

@@ -10,11 +10,10 @@
#include <ESP8266WiFi.h>
#endif
#elif defined(ESP32)
#include <WiFi.h>
#ifdef TCP_ASYNC
#include <AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
#else
#include <WiFi.h>
#endif
#include <AsyncTCP.h> // https://github.com/me-no-dev/AsyncTCP
#endif
#endif
#ifdef EPOXY_DUINO
#define dbg_ptr uint64_t
@@ -65,7 +64,7 @@ class Topic : public IndexedString
class MqttClient;
class MqttMessage
{
const uint16_t MaxBufferLength = 255;
const uint16_t MaxBufferLength = 4096; //hard limit: 16k
public:
enum Type
{
@@ -177,6 +176,7 @@ class MqttClient
// Publish from client to the world
MqttError publish(const Topic&, const char* payload, size_t pay_length);
MqttError publish(const Topic& t, const char* payload) { return publish(t, payload, strlen(payload)); }
MqttError publish(const Topic& t, const String& s) { return publish(t, s.c_str(), s.length()); }
MqttError publish(const Topic& t, const std::string& s) { return publish(t,s.c_str(),s.length());}
MqttError publish(const Topic& t) { return publish(t, nullptr, 0);};

View File

@@ -0,0 +1,8 @@
# See https://github.com/bxparks/EpoxyDuino for documentation about this
# Makefile to compile and run Arduino programs natively on Linux or MacOS.
APP_NAME := length-tests
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP
ARDUINO_LIB_DIRS := ../../../EspMock/libraries
EPOXY_CORE := EPOXY_CORE_ESP8266
include ../../../EpoxyDuino/EpoxyDuino.mk

View File

@@ -0,0 +1,53 @@
#include <AUnit.h>
#include <TinyMqtt.h>
#include <map>
/**
* TinyMqtt local unit tests.
*
* Clients are connected to pseudo remote broker
* The remote should be 127.0.0.1:1883 <--- But this does not work due to Esp network limitations
* We are using 127.0.0.1 because this is simpler to test with a single ESP
* Also, this will allow to mock and thus run Action on github
**/
using namespace std;
MqttBroker broker(1883);
std::map<std::string, std::map<Topic, int>> published; // map[client_id] => map[topic] = count
const char* lastPayload;
size_t lastLength;
void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length)
{
if (srce)
published[srce->id()][topic]++;
lastPayload = payload;
lastLength = length;
}
test(length_decode_greater_than_127)
{
// TODO WRITE THIS TEST
// The test should verify than a mqtt message with more than 127 bytes is correctly decoded
assertEqual(1,2);
}
//----------------------------------------------------------------------------
// setup() and loop()
void setup() {
delay(1000);
Serial.begin(115200);
while(!Serial);
Serial.println("=============[ NO WIFI CONNECTION TinyMqtt TESTS ]========================");
}
void loop() {
aunit::TestRunner::run();
if (Serial.available()) ESP.reset();
}

View File

@@ -2,6 +2,7 @@
# Makefile to compile and run Arduino programs natively on Linux or MacOS.
APP_NAME := local-tests
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock
ESP_LIBS = ESP8266WiFi ESPAsyncTCP
include ../../../EspMock/EspMock.mk
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP
ARDUINO_LIB_DIRS := ../../../EspMock/libraries
EPOXY_CORE := EPOXY_CORE_ESP8266
include ../../../EpoxyDuino/EpoxyDuino.mk

View File

@@ -2,6 +2,7 @@
# Makefile to compile and run Arduino programs natively on Linux or MacOS.
APP_NAME := nowifi-tests
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock
ESP_LIBS = ESP8266WiFi ESPAsyncTCP
include ../../../EspMock/EspMock.mk
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP
ARDUINO_LIB_DIRS := ../../../EspMock/libraries
EPOXY_CORE := EPOXY_CORE_ESP8266
include ../../../EpoxyDuino/EpoxyDuino.mk

View File

@@ -2,6 +2,7 @@
# Makefile to compile and run Arduino programs natively on Linux or MacOS.
APP_NAME := string-indexer-tests
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock
ESP_LIBS = ESP8266WiFi ESPAsyncTCP
include ../../../EspMock/EspMock.mk
ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsync
ARDUINO_LIB_DIRS := ../../../EspMock/libraries
EPOXY_CORE := EPOXY_CORE_ESP8266
include ../../../EpoxyDuino/EpoxyDuino.mk

View File

@@ -105,5 +105,5 @@ void setup() {
void loop() {
aunit::TestRunner::run();
if (Serial.available()) ESP.reset();
// if (Serial.available()) ESP.reset();
}