As the title says... Chat on...

User avatar
By Zhymka
#27145 Hello,

This will be my first post, not sure if posting in correct topic.

I have connected PIR element to ESP chip to GPIO2 port, PIR uses external +5V source. When both connected esp fails to start, if you plug out pir output form gpio2 then it start and again reconnecting with pir output works.
The PIR chip is SR501. I don't understand what i am doing here wrong. Can i handle it in code? Or should i...like create port waiting, or checking if files already ...or add a button to say, it is ok to run scripts...

And the second question: for some reason UP flag does not work, so had to use down, basically at this elements configuration it does not matter. But for what reason when specified UP, trigger does not trigger. is it firware problem?
or wrong usage of code?

A firmware used:
https://github.com/nodemcu/nodemcu-firm ... v_20150704 (integer)
(or should i use different firmware?)

Here is my code:

Init.Lua ->
Code: Select allprint("Setting up WIFI...")
wifi.setmode(wifi.STATION)
wifi.sta.config("#####","?????")
wifi.sta.connect()

tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()== nil then
print("IP unavaiable, Waiting...")
else
tmr.stop(1)
print("Config done, IP is "..wifi.sta.getip())
dofile("pirSensorDataTransmitter.lua")
end
end)


pirSensorDataTransmitter.lua ->
Code: Select allpirSensorPIN = 4

gpio.mode(pirSensorPIN, gpio.INT)
gpio.trig(pirSensorPIN, 'down', sendData)


function sendData()

   --if (gpio.read(pirSensorPIN) == gpio.LOW) return;
   
   conn= net.createConnection(net.TCP, 0)
   conn:on("receive", function(conn, payload) print(payload) end)
   
   conn:connect(80,'184.106.153.149')
   conn:send("GET /update?key=#######??????####\r\n")
   conn:send("Host: api.thingspeak.com\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)
                    print("Closing connection")
                    conn:close()
                 end)
   conn:on("disconnection", function(conn)
                           print("Got disconnection...")
  end)
end

User avatar
By Aeprox
#27168 GPIO0, GPIO2 and GPIO15 are used at boot to choose between flash-mode or user-mode (or something similar, can't recall the actual names). So whatever voltage the PIR is putting on the pin at start-up will define how it boots. My guess is it always boots in flash-mode?

Try using another pin like GPIO12, 13, 14.. What module version are you using? Some don't have these pins pinned-out sadly, but you can hack the hardware a bit, though that requires solder skills with tiny wires.

Concerning your gpio.trig problem; maybe https://github.com/nodemcu/nodemcu-firmware/issues/179 will help?
User avatar
By Zhymka
#27233 Yes that does it, thank you both, need to learn more about those modes. Barnabybear nice trick there you're using, it most likely encapsulates what Aeprox was saying. Thanks : )

A esp version is 01 it has just to i/o ports : ). About that UP flag issue i will need to test, i tried pullup resistor, hovewer it is probably pull_down resistor which i should use...debouncing technique is not related to this issue. If i would find the answer about it, i would post it here. Kind of strange why it is not working, since 'down', 'both' or others works just fine.