ESP8266 IR Sensor Counter with Wi-Fi Logging

Hacking Truth
0

 





ESP8266 IR Sensor Counter with Wi-Fi Logging, MAX7219 Display & Data History Graphs (Complete IoT Project Guide)


🔎 Introduction


IoT projects using ESP8266 are one of the best ways to learn embedded systems, networking, and real-time monitoring. In this project, we build a smart object counter system using an IR Sensor (Flying Fish v1.1), ESP8266 microcontroller, and MAX7219 LED display.

The system connects to home Wi-Fi (Router Mode) and logs sensor activity to create data history and graphical analytics.


This project is useful for:


  • Visitor counting systems
  • Production line monitoring
  • Smart attendance tracking
  • Inventory automation
  • Entry/Exit monitoring systems


⚙️ Components Used


# ESP8266 (NodeMCU / ESP-12) -  The ESP8266 is a low-cost, 32-bit WiFi-enabled microcontroller (typically 80-160MHz) widely used for IoT projects, featuring built-in TCP/IP networking, 4MB flash, and GPIO pins. Popular development boards, such as the NodeMCU CP2102 








# IR Sensor v1.1 (Flying Fish) — a compact infrared obstacle detection module with IR emitter/receiver, adjustable sensitivity potentiometer, and digital output pins for microcontroller-based object detection projects.












# MAX7219 LED Display Module - MAX7219 4-in-1 Display Dot Matrix Module is an integrated serial input/output common-cathode display driver, it connects the microprocessor 7-segment digital LED display with 8 digits, you can also connect a bar graph display or 64 independent









# 10K Resistor -  A 10K resistor is mainly used for signal stability and protection in electronics. In your ESP8266 / sensor projects, common uses are:


✅ 1. Pull-up or Pull-down resistor

Keeps GPIO pins in a stable HIGH or LOW state
Example: IR sensor input / switch input stabilization


✅ 2. Button / Toggle switch circuits

Prevents floating input pins
Ensures reliable ON/OFF detection


✅ 3. Voltage divider

Reduce voltage for safe ESP8266 input (3.3V logic)













# Push Buttons - Ensures reliable ON/OFF detection










# Breadboard – Used to connect components without soldering. Helpful for quick prototyping.







# Jumper Wires – Used to make all the electrical connections between Arduino, sensors, and motor.









Wi-Fi Router (Home Network)




👉 Note: 3-Digit 7-Segment Display is optional because MAX7219 handles digital output more efficiently.





Why Use IR Sensor Instead of MH Flying Fish Sensors?

Currently, IR Sensor is preferred because:





👉 Use MH sensors later for:


  • Gas detection
  • Flame detection
  • Sound detection
  • Environmental monitoring



🔌 Hardware Connections


Sensor NodeMCU 8266 ESP


  • VCC -> 3V3
  • GND -> GND
  • D0  -> D2


switch 


  • OFF -> GND
  • ON(I) -> D1


MAX7219 NodeMU 8266 ESP


  • VCC -> VIN
  • GND -> GND
  • DIN -> D7
  • CS -> D8
  • CLK -> D5




This Exact Setup:




✅ Correct Board Configuration (ESP8266 NodeMCU)

1️⃣ Select Correct Board
Tools → Board → NodeMCU 1.0 (ESP-12E Module)

2️⃣ Select Correct COM Port
Tools → Port → COMx (where ESP8266 is connected)


Check in Device Manager:

Ports (COM & LPT)
 → USB-SERIAL CH340 / CP2102

3️⃣ Correct Upload Settings (Very Important)
Tools → Upload Speed → 115200
Tools → Flash Size → 4MB (FS:2MB OTA:~1019KB)
Tools → CPU Frequency → 80 MHz
Tools → Debug Port → Disabled
Tools → Debug Level → None
Tools → Reset Method → nodemcu




Code 



#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#include <SPI.h>

#define HARDWARE_TYPE MD_MAX72XX::FC16_HW
#define MAX_DEVICES 4

#define DIN_PIN D7
#define CLK_PIN D5
#define CS_PIN  D8

#define IR_PIN     D2
#define SWITCH_PIN D1

