BV1AL 之無所不記

2015-12-31

MK802 telegram + ESP8266 自動控制

由於ESP8266 本身俱有網路功能,只要有wifi 就能簡單建構起來。
這裡使用文字介面的 telegram-cli 加 esp8266(執行 telnetd service)。

利用 telegram-cli 當載具,可以接收來自地表各處給的指令(中國除外)、
向OS(Linux) 送出指令,介面由 python 組成(在我五月寫的 Telegram 機器人
那一篇有說明,用一台小小 的 MK802 (或Raspberry Pi)就可以達成。

MK802 到ESP8266 的介面也是藉由 python 來達成
ESP8266 用nodemcu 跑 telnetd service

與ESP8266 互動的部份:
espc = pexpect.spawn('telnet %s 23'%esp8266_IP)
--以下sendline() 內容可以改成任何指令
espc.sendline('gpio.mode(%s,gpio.OUTPUT)'%pin)
espc.expect('> ')
--取得執行後的資訊
espMSG=espc.before
print(espMSG)

ESP8266 執行 telnetd service 的部份:
s=net.createServer(net.TCP,180)
s:listen(23,function(c)
  function s_output(str)
    if(c~=nil) then c:send(str) end
  end
  node.output(s_output, 0)
  c:on("receive",function(c,l)
    node.input(l)
  end)
  c:on("disconnection",function(c)
    node.output(nil)
  end)
  print(" >> DE BV1AL <<")
end)
 
2015年要結束了,最後一天來灌點水! 

標籤: , , , , ,

0 Comments:

張貼留言

<< Home