Ahmed Benlafqih

All projects

Simulating how disinformation spreads, on an ESP32

Academic project · 2025–2026

A 625-agent model of how people come to adhere to false information, running on an ESP32 and steered from a browser. In partnership with Campus Cyber.

Source code

In short

What it is
A simulation of 625 agents on a 25×25 grid, each with cognitive traits and a camp, that become "infected" by a message with a probability built from social-psychology models. It runs on an ESP32 microcontroller, which hosts its own Wi-Fi network and a web page to steer and watch it.
Why
Disinformation is mass social engineering. The project models which factors make a population adhere to a message regardless of its truth, and which ones help it resist.
My role
Team leader and lead developer, in a team of 5.
Domains and tech
IoT and embedded. C++ (Arduino framework), ESP32, FreeRTOS, PlatformIO, LittleFS, ArduinoJson, HTML and JavaScript front end.
Status
Academic project. Demonstrated as a working simulator. Its findings are qualitative, chiefly that ambient stress is the strongest driver of spread. The public repository holds an earlier, simpler firmware than the one demonstrated.
Period
2025 – 2026

Context and problem

Information manipulation works like social engineering at the scale of a population: it relies on cognitive, social and media factors to get past critical thinking. The project set out to identify the parameters that make people adhere to a piece of information regardless of whether it is true, and to propose a formal model that can simulate a campaign and show where collective resilience comes from.

It was ESILV coursework for the 2025–26 academic year, in a team of five that I led as team leader and lead developer, run in partnership with Campus Cyber.

Closure report, §1.1–1.2; repository README; role and partnership as stated by me

The model

Each of the 625 agents sits on a 25×25 grid and sees its Moore neighbourhood, up to eight neighbours. Agents belong to one of two camps (60 % and 40 %), hold an opinion between 0 and 1, and carry traits such as polarisation, discernment, resistance to manipulation and distrust of outside sources. Infection is one-way: an agent that adheres to the message stays infected.

The demonstrated version, documented in the closure report, combines three layers:

  • Opinion dynamics (Deffuant-Weisbuch). Neighbours whose opinions are close enough, within a tolerance of 0.30, move towards each other at a rate of 0.04 per tick.
  • Persuasion (Elaboration Likelihood Model). An agent’s rational defence, its discernment and resistance, is weakened by ambient stress; the message’s pull grows as it gets closer to the agent’s existing opinion.
  • Social pressure (Drive Theory). The share of infected neighbours pushes towards adhesion, and stress amplifies that push, with an extra boost that grows exponentially above a stress level of 0.7.

These terms feed a logistic function that gives each healthy agent with an infected neighbour its probability of adhering at the next tick. The message can target camp A, camp B or both, and starts either from a single “patient zero” or from a campaign of six seeds.

src/simulation.cpp, lines 84–90 (public firmware)

      const float disc_eff         = me.disc * (1.0f - 0.8f * stress);
      const float social_pressure  = (float)infected_neighbors / total_neighbors;
      const float rational_defense = (disc_eff + me.ino) * (1.0f - stress);
      const float ideological_pull = me.pol * (1.0f - fabsf(me.opinion - message)); // fabsf, not abs
      const float social_drive     = social_pressure * (1.0f + stress);
      const float logit            = ideological_pull + social_drive - rational_defense - me.trust;
      const float probability      = 1.0f / (1.0f + expf(-5.0f * logit));

The public firmware holds a simpler form of this rule (a fixed 0.8 where the report uses a per-agent stress sensitivity, plus certain infection above a stress of 0.95), but not the opinion dynamics, the boost above 0.7 or the polarisation setting: it is an earlier version than the one demonstrated, and the report’s code listing doesn’t correspond to any file in the repository.

Try the model

The model from the closure report runs here in your browser, ported to JavaScript from the report’s equations and tables (§2–4). Choose a target, send a wave, then raise the stress to see the spread speed up.

This is a reconstruction written for this page, not the firmware that ran on the board during the demonstration, whose source is lost. Some choices are mine, where the report leaves a gap or contradicts itself. It gives the opinion update for a pair of neighbours but not how pairs are picked, so here each healthy agent moves towards one random neighbour per tick (eq. 2.2), and that neighbour doesn’t move in the same exchange. Infected neighbours can pull a healthy agent this way, but infected agents only drift towards the message (eq. 4.9). The grid has edges, as in the report’s code listing, although its text calls the grid toroidal. An agent that adheres drifts from its opinion at the start of the tick, as in the listing. Traits are drawn at random, so two populations never react quite the same way.

Closure report, §2–4, tables 2.1 and 3.1, listing A.1; src/scripts/opinion-sim.js in this site’s repository

Architecture

