I have a program that sniffs nearby networks and place it in a json array. But I need to print the rssi values to meters based on a set of conditions.
This is the program that im working on
#include <ArduinoJson.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// #define MQTT_MAX_PACKET_SIZE 2048
const char* ssid = "ARSARO";
const char* password = "arsaro_sti";
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("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(10000);
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();
String myOutput; serializeJson(doc, myOutput);
char myOutput_char[200];
myOutput.toCharArray(myOutput_char, 200);
// Serial.println(myOutput);
client.publish("rssi/test",myOutput_char);
delay(1000);
}
These are the conditions that I want the RSSI value to go through before entering the array.
if (RSSI > -40) {
meters = 1;
} else if (RSSI <= -41 & RSSI >= -51) {
meters = 1.5;
} else if (RSSI <= -52 & RSSI >= -62) {
meters = 2;
} else if (RSSI <= -63 & RSSI >= -73) {
meters = 2.5;
} else if (RSSI <= -74 & RSSI >= -84) {
meters = 3;
} else if (RSSI <= -75 & RSSI >= -85) {
meters = 3.5;
} else if (RSSI <= -86 & RSSI >= -100) {
meters = 4;
} else {
meters = 5;
}
I want to use the said conditions on the scanned RSSI values before putting it inside the array so when I send it via mqtt, it will show the range via meters. Does that make sense?