From a0c41a0ccb67bd2093e080784709070215d92e41 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Fri, 26 Mar 2021 01:51:06 +0100 Subject: [PATCH] Smart ip feature for connect --- examples/tinymqtt-test/commands.txt | 11 ++++-- examples/tinymqtt-test/tinymqtt-test.ino | 43 +++++++++++++++++++++++- 2 files changed, 50 insertions(+), 4 deletions(-) diff --git a/examples/tinymqtt-test/commands.txt b/examples/tinymqtt-test/commands.txt index 6f6b8cb..fe3c2e3 100644 --- a/examples/tinymqtt-test/commands.txt +++ b/examples/tinymqtt-test/commands.txt @@ -1,9 +1,9 @@ -// vim: ts=30 +// vim: ts=40 Exemple of commands that can be sent via the serial monitor to tinymqtt-test ---------------------------------------------------------------------------- - Commands can usually be abbreviated to their first letters. ex: cl for client, a / a.con / a.sub / a.p for publish. +-------- set name value set variable name to value (later replaced) set name if no value, then var is erased @@ -17,9 +17,12 @@ a.publish topic [payload] send a topic with a payload a.subscribe topic subscribes to a topic delete a destroy the client ----------------------------------------------------- +* note, if 'server' is a number, then it replaces the end of the local ip. +i.e. if local ip is 192.168.1.10, connect 2.35 becomes 192.168.2.35 +-------- example: +-------- client c c.connect broker.emqx.io @@ -30,5 +33,7 @@ c.publish topic 15 c.publish topic 20 macro exansion example +---------------------- + set temp publish sensor/temperature c.temp 20 -> c.publish sensor/temperature 20 diff --git a/examples/tinymqtt-test/tinymqtt-test.ino b/examples/tinymqtt-test/tinymqtt-test.ino index afee68c..f03880e 100644 --- a/examples/tinymqtt-test/tinymqtt-test.ino +++ b/examples/tinymqtt-test/tinymqtt-test.ino @@ -1,6 +1,7 @@ #define TINY_MQTT_DEBUG #include // https://github.com/hsaturn/TinyMqtt #include +#include #include /** @@ -78,6 +79,46 @@ std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' ' return sword; } +bool isaddr(std::string s) +{ + if (s.length()==0 or s.length()>3) return false; + for(char c: s) + if (c<'0' or c>'9') return false; + return true; +} + +std::string getip(std::string& str, const char* if_empty=nullptr, char sep=' ') +{ + std::string addr=getword(str, if_empty, sep); + std::string ip=addr; + std::vector build; + bool ok=true; + while(ip.length()) + { + std::string b=getword(ip,nullptr,'.'); + if (isaddr(b) && build.size()<4) + { + build.push_back(b); + } + else + return addr; + } + IPAddress local=WiFi.localIP(); + addr=""; + while(build.size()!=4) + { + std::stringstream b; + b << (int)local[3-build.size()]; + build.insert(build.begin(), b.str()); + } + for(std::string s: build) + { + if (addr.length()) addr += '.'; + addr += s; + } + return addr; +} + std::map vars; std::set commands = { @@ -389,7 +430,7 @@ void loop() { if (compare(s,"connect")) { - client->connect(getword(cmd,"192.168.1.40").c_str(), getint(cmd, 1883), getint(cmd, 60)); + client->connect(getip(cmd,"192.168.1.40").c_str(), getint(cmd, 1883), getint(cmd, 60)); Serial << (client->connected() ? "connected." : "not connected") << endl; } else if (compare(s,"publish"))