Host Probing with NodeMCU

1 minute read

Not so long ago, I got a esp8266. This device is awesome for prototyping and developing IoT gadgets.

Since some devices might go on and off from my network and I wanted to start with something simple to find what devices are connected.

This dead simple host scanner does the job pretty well. The idea behind is to loop through a local network address space (/24) while checking what hosts are responding for ICMP ping. We just need the ESP8266WiFi and ESP8266Ping libraries for that.

//#include <SPI.h>
#include <ESP8266WiFi.h>
#include <ESP8266Ping.h>

const char* ssid = "**";
const char* password = "**";
int i = 1;

 
void setup() {
  Serial.begin(115200);
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
}


void loop() {
  Serial.print("Pinging ");
  Serial.print(": ");
  IPAddress ip (192, 168, 178, i); // The remote ip to ping
  Serial.print(ip);
  bool pingResult = Ping.ping(ip, 2);
  if (pingResult) {
    Serial.print("SUCCESS! RTT = ");
    Serial.print(pingResult);
    Serial.println(" ms");
  } else {
    Serial.print("FAILED! Error code: ");
    Serial.println(pingResult);
  }
  i = i + 1;
}

It will go something like

19:27:12.040 -> Pinging : 192.168.178.1SUCCESS! RTT = 1 ms
19:27:13.089 -> Pinging : 192.168.178.2FAILED! Error code: 0
19:27:15.128 -> Pinging : 192.168.178.3FAILED! Error code: 0
19:27:17.192 -> Pinging : 192.168.178.4FAILED! Error code: 0
19:27:19.196 -> Pinging : 192.168.178.5FAILED! Error code: 0