Skip to content

Commit f6638e6

Browse files
authored
Merge pull request #43 from thinger-io/webconfig
Improve OTA and Webconfig
2 parents bdca7db + 72390f0 commit f6638e6

File tree

16 files changed

+699
-257
lines changed

16 files changed

+699
-257
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
.vscode/settings.json
2+
.DS_Store

CHANGELOG.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
2.25.1
2+
3+
- Fix ESP32 Webconfig issue
4+
- Improve ESP8266 NTP sync
5+
6+
2.25.0
7+
8+
- Add support for ESP32 Webconfig
9+
- Improve OTA by stopping user flows, handling timeouts, providing callbacks, etc.
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#define THINGER_SERIAL_DEBUG
2+
3+
// Requires WifiManager from Library Manager or https://github.com/tzapu/WiFiManager
4+
// At least version 2.0 (available on master)
5+
#include <ThingerESP32WebConfig.h>
6+
7+
ThingerESP32WebConfig thing;
8+
9+
void setup() {
10+
// open serial for debugging
11+
Serial.begin(115200);
12+
13+
pinMode(LED_BUILTIN, OUTPUT);
14+
15+
// resource output example (i.e. reading a sensor value)
16+
thing["millis"] >> outputValue(millis());
17+
18+
/*
19+
Steps for getting the ESP8266 WebConfig working:
20+
1. Connect to Thinger-Device WiFi with your computer or phone, using thinger.io as WiFi password
21+
2. Wait for the configuration window, or navigate to http://192.168.4.1 if it does not appear
22+
3. Configure the wifi where the ESP8266 will be connected, and your thinger.io device credentials
23+
4. Your device should be now connected to the platform.
24+
More details at http://docs.thinger.io/arduino/
25+
*/
26+
}
27+
28+
void loop() {
29+
thing.handle();
30+
}

examples/ESP8266/ESP8266WebConfig/ESP8266WebConfig.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
#define THINGER_SERIAL_DEBUG
22

33
// Requires WifiManager from Library Manager or https://github.com/tzapu/WiFiManager
4-
#include <ThingerWebConfig.h>
4+
#include <ThingerESP8266WebConfig.h>
55

6-
ThingerWebConfig thing;
6+
ThingerESP8266WebConfig thing;
77

