Skip to content

Instantly share code, notes, and snippets.

@jaretburkett
Last active June 30, 2025 13:03
Show Gist options
  • Save jaretburkett/b2779bd7bf19b0a8e025 to your computer and use it in GitHub Desktop.
Save jaretburkett/b2779bd7bf19b0a8e025 to your computer and use it in GitHub Desktop.
ESP8266 on Arduino using wifi signal strength to detect motion
#include <ESP8266WiFi.h>
// Connection Settings
const char* ssid = "YourSSID";
const char* password = "YourPasskey";
// show debug output data on serial
// shows rssi readings when true
bool debug = false;
// number of readings to average for a constant
#define bufferSize 100
// dbi change needed to trigger motion
int sensitivity = 2;
/*
* Motion Detected Function
*
* When Motion is detected, this function is called. Make it do anything you would like.
*
* Note: It will call this function in rapid succession until the averaging buffer catches up.
* So program accordingly.
*/
void motionDetected(){
// what to do if motion is detected
Serial.print("Motion detected at ");
Serial.print(millis());
Serial.println(" ms");
}
// to store buffer data
int RSSIarray[bufferSize +1]; // add 1 for terminating bit
// global vars
int lastRSSI = 0;
int avgRSSI = 0;
void setup() {
Serial.begin(115200);
// connect to station
while (WiFi.status() ==! WL_CONNECTED){
WiFi.begin(ssid,password);
Serial.println("Not Connected"); //debug
}
// wait for first RSSI value to be returned (don't comment out).
while (WiFi.RSSI() > 0){
delay(100);
}
// fill buffer array with readings
for(int i = 0; i <bufferSize; i++){
RSSIarray[i]=WiFi.RSSI();
}
}
void loop() {
while (WiFi.status() ==! WL_CONNECTED){
WiFi.begin(ssid,password);
Serial.println("Not Connected"); //debug
}
// wait for first RSSI value to be returned (don't comment out).
while (WiFi.RSSI() > 0){
delay(100);
}
// get current Reading
int currentRSSI = WiFi.RSSI();
// shift new value to buffer array
for(int i = 1; i < bufferSize; i++){
RSSIarray[i-1] = RSSIarray[i];
}
RSSIarray[bufferSize-1] = currentRSSI;
// avg out RSSI in buffer
int32_t RSSIsum = 0;
// get the sum of the buffer array
for(int i = 0; i< bufferSize; i++){
RSSIsum = RSSIsum + RSSIarray[i];
}
// average out the buffer array
avgRSSI = RSSIsum / bufferSize;
// output debug
if(debug){
Serial.print("Avg: ");
Serial.print(avgRSSI);
Serial.print(" Current Reading: ");
Serial.println(currentRSSI); // debug
}
// check if there was motion
if(avgRSSI >= currentRSSI + sensitivity || avgRSSI <= currentRSSI - sensitivity){
// call the motion detected function.
motionDetected();
}
// delay needed to slow things down. Works best with a small delay
delay(50);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment