Skip to content

Commit ab07f40

Browse files
authored
Merge pull request #19 from ktos/v2
Version 2
2 parents 3e4439c + 1df3444 commit ab07f40

23 files changed

+1596
-914
lines changed

.github/workflows/ci.yml

+3-4
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,10 @@ jobs:
88
runs-on: ubuntu-latest
99
strategy:
1010
matrix:
11-
example: [examples/DebugBasic/DebugBasic.ino, examples/ErrorBasic/ErrorBasic.ino, examples/IntervalBasic/IntervalBasic.ino, examples/WifiMqtt/WifiMqtt.ino]
11+
example: [examples/DebugBasic/DebugBasic.ino, examples/ErrorBasic/ErrorBasic.ino, examples/IntervalBasic/IntervalBasic.ino, examples/WifiMqtt/WifiMqtt.ino, examples/MqttSubscribe/MqttSubscribe.ino, examples/MDNSService/MDNSService.ino]
1212

1313
steps:
14-
- uses: actions/checkout@44c2b7a8a4ea60a981eaca3cf939b5f4305c123b # v4
14+
- uses: actions/checkout@v4
1515
- name: Cache pip
1616
uses: actions/cache@v4
1717
with:
@@ -35,8 +35,7 @@ jobs:
3535
3636
- name: Install library dependencies
3737
run: |
38-
pio pkg install -g -l bblanchon/ArduinoJson@~6.21.1
39-
pio pkg install -g -l https://github.com/ktos/RemoteDebug#v3.0.7
38+
pio pkg install -g -l bblanchon/ArduinoJson@^6.21.5
4039
pio pkg install -g -l knolleary/[email protected]
4140
pio pkg install -g -l sstaub/[email protected]
4241

examples/DebugBasic/DebugBasic.ino

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#include <Mokosh.hpp>
22

3-
Mokosh mokosh;
3+
Mokosh mokosh("Mokosh");
44

