Skip to content
This repository was archived by the owner on Feb 4, 2023. It is now read-only.

Commit 2c59b06

Browse files
authored
v1.5.1 to fix String-related bug
### Releases v1.5.1 1. Fix bug related to String in library and examples
1 parent 85e6329 commit 2c59b06

File tree

13 files changed

+80
-59
lines changed

13 files changed

+80
-59
lines changed

examples/AdvancedWebServer/AdvancedWebServer.ino

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@
3636
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
3737
SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
3838
***************************************************************************************************************************************/
39+
3940
#include "defines.h"
4041

4142
int status = WL_IDLE_STATUS; // the Wifi radio's status
@@ -47,7 +48,7 @@ const int led = 13;
4748

4849
void handleRoot()
4950
{
50-
#define BUFFER_SIZE 500
51+
#define BUFFER_SIZE 512
5152

5253
digitalWrite(led, 1);
5354
char temp[BUFFER_SIZE];
@@ -104,61 +105,52 @@ void handleNotFound()
104105
digitalWrite(led, 0);
105106
}
106107

107-
#if (defined(WIFI_WEBSERVER_VERSION_INT) && (WIFI_WEBSERVER_VERSION_INT >= 1005000))
108-
109-
WWString initHeader = "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n" \
110-
"<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"3\" stroke=\"rgb(0, 0, 0)\" />\n" \
111-
"<g stroke=\"blue\">\n";
108+
#define ORIGINAL_STR_LEN 2048
112109

113110
void drawGraph()
114111
{
115-
WWString out;
116-
117-
out.reserve(3000);
118-
char temp[70];
119-
120-
out += initHeader;
121-
122-
int y = rand() % 130;
112+
static String out;
113+
static uint16_t previousStrLen = ORIGINAL_STR_LEN;
123114

124-
for (int x = 10; x < 300; x += 10)
115+
if (out.length() == 0)
125116
{
126-
int y2 = rand() % 130;
127-
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
128-
out += temp;
129-
y = y2;
117+
WS_LOGWARN1(F("String Len = 0, extend to"), ORIGINAL_STR_LEN);
118+
out.reserve(ORIGINAL_STR_LEN);
130119
}
131-
out += "</g>\n</svg>\n";
132-
133-
server.send(200, "image/svg+xml", fromWWString(out));
134-
}
135120

136-
#else
121+
out = F( "<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n" \
122+
"<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"3\" stroke=\"rgb(0, 0, 0)\" />\n" \
123+
"<g stroke=\"blue\">\n");
137124

138-
void drawGraph()
139-
{
140-
String out;
141-
out.reserve(3000);
142125
char temp[70];
143126

144-
out += F("<svg xmlns=\"http://www.w3.org/2000/svg\" version=\"1.1\" width=\"310\" height=\"150\">\n");
145-
out += F("<rect width=\"310\" height=\"150\" fill=\"rgb(250, 230, 210)\" stroke-width=\"3\" stroke=\"rgb(0, 0, 0)\" />\n");
146-
out += F("<g stroke=\"blue\">\n");
147127
int y = rand() % 130;
148128

149129
for (int x = 10; x < 300; x += 10)
150130
{
151-
int y2 = ( rand() ) % 130;
131+
int y2 = rand() % 130;
152132
sprintf(temp, "<line x1=\"%d\" y1=\"%d\" x2=\"%d\" y2=\"%d\" stroke-width=\"2\" />\n", x, 140 - y, x + 10, 140 - y2);
153133
out += temp;
154134
y = y2;
155135
}
136+
156137
out += F("</g>\n</svg>\n");
157138

158-
server.send(200, F("image/svg+xml"), out);
159-
}
139+
WS_LOGDEBUG1(F("String Len = "), out.length());
160140

161-
#endif
141+
if (out.length() > previousStrLen)
142+
{
143+
WS_LOGERROR3(F("String Len > "), previousStrLen, F(", extend to"), out.length() + 48);
144+
145+
previousStrLen = out.length() + 48;
146+
147+
out.reserve(previousStrLen);
148+
}
149+
else
150+
{
151+
server.send(200, "image/svg+xml", out);
152+
}
153+
}
162154