MD_Parola display = MD_Parola(HARDWARE_TYPE, DIN_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
ESP8266WebServer server(80);

volatile int count = 0;
bool lastIRState = HIGH;

/* ---------- Wi-Fi credentials ---------- */
const char* ssid = "Airtel_ajay_4747";
const char* password = "air83128";

/* ---------- Web page ---------- */
void handleRoot() {
  String page =
    "<!DOCTYPE html><html><head>"
    "<meta name='viewport' content='width=device-width, initial-scale=1'>"
    "<meta http-equiv='refresh' content='2'>"
    "<title>ESP8266 Counter</title></head><body>"
    "<h2>Live Counter - Hackingtruth.in | .org </h2>"
    "<h1>" + String(count) + "</h1>"
    "<a href='/reset'><button>Reset</button></a>"
    "</body></html>";
  server.send(200, "text/html", page);
}

void handleReset() {
  count = 0;
  server.sendHeader("Location", "/");
  server.send(303);
}

void setup() {
  pinMode(IR_PIN, INPUT);
  pinMode(SWITCH_PIN, INPUT_PULLUP);

  display.begin();
  display.setIntensity(5);
  display.displayClear();

  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
  }

  server.on("/", handleRoot);
  server.on("/reset", handleReset);
  server.begin();
}

void loop() {
  server.handleClient();

  bool currentIRState = digitalRead(IR_PIN);

  if (lastIRState == HIGH && currentIRState == LOW) {
    count++;
    if (count > 999) count = 0;
    delay(300);
  }
  lastIRState = currentIRState;

  if (digitalRead(SWITCH_PIN) == LOW) {
    count = 0;
  }

  char buf[4];
  sprintf(buf, "%03d", count);
  display.displayText(buf, PA_CENTER, 0, 0, PA_PRINT, PA_NO_EFFECT);
  display.displayAnimate();
}








NOTE - If you want you can also live counting in your phone with the help of IP address 

You need the ESP8266’s local IP address — this is assigned by your Wi-Fi router when the NodeMCU connects in Router (STA) mode.


Here are the exact methods to get it 👇


✅ Method 1 — Serial Monitor (Best & Fastest)

Add this line in your setup() after Wi-Fi connects:








Serial.begin(115200);

WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
  delay(500);
  Serial.print(".");
}

Serial.println("");
Serial.print("ESP8266 IP Address: ");
Serial.println(WiFi.localIP());





Steps

Upload code
Open Tools → Serial Monitor
Set baud rate 115200


You will see:

ESP8266 IP Address: 192.168.1.45


👉 Open on phone browser:

http://192.168.1.45



Otherwise you can in computet or system that connected to the same wifi network and type in cmd ipconfig



⚠️ Common Upload Error Fix (NodeMCU)

If upload fails:

  • Hold FLASH button
  • Click Upload
  • Release FLASH when uploading starts




How to Check:



  • # Look at the top-right corner of the Arduino IDE.
  • # If the Serial Monitor is open, you’ll see a window titled “Serial Monitor” (it looks like a terminal).
  • # Simply close that window by clicking the X at the top-right of it.



Shortcut:


You can also close it by clicking:
Tools > Serial Monitor (clicking it toggles it closed if it's open)



Why it matters:



The Serial Monitor uses the COM port, so if it’s open, uploading code will fail with errors like the one you got.
Try closing it and then upload your code again.









Lets begin the test - For Live Visualization - CLICK HERE 








Disclaimer

All tutorials are for informational and educational purposes only and have been made using our own routers, servers, websites and other vulnerable free resources. we do not contain any illegal activity. We believe that ethical hacking, information security and cyber security should be familiar subjects to anyone using digital information and computers. Hacking Truth is against misuse of the information and we strongly suggest against it. Please regard the word hacking as ethical hacking or penetration testing every time this word is used. We do not promote, encourage, support or excite any illegal activity or hacking.





Post a Comment

0 Comments
* Please Don't Spam Here. All the Comments are Reviewed by Admin.
Post a Comment (0)
Our website uses cookies to enhance your experience. Learn More
Accept !