ESP8266 WiFi Occupancy Sensor


The following is an overview of a project I made using the SparkFun ESP8266 Thing and a PIR Motion Sensor. I used the data they collected and a Lua script in the Exosite Portals platform to determine the occupancy status of a room.

Setting up the ESP8266

On the front, I soldered on six pins to the board in order to connect the 3.3V FTDI Basic to flash the board. I also added 2 pins with a jumper to toggle bootloader and run program modes. This was in order to allow me to debug from the serial port.

On the backside, I soldered on 4 pins to connect the PIR Motion Sensor. Only GND, 3V3, and SDA were actually used to connect the Motion Sensor. You’ll also need to cut the connection between the DTR pins if you want that jumper on the frontside to do anything!

You can power the ESP8266 with either a Micro-B USB connector or a single-cell LiPo battery with a 2-pin JST connector. I chose to use the battery to give myself the ultimate freedom of placing the Thing wherever I wanted within the room.

Connecting the PIR Motion Sensor

On the ESP8266, you’ll need to connect the PIR’s power cord (red) to the 3V3 pin on the ESP8266’s rear. Connect the ground cord (white) to GND on the ESP8266. Connect the alarm cord (black) to SDA on the ESP8266. Before you do that, however, you’ll need to modify the PIR to bypass the 5V regulator in order to power it using 3.3V.

Here is the initial cord setup on the PIR:

You’ll need to detach the red power cord and solder it back to the board as in the image below:

Finally, you’ll need a 10K resistor between SDA and 3V3 on the board to detect the motion sensor’s alarm pin propery. Now, wire everything together!

Flash the Board with Arduino IDE

Great, your hardware is all setup now! Now all that’s left to do is flash the ESP8266 with some code and point your data to Exosite’s cloud.

You’ll need to download the latest version of the Arduino IDE from here. You’ll need to install both Exosite’s library and the ESP8266 library. Finally, navigate to the code example from our Arduino library -> Examples -> ESP8266OccupancySensor. Edit the code with your own information accordingly.

Lua Script

You’ll notice that the example Arduino code only counts the number of times in 15 second intervals that motion is detected. There’s no boolean value given for whether the room is in fact occupied or not. The best way to come up with this value is a Lua script. If you haven’t used Lua scripts in portals before, check out the support article here. The following code is an example script you could use to come up with a boolean value. It reads the number of motions detected from the same dataport written to in the Arduino example, does some processing on those values, and returns a boolean. Either 1 for occupied or 0 for not occupied is written to dataport ‘occupancy’.

local occupancyint = alias['occupancyint']
local occupancy = alias['occupancy']
local t = {}

while true do
	local timestamp = occupancyint.wait()

	local occupancydatapoint = occupancyint[timestamp]

	local pair = {timestamp, occupancydatapoint}
	if (#t < 7) then
		table.insert(t, pair)
		table.remove(t, 1)
		table.insert(t, pair)

	local count = 0;
	for i = 1, #t do 
		if (t[i][2] > 0) then
			count = count + 1;
  	debug(string.format("table length is: " .. #t))
  	debug(string.format("number of values in the table over 1: " .. count))

	if (count > 3) then
		occupancy.value = 1
		debug(string.format("Wrote value 1 to dataport occupancy"))
		occupancy.value = 0
		debug(string.format("Wrote value 0 to dataport occupancy"))

Hopefully you all will be able to use this example as a jumping board for your own ESP8266-fueled adventures!


You’ll notice in a recent update to the arduino_exosite_library GitHub repo I’ve included support for the Dallas Temperature DS18B20 in parasite power mode. The product page on Sparkfun is here. Hookup is fairly self-explanatory. The two pins on the left and right are combined into one wire that goes to ground. A schematic is now present in the readme for this project in that arduino_exosite_library repo.


I have not been able to get this sketch to work on my ESP8266, I keep getting an error that the device cannot open a connection to exosite. It works fine with dozens of other of networking sketches. Any ideas on why this might be?

Sketch is:

#include <WiFiClient.h>
#include <SPI.h>
//#include <ESP8266mDNS.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include <Exosite.h> //NET changed for pH
#include <ESP8266WiFi.h>

const char* ssid = " ";
const char* password = " ";
String cikData = “”;
//String returnString;
//String readString = “pH”;

class WiFiClient client;

Exosite exosite(cikData, &client);

void setup() {



void loop() {
String readParam = “pH”;
String writeString = “pH=1”;
String returnString = “”;

while (!exosite.writeRead(writeString, readParam, returnString)) {
Serial.println(“Retrying connection”);




unsigned long Exosite:time();

Use in setup to connect to local Wifi network
void connectWifi() {

Serial.println("Connecting to ");

WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED) {

Serial.println(“WiFi connected”);
Serial.print("IP address: ");




You have to use HTTPS with Exosite. So switch that line to WiFiClientSecure client;