Hi, I have an ESP8266 program that prints the networks scanned and places them in a JSON Array. I'm trying to send it to via mqtt and node-red. I've also converted it into a String in arduino but when I connect a text node it prints the array with braces and quotations. How do I program node-red into printing only the SSID and RSSI values, and a new line for every network found.
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Tenda_FE2038";
const char* password = "huertas032793";
const char* mqtt_server = "192.168.1.2";
WiFiClient espClient;
PubSubClient client(espClient);
void callback(String topic, byte* message, unsigned int length) {
// Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
// Subscribe or resubscribe to a topic
// You can subscribe to more topics (to control more LEDs in this example)
client.subscribe("rssi/test");
client.subscribe("name/test");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
DynamicJsonDocument getAccessPointsAsDynamicJson() {
DynamicJsonDocument doc(2560);
JsonArray array = doc.createNestedArray("Networks: ");
Serial.println("Start scanning");
int n = WiFi.scanNetworks();
Serial.print(n);
Serial.println(" networks found");
for (int i = 0; i < n; ++i)
{ JsonObject
nested = array.createNestedObject();
nested["ssid"] = WiFi.SSID(i);
// nested["bssid"] = WiFi.BSSID(i);
nested["rssi"] = WiFi.RSSI(i);
// nested["channel"] = WiFi.channel(i);
}
return doc;
}
void setSerial()
{
Serial.begin(115200);
while (!Serial) continue;
}
void setup()
{
setSerial();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
// Print local IP address and start web server
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop()
{
if (!client.connected()) {
reconnect();
}
if(!client.loop())
client.connect("ESP8266Client");
DynamicJsonDocument doc = getAccessPointsAsDynamicJson();
const char* first_found_ssid = doc["ap"][0]["ssid"];
const int first_found_rssi = doc["ap"][0]["rssi"];
const char* second_found_ssid = doc["ap"][1]["ssid"];
//Serial.println(first_found_ssid);
//Serial.println(first_found_rssi);
//Serial.println(second_found_ssid);
delay(1000);
String myOutput; serializeJson(doc, myOutput);
char myOutput_char[200];
myOutput.toCharArray(myOutput_char, 200);
//Serial.println(myOutput);
client.publish("name/test",myOutput_char);
}
[{"id":"889428da.48e2f8","type":"tab","label":"Flow 2","disabled":false,"info":""},{"id":"6b64ab7b.cd1ac4","type":"json","z":"889428da.48e2f8","name":"","property":"payload","action":"obj","pretty":true,"x":530,"y":300,"wires":[["301f0300.9c21fe"]]},{"id":"249daef3.964e22","type":"mqtt in","z":"889428da.48e2f8","name":"","topic":"name/test","qos":"0","datatype":"auto","broker":"6669e693.9f8508","x":360,"y":360,"wires":[["aaf0037d.3a5a7","6b64ab7b.cd1ac4"]]},{"id":"301f0300.9c21fe","type":"ui_text","z":"889428da.48e2f8","group":"13637635.f64a3a","order":0,"width":0,"height":0,"name":"","label":"text","format":"{{msg.payload}}","layout":"col-center","x":690,"y":260,"wires":[]},{"id":"aaf0037d.3a5a7","type":"debug","z":"889428da.48e2f8","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","x":750,"y":420,"wires":[]},{"id":"6669e693.9f8508","type":"mqtt-broker","z":"","name":"mosquitto","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":false,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""},{"id":"13637635.f64a3a","type":"ui_group","z":"","name":"rssi","tab":"a9b73d04.c94c9","disp":true,"width":"6","collapse":false},{"id":"a9b73d04.c94c9","type":"ui_tab","z":"","name":"Home","icon":"mic","disabled":false,"hidden":false}]
this is the flow im working on
this is what the output looks like in text node