Easy example to check Wifi signal strength

Hello all, I wanted to share an easy sketch I did to check wether Arduino has Wifi connection (as a client to a router) or not, and do an action when wifi signal is lost. In this example Led13 will blink if wifi is ok and will turn off if wifi is lost. I tested it turning on and off my router while the sketch was running and works fine.

I modified pretty-wifi-info.lua to output only the signal’s strength.

I needed this example because of my project, when wifi is lost, the product enters on manual mode state, and when wifi returns it just returns to its common actions.

Here’s the Arduino Yun code:

#include <Process.h>

char c2[4] = {'1','2','3','4'};

long intervalo1 = 5000;
long intervalo2 = 500;

long tiempo1 = 0;
long tiempo2 = 0;

int status_wifi = 0;

void setup() {
  pinMode(13,OUTPUT);
  digitalWrite(13,HIGH);
  delay(60000);               wait for Linux to boot
  Bridge.begin();
  digitalWrite(13,LOW);

}

void loop() {
  
  if(millis() - tiempo1 > intervalo1){
    tiempo1 = millis();
    runProcess(); //get the signal's strength
  }
  
  switch (status_wifi){
    case 0:
            digitalWrite(13,LOW);      //led13 is off when wifi is lost
            break;
    case 1:
            if(millis() - tiempo2 > intervalo2){
              tiempo2 = millis();
              digitalWrite(13,!digitalRead(13));  //led13 blinks when wifi is ok
            }
            break;
  }

}

void runProcess() {
  
  Process p;        // Create a process and call it "p"
  p.runShellCommand("lua /root/martin/pretty-wifi-info.lua");  // use CLI commands just like 
                                                                      //inside linux console
  int i=0;
  while (p.available()>0) {
    char c = p.read();
    c2[i] = c;
    i=i+1;
  }
  String c3=c2;
  int a=c3.toInt();   //convert String to int

  if (a == 0){         //check if signal is 0%, singal strength goes from 0 to 100%
    status_wifi = 0;  //wifi signal is lost
  }
  else 
    status_wifi = 1; //wifi signal is ok
}

wifi-signal strength’s script:

pretty-wifi-info.lua

#!/usr/bin/lua

local function get_basic_net_info(network, iface, accumulator)
  local net = network:get_network(iface)
  local device = net and net:get_interface()

  if device then
    accumulator["uptime"] = net:uptime()
    accumulator["iface"] = device:name()
    accumulator["mac"] = device:mac()
    accumulator["rx_bytes"] = device:rx_bytes()
    accumulator["tx_bytes"] = device:tx_bytes()
    accumulator["ipaddrs"] = {}

    for _, ipaddr in ipairs(device:ipaddrs()) do
      accumulator.ipaddrs[#accumulator.ipaddrs + 1] = {
        addr = ipaddr:host():string(),
        netmask = ipaddr:mask():string()
      }
    end
  end
end

local function get_wifi_info(network, iface, accumulator)
  local net = network:get_wifinet(iface)

  if net then
    local dev = net:get_device()
    if dev then
      accumulator["mode"] = net:active_mode()
      accumulator["ssid"] = net:active_ssid()
      accumulator["encryption"] = net:active_encryption()
      accumulator["quality"] = net:signal_percent()
    end
  end
end

local function collect_wifi_info()
  local network = require"luci.model.network".init()
  local accumulator = {}
  get_basic_net_info(network, "lan", accumulator)
  get_wifi_info(network, "wlan0", accumulator)
  return accumulator
end

local info = collect_wifi_info()

--[[print("Current WiFi configuration")
if info.ssid then
 print("SSID: " .. info.ssid)
end
if info.mode then
  print("Mode: " .. info.mode)
end
--]]
if info.quality then
  print(info.quality)
end
--[[
if info.encryption then
  print("Encryption method: " .. info.encryption)
end
if info.iface then
  print("Interface name: " .. info.iface)
end
if info.uptime then
  print("Active for: " .. math.floor(info.uptime / 60) .. " minutes")
end
if info.ipaddrs > 0 then
  print(info.ipaddrs[1].addr)
end
if info.mac then
  print("MAC address: " .. info.mac)
end
if info.rx_bytes and info.tx_bytes then
  print("RX/TX: " .. math.floor(info.rx_bytes / 1024) .. "/" .. math.floor(info.tx_bytes / 1024) .. " KBs")
end
--]]