test client enhancements

This commit is contained in:
hsaturn
2021-03-24 21:19:44 +01:00
parent 7107da2cce
commit 0d6e194560
2 changed files with 23 additions and 8 deletions

View File

@@ -0,0 +1,11 @@
// vim: ts=30
Exemple of commands that can be sent via the serial monitor to tinymqtt-test
----------------------------------------------------------------------------
client a starts a client (not connected no internal broker)
a.connect [server][port][alive] connects the client, default port=1883
a.publish topic [payload] send a topic with a payload
a.subscribe topic subscribes to a topic
delete a destroy the client

View File

@@ -54,15 +54,15 @@ void setup()
brokers["broker"] = broker; brokers["broker"] = broker;
} }
int getint(std::string& str, const int if_empty=0, char sep=' ') int getint(std::string& str, const int if_empty=0)
{ {
std::string sword; std::string sword;
while(str.length() && str[0]!=sep) while(str.length() && str[0]>='0' && str[0]<='9')
{ {
sword += str[0]; str.erase(0,1); sword += str[0]; str.erase(0,1);
} }
while(str[0]==sep) str.erase(0,1); while(str[0]==' ') str.erase(0,1);
if (if_empty and sword.length()==0) sword=if_empty; if (if_empty and sword.length()==0) return if_empty;
return atoi(sword.c_str()); return atoi(sword.c_str());
} }
@@ -78,6 +78,7 @@ std::string getword(std::string& str, const char* if_empty=nullptr, char sep=' '
return sword; return sword;
} }
// publish at regular interval
class automatic class automatic
{ {
public: public:
@@ -189,6 +190,7 @@ class automatic
std::string topic_; std::string topic_;
bool bon=false; bool bon=false;
static std::map<MqttClient*, automatic*> autos; static std::map<MqttClient*, automatic*> autos;
float temp=19;
}; };
std::map<MqttClient*, automatic*> automatic::autos; std::map<MqttClient*, automatic*> automatic::autos;
@@ -311,12 +313,14 @@ void loop()
{ {
if (compare(s,"connect")) if (compare(s,"connect"))
{ {
client->connect(getword(cmd,"192.168.1.40").c_str(), 1883); client->connect(getword(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"))
{ {
retval = client->publish(getword(cmd, topic.c_str())); while (cmd[0]==' ') cmd.erase(0,1);
retval = client->publish(getword(cmd, topic.c_str()), cmd.c_str(), cmd.length());
cmd=""; // remove payload
} }
else if (compare(s,"subscribe")) else if (compare(s,"subscribe"))
{ {
@@ -407,9 +411,9 @@ void loop()
Serial << endl; Serial << endl;
Serial << " MqttClient:" << endl; Serial << " MqttClient:" << endl;
Serial << " client {name} {parent broker} : create a client then" << endl; Serial << " client {name} {parent broker} : create a client then" << endl;
Serial << " name.connect [ip]" << endl; Serial << " name.connect [ip] [port] [alive]" << endl;
Serial << " name.subscribe [topic]" << endl; Serial << " name.subscribe [topic]" << endl;
Serial << " name.publish [topic]" << endl; Serial << " name.publish [topic][payload]" << endl;
Serial << " name.view" << endl; Serial << " name.view" << endl;
Serial << " name.delete" << endl; Serial << " name.delete" << endl;