Smart ip feature for connect

This commit is contained in:
hsaturn
2021-03-26 01:51:06 +01:00
parent b780dcf99c
commit a0c41a0ccb
2 changed files with 50 additions and 4 deletions

View File

@@ -1,9 +1,9 @@
// vim: ts=30 // vim: ts=40
Exemple of commands that can be sent via the serial monitor to tinymqtt-test Exemple of commands that can be sent via the serial monitor to tinymqtt-test
---------------------------------------------------------------------------- ----------------------------------------------------------------------------
Commands can usually be abbreviated to their first letters. Commands can usually be abbreviated to their first letters.
ex: cl for client, a / a.con / a.sub / a.p for publish. 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 value set variable name to value (later replaced)
set name if no value, then var is erased 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 a.subscribe topic subscribes to a topic
delete a destroy the client 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: example:
--------
client c client c
c.connect broker.emqx.io c.connect broker.emqx.io
@@ -30,5 +33,7 @@ c.publish topic 15
c.publish topic 20 c.publish topic 20
macro exansion example macro exansion example
----------------------
set temp publish sensor/temperature set temp publish sensor/temperature
c.temp 20 -> c.publish sensor/temperature 20 c.temp 20 -> c.publish sensor/temperature 20

View File

@@ -1,6 +1,7 @@
#define TINY_MQTT_DEBUG #define TINY_MQTT_DEBUG
#include <TinyMqtt.h> // https://github.com/hsaturn/TinyMqtt #include <TinyMqtt.h> // https://github.com/hsaturn/TinyMqtt
#include <MqttStreaming.h> #include <MqttStreaming.h>
#include <sstream>
#include <map> #include <map>
/** /**
@@ -78,6 +79,46 @@ std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' '
return sword; 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<std::string> 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<std::string, std::string> vars; std::map<std::string, std::string> vars;
std::set<std::string> commands = { std::set<std::string> commands = {
@@ -389,7 +430,7 @@ void loop()
{ {
if (compare(s,"connect")) 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; Serial << (client->connected() ? "connected." : "not connected") << endl;
} }
else if (compare(s,"publish")) else if (compare(s,"publish"))