Notifications
Clear all

[Solved] How To Parse Text string into smaller, equal size, strings?  

  RSS

billd01
(@billd01)
Active Member
Joined: 1 month ago
Posts: 7
2020-07-10 10:05 am  

Hi, I have a project where I use a Wemos D1R2 ESP8266 board to run WiFiScan.ino, it creates this output to serial monitor:

4 networks found
1: GoTigers!_2GEXT_EX6100 (-46)*
2: GoTigers!_2GEXT (-69)*
3: SNS/AhEDASAAALjpN6h6LgA= (-83)
4: Biden2020 (-72)*

I have an MCUFriend 3.2" TFT shield that works well on Arduino Mega . . . I want to display the scan results on the TFT. The excellent I2C tutorial here https://dronebotworkshop.com/i2c-arduino-arduino/ has the Wemos (with some minor modification to the code) communication reliably with the Arduino Mega . . . but . . . any ANSWER_SIZE string greater than about 25 characters results in garbled output on the receive device (I'm using the Mega with the TFT as the MASTER to request data from the WEMOS slave).

It could be timing, logic levels or any number of issues, but I know it works perfect with ANSWER_SIZE limited to 16. So If I could 'simply' break the WiFiScan output into equal 16 -char strings it would work.

I've found dozens of examples of how to split strings by looking for a delimiter character (\n, ".", ":" etc), but can't find anything to simply count to 16 characters, save that as a string, then count another 16, save that etc . . .

Thoughts, help, guidance much appreciated.

thnx

billd


Quote
triform
(@triform)
Reputable Member
Joined: 1 year ago
Posts: 325
2020-07-10 1:58 pm  

Hi @billd01,

I assume you are using the string class.  See the substring method in the string class.

Also, you may want to look at pullup resistors on the I2C bus if you don't already have them, a floating I2C line can cause issues.  If you have long wires connecting the two devices, try to shorten them.  Noise from induction should be lessened (somewhat).


billd01 liked
ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 472
2020-07-10 11:28 pm  

@billd01

I assume this is the code you refer to?

 

/*
    This sketch demonstrates how to scan WiFi networks.
    The API is almost the same as with the WiFi Shield library,
    the most obvious difference being the different file you need to include:
*/
#include "ESP8266WiFi.h"

void setup() {
  Serial.begin(115200);

  // Set WiFi to station mode and disconnect from an AP if it was previously connected
  WiFi.mode(WIFI_STA);
  WiFi.disconnect();
  delay(100);

  Serial.println("Setup done");
}

void loop() {
  Serial.println("scan start");

  // WiFi.scanNetworks will return the number of networks found
  int n = WiFi.scanNetworks();
  Serial.println("scan done");
  if (n == 0) {
    Serial.println("no networks found");
  } else {
    Serial.print(n);
    Serial.println(" networks found");
    for (int i = 0; i < n; ++i) {
      // Print SSID and RSSI for each network found
      Serial.print(i + 1);
      Serial.print(": ");
      Serial.print(WiFi.SSID(i));
      Serial.print(" (");
      Serial.print(WiFi.RSSI(i));
      Serial.print(")");
      Serial.println((WiFi.encryptionType(i) == ENC_TYPE_NONE) ? " " : "*");
      delay(10);
    }
  }
  Serial.println("");

  // Wait a bit before scanning again
  delay(5000);
}








This post was modified 1 month ago by robotBuilder

ReplyQuote
robotBuilder
(@robotbuilder)
Reputable Member
Joined: 1 year ago
Posts: 472
2020-07-10 11:31 pm  

I don't have any of the hardware you are using so I can't work out and test any software solutions myself.

 

  
//based on this source  https://www.geeksforgeeks.org/divide-a-string-in-n-equal-parts/ 

#include<string.h> 

void divideString(char str[], int n) 
{ 
      
    int str_size = strlen(str); 
    int i; 
    int part_size; 
  
    // Check if string can be divided in 
    // n equal parts 
    if (str_size % n != 0) 
    { 
        Serial.println("Invalid Input: String size"); 
        Serial.println(" is not divisible by n"); 
        return; 
    } 
  
    // Calculate the size of parts to 
    // find the division points 
    part_size = str_size / n; 
    for (i = 0; i< str_size; i++) 
    { 
        if (i % part_size == 0) 
            Serial.println();  
        Serial.println(str[i]); 
    } 
} 

void setup() {
  Serial.begin(115200);

}

void loop() {
  
    // length od string is 28 
    char str[] = "a_simple_divide_string_quest"; 
  
    // Print 4 equal parts of the string  
    divideString(str, 4); 
    
  delay(5000);
}

 

 

This post was modified 1 month ago 4 times by robotBuilder

billd01 liked
ReplyQuote
billd01
(@billd01)
Active Member
Joined: 1 month ago
Posts: 7
2020-07-11 4:26 am  

@robotbuilder Yes, that's it, works perfectly.

 

Thnx All for the suggestions, since the original post I did some testing and determined that the maximum ANSWERSIZE is 32 characters, I2C comms between the WEMOS slave and MEGA master is rock solid up to 32 characters, and fails at 33 or above.

image

This suggests 5 available data bits in the 8-bit I2C byte structure (perhaps start, stop & parity for the other 3?).

32 characters is ample to display SSID, RSSI info my my project, which saves me having to go down the rabbit hole of parseing strings . . .

Thnx again for the feedback

cul

billd 


ReplyQuote