55
// callback for custom command handling
6-
void customCommand(String command)
6+
void customCommand(String command, String param)
77
{
88
// you can use mdebugI, mdebugE and so on
99
// to automatically work with RemoteDebug
@@ -19,12 +19,12 @@ void setup()
1919
{
2020
// debug set to Verbose will display everything
2121
// available are: verbose, debug, info, warning, error
22-
mokosh.setDebugLevel(DebugLevel::VERBOSE);
22+
mokosh.setLogLevel(LogLevel::VERBOSE);
2323
mokosh.onCommand = customCommand;
2424

2525
// no custom configuration is set, so it will try to load
2626
// from /config.json in LittleFS
27-
mokosh.begin("Mokosh");
27+
mokosh.begin();
2828
}
2929

3030
void loop()

examples/ErrorBasic/ErrorBasic.ino

+5-3
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,15 @@
22

33
Mokosh m;
44

5-
void setup() {
5+
void setup()
6+
{
67
// will automatically reboot if error is met
78
m.setRebootOnError(true);
8-
m.begin("Mokosh");
9+
m.begin();
910
}
1011

11-
void loop() {
12+
void loop()
13+
{
1214
delay(1000);
1315
m.error(201); // throw error code 201 (will reboot)
1416
}

examples/ErrorHandler/ErrorHandler.ino

+15-10
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,39 @@
22

33
Mokosh m;
44

5-
void on_error(int code) {
6-
if (code == 201) {
5+
void on_error(int code)
6+
{
7+
if (code == 201)
8+
{
79
mdebugE("Error 201!");
810
pinMode(LED_BUILTIN, HIGH);
911
delay(1000);
1012
pinMode(LED_BUILTIN, LOW);
1113
}
1214

13-
if (code == 202) {
15+
if (code == 202)
16+
{
1417
mdebugE("Error 202!");
1518
}
1619
}
1720

18-
void setup() {
19-
pinMode(LED_BUILTIN, OUTPUT);
21+
void setup()
22+
{
23+
pinMode(2, OUTPUT);
2024
// custom error handler
2125
m.onError = on_error;
2226

23-
m.setDebugLevel(DebugLevel::VERBOSE);
27+
m.setLogLevel(LogLevel::VERBOSE);
2428

25-
m.begin("Mokosh");
29+
m.begin();
2630
}
2731

28-
void loop() {
32+
void loop()
33+
{
2934
delay(1000);
3035
m.error(201); // error code 201
3136
delay(1000);
3237
m.error(202);
33-
34-
m.loop();
38+
39+
m.loop();
3540
}

examples/IntervalBasic/IntervalBasic.ino

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,16 @@
11
#include <Mokosh.hpp>
22

3-
Mokosh mokosh;
3+
Mokosh mokosh("Mokosh");
44

55
void some_func()
66
{
7-
mokosh.publish("test", "alive!");
7+
auto mqtt = mokosh.getMqttService();
8+
mqtt->publish("test", "alive!");
89
}
910

1011
void setup()
1112
{
12-
mokosh.begin("Mokosh");
13+
mokosh.begin();
1314

1415
mokosh.registerIntervalFunction(some_func, 1200); // register some_func to run every 1.2 seconds
1516
}

examples/MDNSService/MDNSService.ino

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#include <Mokosh.hpp>
2+
#include <MokoshMDNSService.hpp>
3+
4+
Mokosh m;
5+
6+
void setup()
7+
{
8+
m.setLogLevel(LogLevel::VERBOSE);
9+
m.registerService(MokoshServices::MDNSService::KEY, std::make_shared<MokoshServices::MDNSService>());
10+
11+
m.begin();
12+
13+
auto mdns = m.getRegisteredService<MokoshServices::MDNSService>(MokoshServices::MDNSService::KEY);
14+
mdns->addMDNSService("TEST", "HTTP", 80);
15+
}
16+
17+
void loop()
18+
{
19+
m.loop();
20+
}
+30
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#include <Mokosh.hpp>
2+
3+
Mokosh m;
4+
5+
void setup()
6+
{
7+
m.setLogLevel(LogLevel::VERBOSE);
8+
m.begin();
9+
10+
auto mqtt = m.getMqttService();
11+
12+
mqtt->onMessage = [](String topic, uint8_t *msg, unsigned int length)
13+
{
14+
#if defined(ESP32)
15+
auto mess = String(msg, length);
16+
#elif defined(ESP8266)
17+
char ch[length];
18+
strcpy(ch, (const char *)msg);
19+
String mess(ch);
20+
#endif
21+
22+
mdebugI("Received message on topic %s: %s", topic.c_str(), mess.c_str());
23+
};
24+
mqtt->subscribe("some/topic");
25+
}
26+
27+
void loop()
28+
{
29+
m.loop();
30+
}

examples/WifiMqtt/WifiMqtt.ino

+16-14
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
#include <Mokosh.hpp>
22

3-
// global object for the framework
4-
Mokosh mokosh;
3+
// global object for the framework, disable using config file
4+
Mokosh mokosh("Mokosh", "1.0.0", false);
55

6-
void setup() {
6+
void setup()
7+
{
78
srand(millis());
89

910
// static configuration to be used: Wifi with yourssid and yourpassword password
1011
// mqtt broker at 192.168.1.10, port 1883
11-
12-
mokosh.setConfigFile(false);
13-
mokosh.config.set(mokosh.config.key_ssid, "yourssid");
14-
mokosh.config.set(mokosh.config.key_wifi_password, "yourpassword");
15-
mokosh.config.set(mokosh.config.key_broker, "192.168.1.10");
16-
mokosh.config.set(mokosh.config.key_broker_port, 1883);
12+
mokosh.config->set(mokosh.config->key_ssid, "yourssid");
13+
mokosh.config->set(mokosh.config->key_wifi_password, "yourpass");
14+
mokosh.config->set(mokosh.config->key_broker, "192.168.1.10");
15+
mokosh.config->set(mokosh.config->key_broker_port, 1883);
1716

1817
// device id will be Mokosh_ABCDE where ABCDE is a chip id
19-
mokosh.begin("Mokosh");
18+
mokosh.begin();
2019
}
2120

2221
long lastMessage = 0;
2322

24-
void loop() {
23+
void loop()
24+
{
2525
// publish new random int message every 2000 ms
26-
27-
if (millis() - lastMessage > 2000) {
26+
27+
if (millis() - lastMessage > 2000)
28+
{
2829
// will publish to Mokosh_ABCDE/rand topic
29-
mokosh.publish("rand", rand());
30+
auto mqtt = mokosh.getMqttService();
31+
mqtt->publish("rand", rand());
3032
lastMessage = millis();
3133
}
3234

src/DebugAdapter.hpp

+90
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#ifndef DEBUGADAPTER_H
2+
#define DEBUGADAPTER_H
3+
4+
#include "MokoshService.hpp"
5+
6+
// Debug level - starts from 0 to 6, higher is more severe
7+
typedef enum LogLevel
8+
{
9+
PROFILER = 0,
10+
DEBUG = 1,
11+
VERBOSE = 2,
12+
INFO = 3,
13+
WARNING = 4,
14+
ERROR = 5,
15+
ANY = 6
16+
} LogLevel;
17+
18+
class DebugAdapter : public MokoshService
19+
{
20+
public:
21+
virtual bool isActive(LogLevel level) = 0;
22+
virtual void setActive(LogLevel level) = 0;
23+
virtual void debugf(LogLevel level, const char *func, const char *file, int line, long time, const char *msg) = 0;
24+
virtual void ticker_step() = 0;
25+
virtual void ticker_finish(bool success) = 0;
26+
};
27+
28+
class SerialDebugAdapter : public DebugAdapter
29+
{
30+
public:
31+
virtual bool setup()
32+
{
33+
return true;
34+
}
35+
36+
virtual void loop() {}
37+
38+
virtual bool isActive(LogLevel level)
39+
{
40+
return true;
41+
}
42+
43+
virtual void setActive(LogLevel level) {}
44+
45+
virtual void debugf(LogLevel level, const char *func, const char *file, int line, long time, const char *msg)
46+
{
47+
char lvl;
48+
switch (level)
49+
{
50+
case LogLevel::DEBUG:
51+
lvl = 'D';
52+
break;
53+
case LogLevel::ERROR:
54+
lvl = 'E';
55+
break;
56+
case LogLevel::INFO:
57+
lvl = 'I';
58+
break;
59+
case LogLevel::WARNING:
60+
lvl = 'W';
61+
break;
62+
case LogLevel::VERBOSE:
63+
lvl = 'V';
64+
break;
65+
default:
66+
lvl = 'A';
67+
}
68+
69+
Serial.printf("(%c t:%ldms) (%s %s:%d) %s\n", lvl, time, func, file, line, msg);
70+
}
71+
72+
virtual void ticker_step() override
73+
{
74+
Serial.print(".");
75+
}
76+
77+
virtual void ticker_finish(bool success) override
78+
{
79+
if (success)
80+
{
81+
Serial.println(" ok");
82+
}
83+
else
84+
{
85+
Serial.println(" fail");
86+
}
87+
}
88+
};
89+
90+
#endif

0 commit comments

Comments
 (0)