Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By treii28
#51060 I was playing around with the wifi on a nodemcu and just learning concepts and it struck me that some might find this code sample useful. Essentially I hooked up a passive piezo buzzer between pin 2 and ground and use the built-in led on the dev board. It will scan for a given SSID every 5 seconds and beep if it detects it. It's useful to determine the range of your home wifi.

With just a few mods you could probably also set it up to look for say, open wifi nodes and probably even modify it to give you a number of beeps based on the strongest signal or something similar.

Code: Select all/*
    This sketch demonstrates how to scan WiFi networks.
    The API is almost the same as with the WiFi Shield library,
    the most obvious difference being the different file you need to include:
*/
#include <ESP8266WiFi.h>

#define PIEZO   5
#define LED     16        // Led in NodeMCU at pin GPIO16 (D0).

String target = "NETGEAR60";

void setup() {
  Serial.begin(115200);
  pinMode(LED, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
  pinMode(PIEZO, OUTPUT);

  Serial.println("Setup done");
}

void loop() {
  boolean status = scanNetworks();
  if (status == true) {
    beep();
  }

  // Wait a bit before scanning again
  delay(5000);
}

boolean scanNetworks() {
  Serial.println("scan start");
  boolean found = false;
  // 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");
    for (int i = 0; i < n; ++i)
    {
      if (target == WiFi.SSID(i)) {
        found = true;
        Serial.println("Target 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) ? " " : "*");
      }
    }
  }
  Serial.println("");
  return found;
}

void blinkLed(int td) {
  digitalWrite(LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
  delay(td);                // Wait for a td milliseconds
  digitalWrite(LED, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(10);
}

void beep() {
  int pitch = 150;
  int duration = 250;
  digitalWrite(LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
  for (int i = 0; i < duration; i++) {
    digitalWrite(PIEZO, HIGH);
    delayMicroseconds(pitch);
    digitalWrite(PIEZO, LOW);
    delayMicroseconds(pitch);
  }
  digitalWrite(LED, HIGH);  // Turn the LED off by making the voltage HIGH
}
User avatar
By treii28
#51096 Oh, I also left my 'blinkLed(i) method in there which you can use to make it silent rather than beeping. Just use a value like 250 or so to emit a blink when it detects and replaced beep() with blinkLed(250); or something similar.
User avatar
By mrburnette
#51103
treii28 wrote:<...>
With just a few mods you could probably also set it up to look for say, open wifi nodes and probably even modify it to give you a number of beeps based on the strongest signal or something similar.
...


Yep ... for those wanting existing code for play, here is my Open WiFi OLED detector

I like the idea of the piezo for a particular SSID ...

Ray