Gathering results and placing in an array

Hi, im working on a project that scans the wifi networks nearby with rssi values and I need to print it after scanning. Im using an esp8266 nodemcu. I'm also using node-red and printing it via mqtt using mosquitto. My problem is that the SSID, RSSI values are printing one by one. I need my loop to get all available networks before printing it. Im thinking of placing it into an array and printing that but my programming skills are kinda limited.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

char dbm[10] = "dBm: ";

// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "Tenda_FE2038";
const char* password = "huertas032793";

// Change the variable to your Raspberry Pi IP address, so it connects to your MQTT broker
const char* mqtt_server = "";

String sssid;
uint8_t encryptionType;
int32_t RSSI;
uint8_t* BSSID;
int32_t channel;
bool isHidden; 
uint8_t prevRssi;

// Initializes the espClient. You should change the espClient name if you have multiple ESPs running in your home automation system
WiFiClient espClient;
PubSubClient client(espClient);
// Timers auxiliar variables
long now = millis();
long lastMeasure = 0;

// Don't change the function below. This functions connects your ESP8266 to your router
 /*void setup_wifi() {
  // We start by connecting to a WiFi network
  Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  Serial.print("WiFi connected - ESP IP address: ");
// This functions is executed when some device publishes a message to a topic that your ESP8266 is subscribed to
// Change the function below to add logic to your program, so when a device publishes a message to a topic that 
// your ESP8266 is subscribed you can actually do something
void callback(String topic, byte* message, unsigned int length) {
 // Serial.print("Message arrived on topic: ");
  Serial.print(". Message: ");
  String messageTemp;
  for (int i = 0; i < length; i++) {
    messageTemp += (char)message[i];


// This functions reconnects your ESP8266 to your MQTT broker
// Change the function below if you want to subscribe to more topics with your ESP8266 
void reconnect() {
  // Loop until we're reconnected
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    // Attempt to connect
    if (client.connect("ESP8266Client")) {
      // Subscribe or resubscribe to a topic
      // You can subscribe to more topics (to control more LEDs in this example)
    } else {
      Serial.print("failed, rc=");
      Serial.println(" try again in 5 seconds");
      // Wait 5 seconds before retrying

// The setup function sets your ESP GPIOs to Outputs, starts the serial communication at a baud rate of 115200
// Sets your mqtt broker and sets the callback function
// The callback function is what receives messages and actually controls the LEDs
void setup() {

 Serial.print("Connecting to ");
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
  // Print local IP address and start web server
  Serial.println("WiFi connected.");
  Serial.println("IP address: ");

  client.setServer(mqtt_server, 1883);


// For this project, you don't need to change anything in the loop function. Basically it ensures that you ESP is connected to your broker
void loop() {

   if (!client.connected()) {

byte available_networks = WiFi.scanNetworks();
  int netnum = 0;
  prevRssi = 0;
  String networkarray[50];
// first lets find the SSID of the network you are looking for
// by iterating through all of the avaialble networks
// since in an enterprise there may be more than one BSSID for the SSID
// Lets find the stringest one

  for (int network = 0; network < available_networks; network++) 
     //client.print(network + 1);
     Serial.print(network + 1);
      //client.print(": ");
      Serial.print(": ");
      netnum = network;
      prevRssi = (uint8_t)WiFi.RSSI(network);

    int net1 = network+ 1;
  //  String networkarray[60] = {WiFi.SSID(network)};

      WiFi.getNetworkInfo(netnum, sssid, encryptionType, RSSI, BSSID, channel, isHidden);
  //client.print("Signal strength: ");
  Serial.print("Signal strength: ");
  float bars; 

   long rssi = WiFi.RSSI();
   String ssidstring = WiFi.SSID(network);
   int ssidchar_len = ssidstring.length() + 1;
   char ssidchar[ssidchar_len];
   ssidstring.toCharArray(ssidchar, ssidchar_len);
  if (RSSI > -40) { 
    bars = 1;
  } else if (RSSI <= -41 & RSSI >= -51) {
    bars = 1.5;
  } else if (RSSI <= -52 & RSSI >= -62) {
    bars = 2;
  } else if (RSSI <= -63 & RSSI >= -73) {
    bars = 2.5;
  } else if (RSSI <= -74 & RSSI >= -84) {
    bars = 3;
  } else if (RSSI <= -75 & RSSI >= -85) {
    bars = 3.5;
  } else if (RSSI <= -86 & RSSI >= -100) {
    bars = 4;
  } else {
    bars = 5;

  char rssichar[20];
  char netnum_buffer[20];
  char combined[30];
  char barschar[5];

itoa(bars, barschar, 10);
itoa(rssi, rssichar, 10);
itoa(net1, netnum_buffer, 10);
//  strcat(combined, dbm);
//  strcat(combined, rssichar);
  client.publish("rssi/test", rssichar);
  // client.publish("name/test", netnum_buffer);

Here is a picture of my flow

Do I edit the program itself or can I program node-red into doing what I want? Any help will be appreciated.

Look at the second example in the multiple message section of this page in the docs

1 Like

Both is possible, depending on what you feel you can master best way. I personally would fix it in the esp code so that it sends just one message with everything already collected and sorted the way I want

Do you have an idea on how to do it on the esp code? Am I right it my assumption that I can put all the scanned networks in an array and then send it to node-red?

Yes, well, for NR it is perfect to receive a javascript object, that makes the rest very easy to handle in NR

So I would figure out how you could create such a structure in C++ ??? (an object created from a suitable class definition)

I would scan and either send all properties per SSID or put everything together for all SSID's found and then send it as an MQTT message (string)

In NR I receive the string and use a JSON node to convert it into a javascript object. From here you will be able to pick any properties (keys and values) and do the rest