LoginSignup
2
4

More than 5 years have passed since last update.

ボタンが押されたら通知する

Posted at

ESP8266にnodemcuを入れて、押しボタンを接続して、ボタンが押されたらiFTTTでiPhoneに通知する。

◯接続
RSTとGNDの間にプッシュボタンをつなぎ、RSTとGPIO16を繋ぐ

◯動作
起動するとWi-Fiの接続を確認してiftttにつなぐ。送信したらdeepsleepに入る。
⭐️単純にこのままだとメンテナンスできないので、起動時にGPIO14がlowだと何も処理せず、sleepもしないようにする(これでシリアルでメンテできる)

◯コード


-- wake up, check wifi connection

print("button_ifttt")

function send_ifttt()
conn=net.createConnection(net.TCP, 0)
conn:on("receive", function(conn, payload)
print(payload) 
end) 
conn:on("connection", function(c) 
tmr.stop(0)
print("connected")
conn:send("GET /trigger/test/with/key/YOURKEY?value1=button HTTP/1.1\r\n") 
conn:send("Host: maker.ifttt.com\r\n")
conn:send("Accept: /\r\n") 
conn:send("User-Agent: Mozilla/4.0 (compatible; esp8266 Lua; Windows NT 5.)\r\n") 
conn:send("\r\n")
end)
conn:on("sent", function(c)
print("sent")

-- deep sleep infinite
tmr.delay(500000)
-- node.dsleep(0)
end)
tmr.alarm(0, 30*1000, 0, function()
-- abort
print("connecting timeout")
conn.close()
tmr.delay(500*1000) -- 500msec
-- node.dsleep(0)
end)
conn:connect(80,'maker.ifttt.com') 
end

led = 2
gpio.mode(led, gpio.OUTPUT)
function ledOn()
gpio.write(led, gpio.HIGH)
end
function ledOff()
gpio.write(led, gpio.LOW)
end

check = 0
function check_wifi()
tmr.alarm(0, 1000, 1, function()
ledOn()
print("check wifi connection")
if 5 == wifi.sta.status() then
print("let's ifttt")
tmr.stop(0)
send_ifttt()
else
if 10 > check then
print("wait wifi connection...")
check = check + 1
else
-- restart 1min later
print("retry 1min later")
tmr.stop(0)
tmr.delay(500000)
node.dsleep(60*1000*1000)
end
ledOff()
end
end)
end

ledOn()

gpio.mode(5, gpio.INPUT, gpio.PULLUP)
if gpio.LOW == gpio.read(5) then
print("abort")
else
check_wifi()
end


※念のためにdeepsleepはコメントアウトしてあります

◯補足
はじめはGPIOでのキャンセル処理してを入れてなくて、ファーム書き込みをやり直す羽目になりました。
さらに、ファーム書き込みに失敗したのか起動時にへんなメッセージが、、、(_)
何度かファーム書き込みをやり直して復活しました

◯課題
Wi-Fiが繋がらないと無限リトライになっているので諦めるようにする

2
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
4