Skip to content

Commit d606fce

Browse files
committed
Manual "String" parsing. ArduinoJSON is flaky in handeling data from Twiiter.
1 parent b313a70 commit d606fce

File tree

1 file changed

+55
-10
lines changed

1 file changed

+55
-10
lines changed

examples/esp8266/TwitterTweetSearchFSWiFiMgr/TwitterTweetSearchFSWiFiMgr.ino

+55-10
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,13 @@
1515

1616
//#define MAX7219DISPLAY // uncomment if using MAX7219-4-digit-display-for-ESP8266
1717
#ifdef MAX7219DISPLAY
18-
#include <SPI.h>
19-
#include <bitBangedSPI.h>
20-
#include <MAX7219_Dot_Matrix.h> // https://github.com/SensorsIot/MAX7219-4-digit-display-for-ESP8266
18+
#include <SPI.h>
19+
#include <bitBangedSPI.h>
20+
#include <MAX7219_Dot_Matrix.h> // https://github.com/SensorsIot/MAX7219-4-digit-display-for-ESP8266
21+
// VCC -> 5V, GND -> GND, DIN -> D7, CS -> D8 (configurable below), CLK -> D5
22+
const byte chips = 4; // Number of Display Chips
23+
MAX7219_Dot_Matrix display (chips, D8); // Chips / LOAD
24+
unsigned long MOVE_INTERVAL = 20; // (msec) increase to slow, decrease to fast
2125
#endif
2226

2327
bool resetsettings = false; // true to reset WiFiManager & delete FS files
@@ -34,12 +38,6 @@ unsigned long twi_update_interval = 20; // (seconds) minimum 5s (180 API calls
3438
static char const accesstoken[] = "041657084136508135-F3BE63U4Y6b346kj6bnkdlvnjbGsd3V";
3539
static char const accesstoken_sec[] = "bsekjH8YT3dCWDdsgsdHUgdBiosesDgv43rknU4YY56Tj";
3640
#endif
37-
#ifdef MAX7219DISPLAY
38-
// VCC -> 5V, GND -> GND, DIN -> D7, CS -> D8 (configurable below), CLK -> D5
39-
const byte chips = 4; // Number of Display Chips
40-
MAX7219_Dot_Matrix display (chips, D8); // Chips / LOAD
41-
unsigned long MOVE_INTERVAL = 20; // (msec) increase to slow, decrease to fast
42-
#endif
4341
#ifndef AutoAP_password
4442
#define AutoAP_password "password" // Dafault AP Password
4543
#endif
@@ -201,6 +199,53 @@ void extractJSON(String tmsg) {
201199
delete [] msg2;
202200
}
203201

202+
void extractTweetText(String tmsg) {
203+
unsigned int msglen = tmsg.length();
204+
205+
String seatchstr = ",\"text\":\"";
206+
unsigned int searchlen = seatchstr.length();
207+
int pos1 = -1, pos2 = -1;
208+
for(int i=0; i <= msglen - searchlen; i++) {
209+
if(tmsg.substring(i,searchlen+i) == seatchstr) {
210+
pos1 = i + searchlen;
211+
break;
212+
}
213+
}
214+
seatchstr = "\",\"";
215+
searchlen = seatchstr.length();
216+
for(int i=pos1; i <= msglen - searchlen; i++) {
217+
if(tmsg.substring(i,searchlen+i) == seatchstr) {
218+
pos2 = i;
219+
break;
220+
}
221+
}
222+
String text = tmsg.substring(pos1, pos2);
223+
224+
seatchstr = ",\"screen_name\":\"";
225+
searchlen = seatchstr.length();
226+
int pos3 = -1, pos4 = -1;
227+
for(int i=pos2; i <= msglen - searchlen; i++) {
228+
if(tmsg.substring(i,searchlen+i) == seatchstr) {
229+
pos3 = i + searchlen;
230+
break;
231+
}
232+
}
233+
seatchstr = "\",\"";
234+
searchlen = seatchstr.length();
235+
for(int i=pos3; i <= msglen - searchlen; i++) {
236+
if(tmsg.substring(i,searchlen+i) == seatchstr) {
237+
pos4 = i;
238+
break;
239+
}
240+
}
241+
String usert = "@" + tmsg.substring(pos3, pos4);
242+
243+
if (text.length() >0 ) {
244+
text = usert + " says " + text;
245+
search_msg = std::string(text.c_str(), text.length());
246+
}
247+
}
248+
204249
void updateDisplay(){
205250
char *msg = new char[search_msg.length() + 1];
206251
strcpy(msg, search_msg.c_str());
@@ -216,7 +261,7 @@ void updateDisplay(){
216261
#ifdef MAX7219DISPLAY
217262
display.sendString ("--------");
218263
#endif
219-
extractJSON(tcr.searchTwitter(search_str));
264+
extractTweetText(tcr.searchTwitter(search_str));
220265
DEBUG_PRINT("Search: ");
221266
DEBUG_PRINTLN(search_str.c_str());
222267
DEBUG_PRINT("MSG: ");

0 commit comments

Comments
 (0)