163155
void setup()
164156
{

examples/AdvancedWebServer/defines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define DEBUG_WIFI_WEBSERVER_PORT Serial
1616

1717
// Debug Level from 0 to 4
18-
#define _WIFI_LOGLEVEL_ 1
18+
#define _WIFI_LOGLEVEL_ 2
1919
#define _WIFININA_LOGLEVEL_ 3
2020

2121
#if ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )

examples/HTTPClient/SimpleWebSocket/SimpleWebSocket.ino

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,9 @@ void setup()
116116

117117
void loop()
118118
{
119-
Serial.println("starting WebSocket client");
119+
static String data = " => Hello from SimpleWebSocket on " + String(BOARD_NAME) + ", millis = ";
120+
121+
Serial.println("Starting WebSocket client");
120122

121123
wsClient.begin();
122124

@@ -128,8 +130,10 @@ void loop()
128130
// send a hello #
129131
wsClient.beginMessage(TYPE_TEXT);
130132
wsClient.print(count);
131-
String data = " => Hello from SimpleWebSocket on " + String(BOARD_NAME) + ", millis = " + String(millis());
133+
132134
wsClient.print(data);
135+
wsClient.print(millis());
136+
133137
wsClient.endMessage();
134138

135139
// increment count for next message

examples/HTTPClient/SimpleWebSocket/defines.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
#define DEBUG_WIFI_WEBSERVER_PORT Serial
1616

1717
// Debug Level from 0 to 4
18-
#define _WIFI_LOGLEVEL_ 4
18+
#define _WIFI_LOGLEVEL_ 3
1919
#define _WIFININA_LOGLEVEL_ 3
2020

2121
#if ( defined(ARDUINO_PORTENTA_H7_M7) || defined(ARDUINO_PORTENTA_H7_M4) )

examples/HelloServer/HelloServer.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const int led = 13;
2020

2121
void handleRoot()
2222
{
23-
#define BUFFER_SIZE 400
23+
#define BUFFER_SIZE 512
2424

2525
digitalWrite(led, 1);
2626
char temp[BUFFER_SIZE];

examples/HelloServer2/HelloServer2.ino

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const int led = 13;
2020

2121
void handleRoot()
2222
{
23-
#define BUFFER_SIZE 400
23+
#define BUFFER_SIZE 512
2424

2525
digitalWrite(led, 1);
2626
char temp[BUFFER_SIZE];

examples/HttpBasicAuth/HttpBasicAuth.ino

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,10 @@ void setup()
9595
Serial.print(WiFi.localIP());
9696
Serial.println(F("/ in your browser to see it working"));
9797

98-
Serial.println(String("To login, use Username = ") + www_username + ", Password = " + www_password );
98+
Serial.print(F("To login, use Username = "));
99+
Serial.print(www_username);
100+
Serial.print(F(", Password = "));
101+
Serial.println(www_password);
99102
}
100103

101104
void loop()

examples/MQTTClient_Auth/MQTTClient_Auth.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ PubSubClient client(mqttServer, 1883, callback, wifiClient);
5858

5959
void reconnect()
6060
{
61+
static String data = "Hello from MQTTClient_Auth on " + String(BOARD_NAME);
62+
6163
// Loop until we're reconnected
6264
while (!client.connected())
6365
{
@@ -70,8 +72,6 @@ void reconnect()
7072
Serial.println("...connected");
7173

7274
// Once connected, publish an announcement...
73-
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
74-
7575
client.publish(TOPIC, data.c_str());
7676

7777
//Serial.println("Published connection message successfully!");
@@ -193,7 +193,9 @@ void loop()
193193
Serial.println("Message failed to send.");
194194
}
195195

196-
Serial.print("Message Send : " + String(TOPIC) + " => ");
196+
Serial.print("Message Send : ");
197+
Serial.print(TOPIC);
198+
Serial.print(" => ");
197199
Serial.println(data);
198200
}
199201

examples/MQTTClient_Basic/MQTTClient_Basic.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,8 @@ PubSubClient client(mqttServer, 1883, callback, wifiClient);
5858

5959
void reconnect()
6060
{
61+
static String data = "Hello from MQTTClient_Basic on " + String(BOARD_NAME);
62+
6163
// Loop until we're reconnected
6264
while (!client.connected())
6365
{
@@ -70,8 +72,6 @@ void reconnect()
7072
Serial.println("...connected");
7173

7274
// Once connected, publish an announcement...
73-
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
74-
7575
client.publish(TOPIC, data.c_str());
7676

7777
//Serial.println("Published connection message successfully!");
@@ -195,7 +195,9 @@ void loop()
195195
Serial.println("Message failed to send.");
196196
}
197197

198-
Serial.print("Message Send : " + String(TOPIC) + " => ");
198+
Serial.print("Message Send : ");
199+
Serial.print(TOPIC);
200+
Serial.print(" => ");
199201
Serial.println(data);
200202
}
201203

examples/MQTT_ThingStream/MQTT_ThingStream.ino

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ void mqtt_receive_callback(char* topic, byte* payload, unsigned int length)
9696

9797
void reconnect()
9898
{
99+
static String data = "Hello from MQTTClient_ThingStream on " + String(BOARD_NAME);
100+
99101
// Loop until we're reconnected
100102
while (!client.connected())
101103
{
@@ -115,8 +117,6 @@ void reconnect()
115117
Serial.println("...connected");
116118

117119
// Once connected, publish an announcement...
118-
String data = "Hello from MQTTClient_SSL on " + String(BOARD_NAME);
119-
120120
client.publish(topic.c_str(), data.c_str());
121121

122122
Serial.println("Published connection message successfully!");
@@ -241,7 +241,9 @@ void loop()
241241
Serial.println("Message failed to send.");
242242
}
243243

244-
Serial.print("MQTT Message Send : " + topic + " => ");
244+
Serial.print("MQTT Message Send : ");
245+
Serial.print(topic);
246+
Serial.print(" => ");
245247
Serial.println(data);
246248
}
247249

0 commit comments

Comments
 (0)