ESP32 firmware architectureA phone or laptop joins the ESP32's own Wi-Fi access point and talks HTTP on port 80. On the ESP32, the Arduino loop serves static pages from LittleFS and a JSON API: GET /data returns the grid and is rate-limited; the control routes need a shared token. A FreeRTOS task pinned to core 0 computes the next generation every 100 ms into a private buffer and commits it to the shared grid under a mutex. The public firmware has one mutex; the report describes a second one for the parameters.Phone or laptop (browser)Wi-Fi access point (ESP32 soft AP)ESP32Arduino loop(), HTTP on port 80· static pages from LittleFS· GET /data: grid as JSON, rate-limited· control routes: shared token requiredread: waits ≤ 20 ms for the mutexshared 25×25 gridmutexcommit: waits ≤ 10 msFreeRTOS task, core 0next generation every 100 ms,private buffer, then commitPublic firmware: one mutex. Report: a second one for the parameters.
How the device is organised. The ESP32 hosts its own Wi-Fi network, so the demo needs no infrastructure: a phone joins it and opens the page. The simulation and the web server run side by side and share the grid, guarded by a mutex, plus two unguarded parameters (stress and target).src/main.cpp, src/simulation.cpp, src/web_server.cpp; closure report §5

The board is a dual-core ESP32. A FreeRTOS task pinned to core 0 computes a new generation every 100 ms with vTaskDelayUntil, so the tick rate doesn’t drift with the work done. It computes each generation into a private buffer and copies it into the shared grid under a mutex, so the web server never reads a half-updated grid. The Arduino loop serves the web pages from the LittleFS file system and a small JSON API: /data returns the state of the 625 cells, and control routes change the stress, the target and the injection mode.

Dark web interface titled Propagation d'opinion: a 25 by 25 grid of muted red and blue cells on the left, with an opinion histogram under it; on the right, buttons to target camp A, camp B or both, a patient zero or six-seed campaign choice, a large Envoyer fake news button, stress and polarisation sliders, infection counters at 0 of 362 and 0 of 263, and a legend.
The interface of the demonstrated version at tick 12, before any message is sent: cells are tinted by opinion (red to blue), and opinions cluster around neutral, with a mean of 0.50; stress is 0.00 and polarisation 0.20. This web front end is not in the public repository.Project visual, closure report §5.4

Security considerations

These protections are in the firmware on GitHub, the older version. Whether the demonstrated firmware had them isn’t known: the web interface recovered from the demonstration board sends no token on its control calls, and the report never mentions one. What the public firmware does:

  • The control routes require a shared token, sent in an X-Token header or a token parameter; anything else gets a 401.
  • /data is open by design but limited to one answer every 100 ms, with a 429 beyond that.
  • Stress and target values are clamped to [0, 1].
  • The Wi-Fi password and the token come from a secrets.h file kept out of git.
  • The README has a short threat model that names the risks it accepts.

src/web_server.cpp, lines 11–19

static bool isAuthorized() {
  if (server.hasHeader("X-Token") && server.header("X-Token") == API_TOKEN)
    return true;
  // Also accept token as query param for easy browser testing
  if (server.hasArg("token") && server.arg("token") == API_TOKEN)
    return true;
  server.send(401, "text/plain", "Unauthorized");
  return false;
}

The limits are real, and some are accepted in the README. The device serves plain HTTP, so the token travels in clear. Accepting it as a URL parameter puts it in browser history. State changes use GET requests. /data allows any origin. The rate limit is global, so one client can starve the others.

Results

The simulation grid at tick 167 with stress at 0.00: a large dark-red block of infected cells in the lower centre and right and a smaller one in the upper left, bright yellow cells at their edges and scattered elsewhere, and grey healthy cells in the upper right and lower left.
Tick 167, stress 0.00: the infected area (dark) has spread across part of the grid; yellow cells are the interface’s “flash infection” marker for newly infected agents, along the spreading front.
The simulation grid at tick 154 with stress at 0.30, every cell dark red, and the opinion histogram reduced to a single bar on the pro-institutional side.
Tick 154, stress 0.30: the whole population is infected and opinions fit in a single histogram bin on the pro-institutional side.

The report’s findings are qualitative. Ambient stress is the strongest lever: as it rises, rational defence falls and social pressure grows, and spread speeds up. Initial polarisation speeds up the formation of opinion bubbles, and the two camps react differently to the same message. The report and the repository contain no run counts, timings or curves, so these are observations of the simulator’s behaviour, not measurements.

Closure report §6.1 and §7.1; English summary

Limitations

  • The repository is behind the demonstration. The public firmware is the earlier model: no opinion dynamics, no boost above 0.7, no polarisation setting, one mutex where the report describes two, and its initial trait distributions differ from the report’s table. The web front end and the secrets.h template aren’t committed, so the repository can’t be built and flashed as its README describes.
  • Shared state in the firmware. Stress and target are plain variables written by the web handlers and read by the simulation task without a lock. A reset that arrives mid-tick can be overwritten by that tick’s commit. If the commit can’t take the mutex within 10 ms, the tick is dropped silently; if /data can’t within 20 ms, it answers with an empty grid.
  • Model limits stated by the team. No recovery state, a regular grid instead of a realistic social network, static traits and a single message.
  • No tests, and the sources contain no recorded experiments behind the results.

Possible next steps

The report lists a recovered state, a scale-free network topology, traits that learn, competing narratives and calibration on survey data. None of it was done. On the device side, the gaps above have direct fixes: POST routes with the token in a header only, a lock around the shared parameters, and the demonstrated firmware and front end committed to the repository.