10 buck WiFi PIR Sensor

Posted on
Mon Feb 23, 2015 11:16 am
BassMint offline
Posts: 105
Joined: Dec 24, 2013

10 buck WiFi PIR Sensor

I've completed my first project utilizing the ESP8266. A WiFi Passive Infrared Sensor for $10.
ESP8266 $3, Converter $1, PIR $2 Breadboard $1, 5v PS $3
(some items bought in bulk (x4), haven't figured in an enclosure yet.)

It talks to Indigo via the Cynical Network plugin.

When I figure out the DHT-11 Temp/humidity sensors I have, I am going to stick one on the board. After I've tested the bugger for bit, Im going to make a bunch.

I'm using the nodeMCU variation of the firmware which utilized the LUA language. (which I am now learning)
This is the code for the ESP8266

Code: Select all
-- Send Alarm status to HomeServer
-- some code taken from ok1cdj
-- 2015 AReResearch (Andy Reischle)

SensorID = "1"
status = "CLEAR"
oldstatus = "CLEAR"

gpio.mode(3,gpio.INPUT,gpio.FLOAT)

tmr.alarm(0, 1000, 1, function() -- Set alarm to one second
   if gpio.read(3)==0 then status="ALARM" else status="CLEAR" end
    if status ~= oldstatus then sendalarm (SensorID,status) end
   oldstatus = status
end)

function sendalarm(SensorID,status)
print("Open connection...")
    conn=net.createConnection(net.TCP, 0)
    conn:on("receive", function(sck, c) print(c) end )
    conn:connect(40001, "192.168.1.10")
    conn:send(status)
    conn:on("sent", function(conn) print "Closing connection" conn:close() end)
end
Attachments
ESP PIR.png
ESP PIR.png (495.01 KiB) Viewed 2855 times

Posted on
Mon Feb 23, 2015 11:30 am
Korey offline
User avatar
Posts: 816
Joined: Jun 04, 2008
Location: Henderson, NV

Re: 10 buck WiFi PIR Sensor

Nice work!

Package those units up and sell them!

I'd be interested in a few !

--
Korey

Posted on
Mon Feb 23, 2015 2:37 pm
roussell offline
User avatar
Posts: 1108
Joined: Aug 18, 2008
Location: Alabama

Re: 10 buck WiFi PIR Sensor

Nice work, I've been playing with them too for cheap sensors to use in place of door/window switches. Haven't tried the PIR yet though!

Posted on
Sat Feb 28, 2015 4:33 am
richo offline
Posts: 158
Joined: Nov 25, 2014
Location: Pomorskie, Poland

Re: 10 buck WiFi PIR Sensor

Have you tried to update variables in Indigo using RESTfull with authorization?

Ryszard

Posted on
Sun Mar 01, 2015 1:52 pm
richo offline
Posts: 158
Joined: Nov 25, 2014
Location: Pomorskie, Poland

Re: 10 buck WiFi PIR Sensor

OK got it working w/o Cynical but no authentication using RESTfull call.

Can anyone help me with authentication - even basic authorisation? I'm new to HTTP calls.

Code: Select all
--Send sensor status to Indogo Server using RESTfull w/o authentication

function sendalarm(SensorID,status)
     print("Open connection...")
     conn=net.createConnection(net.TCP, 0)
     conn:on("receive", function(conn, payload)
          --print(payload)
     end)
     conn:connect(8176,'192.168.0.2')
     conn:send("PUT /variables/ESPtest?_method=put&value="..status.." HTTP/1.1\r\n")
     conn:send("Content-Length: 0\r\n")
     conn:send("Host: Indigo Home Control Server\r\n")
     conn:send("Accept: */*\r\n")
     conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
     conn:send("\r\n")
     conn:on("sent",function(conn)
          conn:close()
          print("Closed connection...")
     end)
end

SensorID = "1"
status = "OPEN"
oldstatus = "OPEN"

doorpin=4
gpio.mode(doorpin,gpio.INPUT,gpio.INT)

tmr.alarm(0, 100, 1, function() -- Set alarm to one second
   if gpio.read(doorpin)==0 then status="OPEN" else status="CLOSED" end
    if status ~= oldstatus then sendalarm (SensorID,status) end
   oldstatus = status
end)

Ryszard

Posted on
Sun Mar 01, 2015 3:33 pm
richo offline
Posts: 158
Joined: Nov 25, 2014
Location: Pomorskie, Poland

Re: 10 buck WiFi PIR Sensor

I was trying to add authentication like that but didn't work !?

Encoded admin:admin with base64 to: YWRtaW46YWRtaW4=

PS
Changed Indigo authentication to basic with IndigoWebServer.conf:
[global]
authen.use_digest = False
authen.use_basic = True

Code: Select all
     conn:send("PUT /variables/ESPtest?_method=put&value="..status.." HTTP/1.1\r\n")
     conn:send("Authorization: Basic YWRtaW46YWRtaW4=")
     conn:send("Content-Length: 0\r\n")
     conn:send("Host: Indigo Home Control Server\r\n")
     conn:send("Accept: */*\r\n")
     conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.1)\r\n")
     conn:send("\r\n")

Ryszard

Posted on
Tue Mar 03, 2015 3:31 pm
richo offline
Posts: 158
Joined: Nov 25, 2014
Location: Pomorskie, Poland

Re: 10 buck WiFi PIR Sensor

Finally got it working but to be honest don't know why. Here is a final code.

Code: Select all
--Send sensor status to Indigo Server using RESTfull w/o authentication

function sendalarm(SensorID,status)
     print("Open connection...")
     sk=net.createConnection(net.TCP, 0)
     sk:on("receive", function(sck, c) print("Payload: \n"..c) end)
     sk:connect(8176,"192.168.0.2")
     sk:send("PUT /variables/ESPtest?_method=put&value="..status)
     sk:send(" HTTP/1.1\r\nHost: 192.168.0.2\r\nContent-Length: 0\r\n")
     sk:send("Authorization: Basic YWRtaW46YWRtaW4=\r\nAccept: */*\r\n\r\n")
     print('Memory left:'.. node.heap())
     sk:on("sent", function(sck) sk:close() print("Closed connection") end)
end

SensorID = "1"
status = "OPEN"
oldstatus = "OPEN"

doorpin=4
gpio.mode(doorpin,gpio.INT)


tmr.alarm(0, 100, 1, function() -- Set pause between status readings
   if gpio.read(doorpin)==0 then status="OPEN" else status="CLOSED" end
    if status ~= oldstatus then sendalarm (SensorID,status) end
   oldstatus = status
end)

Ryszard

Page 1 of 1

Who is online

Users browsing this forum: No registered users and 2 guests