-
Notifications
You must be signed in to change notification settings - Fork 1
/
ESP8266_Networkscanner.ino
131 lines (97 loc) · 3.21 KB
/
ESP8266_Networkscanner.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "Karthik";
const char* password = "8778813014";
const char* mqttServer = "192.168.43.235"; //IP address of the mqtt broker
const int mqttPort = 1883; //port number of the broker
const char* mqttUser = "pi";
const char* mqttPassword = "spider19";
WiFiClient espClient;
PubSubClient client(espClient);
void setup()
{
int h = 0;
Serial.begin(115200);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.println("Connecting to WiFi..");
}
Serial.println("Connected to the WiFi network");
client.setServer(mqttServer, mqttPort);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("Connecting to MQTT...");
if (client.connect("ESP8266Client1", mqttUser, mqttPassword)) {
Serial.println("connected");
} else {
Serial.print("failed with state ");
Serial.print(client.state());
delay(2000);
}
}
client.subscribe("test1");
}
void callback(char* topic, byte* payload, unsigned int length) {
String a[20];
Serial.print("Message arrived in topic: ");
Serial.println(topic);
Serial.print("Message:");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
Serial.println("-----------------------");
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");
int k = 0;
int z=-1;
// 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);
int i=0;
for (int j=0;j<n;j++)
{
for (int k=0;k<i;k++)
{
if ( WiFi.SSID(j) == a[k])
{
z=1;
break;
}
}
if(z!=1)
{
String h = ","+WiFi.SSID(j) + "," + WiFi.RSSI(j)+",";
if(WiFi.SSID(j).toInt()<10 && WiFi.SSID(j).toInt()>0)
{a[i++]=WiFi.SSID(j);
char hh1[30];
Serial.println(h);
h.toCharArray(hh1, 30);
//Serial.println(hh1);
client.publish("lane1", hh1);
z=0;
}
}
}
}
}
void loop()
{
client.loop();
}