88
void setup() {
99
// open serial for debugging

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=thinger.io
2-
version=2.21.1
2+
version=2.25.1
33
author=Alvaro Luis Bustamante <[email protected]>
44
maintainer=Thinger.io <[email protected]>
55
sentence=Arduino library for the Thinger.io Internet of Things Platform.

src/ThingerBC66.h

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2017 THINK BIG LABS SL
4+
// Author: [email protected] (Alvaro Luis Bustamante)
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
#ifndef THINGER_MKRNB_H
25+
#define THINGER_MKRNB_H
26+
27+
28+
#define GPRS_CONNECTION_TIMEOUT 30000
29+
30+
#include <LClient.h>
31+
#include "ThingerClient.h"
32+
33+
class ThingerBC66 : public ThingerClient {
34+
35+
public:
36+
ThingerBC66(const char* user, const char* device, const char* device_credential) :
37+
ThingerClient(client_, user, device, device_credential)
38+
{
39+
40+
41+
}
42+
43+
~ThingerBC66(){
44+
45+
}
46+
47+
protected:
48+
49+
virtual bool network_connected(){
50+
return connected_;
51+
}
52+
53+
virtual bool connect_network(){
54+
Dev.cereg(true);
55+
connected_ = true;
56+
}
57+
58+
virtual bool secure_connection(){
59+
return false;
60+
}
61+
62+
protected:
63+
LClient client_;
64+
bool connected_ = false;
65+
66+
};
67+
68+
#endif

src/ThingerClient.h

Lines changed: 5 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -470,7 +470,10 @@ class ThingerClient : public thinger::thinger {
470470
public:
471471

472472
void reboot(){
473-
reboot_ = true;
473+
delay(1000);
474+
stop();
475+
delay(1000);
476+
run_reboot();
474477
}
475478

476479
void stop(){
@@ -492,14 +495,6 @@ class ThingerClient : public thinger::thinger {
492495
}else{
493496
delay(RECONNECTION_TIMEOUT); // get some delay for a connection retry
494497
}
495-
496-
if(reboot_){
497-
reboot_ = false;
498-
delay(1000);
499-
stop();
500-
delay(1000);
501-
run_reboot();
502-
}
503498
}
504499

505500
bool is_connected(){
@@ -553,13 +548,13 @@ class ThingerClient : public thinger::thinger {
553548
const char* device_password_;
554549
const char* host_;
555550
const char* root_ca_;
556-
bool reboot_ = false;
557551
std::function<void(THINGER_STATE)> state_listener_;
558552
#ifndef THINGER_DISABLE_OUTPUT_BUFFER
559553
uint8_t * out_buffer_;
560554
size_t out_size_;
561555
size_t out_total_size_;
562556
#endif
557+
563558
};
564559

565560
/**

src/ThingerESP32WebConfig.h

Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
// The MIT License (MIT)
2+
//
3+
// Copyright (c) 2017 THINK BIG LABS SL
4+
// Author: [email protected] (Alvaro Luis Bustamante)
5+
//
6+
// Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// of this software and associated documentation files (the "Software"), to deal
8+
// in the Software without restriction, including without limitation the rights
9+
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// copies of the Software, and to permit persons to whom the Software is
11+
// furnished to do so, subject to the following conditions:
12+
//
13+
// The above copyright notice and this permission notice shall be included in
14+
// all copies or substantial portions of the Software.
15+
//
16+
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
// THE SOFTWARE.
23+
24+
#ifndef THINGER_ESP32_WEBCONFIG_H
25+
#define THINGER_ESP32_WEBCONFIG_H
26+
#define FORMAT_SPIFFS_IF_FAILED true
27+
28+
#include <FS.h>
29+
#include <SPIFFS.h>
30+
#include "ThingerESP32.h"
31+
#include "ThingerWebConfig.h"
32+
33+
34+
class pson_spiffs_decoder : public protoson::pson_decoder{
35+
public:
36+
pson_spiffs_decoder(File& file) : file_(file)
37+
{
38+
}
39+
40+
protected:
41+
virtual bool read(void* buffer, size_t size){
42+
size_t read = file_.readBytes((char*)buffer, size);
43+
protoson::pson_decoder::read(buffer, read);
44+
return read == size;
45+
}
46+
47+
private:
48+
File& file_;
49+
};
50+
51+
class pson_spiffs_encoder : public protoson::pson_encoder{
52+
public:
53+
pson_spiffs_encoder(File& file) : file_(file)
54+
{
55+
}
56+
57+
protected:
58+
virtual bool write(const void* buffer, size_t size){
59+
size_t wrote = file_.write((const uint8_t*)buffer, size);
60+
protoson::pson_encoder::write(buffer, wrote);
61+
return wrote == size;
62+
}
63+
64+
private:
65+
File& file_;
66+
};
67+
68+
class ThingerESP32WebConfig : public ThingerWebConfig<ThingerESP32>{
69+
70+
public:
71+
ThingerESP32WebConfig(const char* user="", const char* device="", const char* credential="") :
72+
ThingerWebConfig<ThingerESP32>(user, device, credential) {
73+
74+
}
75+
76+
bool clean_credentials() override{
77+
THINGER_DEBUG("_CONFIG", "Cleaning credentials...");
78+
// clean Thinger.io credentials from file system
79+
if(SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
80+
THINGER_DEBUG("_CONFIG", "FS Mounted!");
81+
if (SPIFFS.exists(CONFIG_FILE)) {
82+
THINGER_DEBUG("_CONFIG", "Removing Config File...");
83+
if(SPIFFS.remove(CONFIG_FILE)){
84+
THINGER_DEBUG("_CONFIG", "Config file removed!");
85+
return true;
86+
}else{
87+
THINGER_DEBUG("_CONFIG", "Cannot delete config file!");
88+
}
89+
}else{
90+
THINGER_DEBUG("_CONFIG", "No config file to delete!");
91+
}
92+
SPIFFS.end();
93+
}
94+
return false;
95+
}
96+
97+
98+
protected:
99+
100+
bool save_configuration(pson& config) override{
101+
THINGER_DEBUG("_CONFIG", "Updating Device Info...");
102+
if (SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
103+
File configFile = SPIFFS.open(CONFIG_FILE, "w");
104+
if (configFile) {
105+
pson_spiffs_encoder encoder(configFile);
106+
encoder.encode(config);
107+
configFile.close();
108+
THINGER_DEBUG("_CONFIG", "Done!");
109+
return true;
110+
} else {
111+
THINGER_DEBUG("_CONFIG", "Failed to open config file for writing!");
112+
}
113+
SPIFFS.end();
114+
}
115+
return false;
116+
}
117+
118+
bool load_configuration(pson& config) override{
119+
THINGER_DEBUG("_CONFIG", "Mounting FS...");
120+
if (SPIFFS.begin(FORMAT_SPIFFS_IF_FAILED)) {
121+
THINGER_DEBUG("_CONFIG", "FS Mounted!");
122+
if (SPIFFS.exists(CONFIG_FILE)) {
123+
//file exists, reading and loading
124+
THINGER_DEBUG("_CONFIG", "Opening Config File...");
125+
File configFile = SPIFFS.open(CONFIG_FILE, "r");
126+
if(configFile){
127+
THINGER_DEBUG("_CONFIG", "Config File is Open!");
128+
pson_spiffs_decoder decoder(configFile);
129+
bool result = decoder.decode(config);
130+
configFile.close();
131+
return result;
132+
}else{
133+
THINGER_DEBUG("_CONFIG", "Config File is Not Available!");
134+
}
135+
}
136+
// close SPIFFS
137+
SPIFFS.end();
138+
} else {
139+
THINGER_DEBUG("_CONFIG", "Failed to Mount FS!");
140+
}
141+
return false;
142+
}
143+
144+
};
145+
146+
147+
148+
#endif

0 commit comments

Comments
 (0)