Skip to content

Instantly share code, notes, and snippets.

@gdamjan
Last active August 29, 2015 14:13
Show Gist options
  • Save gdamjan/64fb6d6152ed1f9c8178 to your computer and use it in GitHub Desktop.
Save gdamjan/64fb6d6152ed1f9c8178 to your computer and use it in GitHub Desktop.
irc in lua for the esp8266 with nodemcu -- https://github.com/nodemcu/nodemcu-firmware
function connect_callback(conn)
conn:send("NICK esp8266_hacklab\r\n")
conn:send("USER esp8266_hacklab 8 * :Heeelloooo\r\n")
conn:send("JOIN #lugola\r\n")
conn:send("PRIVMSG #lugola :здраво свет\r\n")
end
function receive_callback(conn, payload)
print(payload)
if string.find(payload, "PING :") == 1 then
conn:send("PONG :" .. string.sub(payload, 7))
end
end
function startbot()
local conn
conn = net.createConnection(net.TCP, false)
conn:on("receive", receive_callback)
conn:on("connection", connect_callback)
conn:connect(6667, "chat.freenode.net")
end
tmr.alarm(1, 1000, 1, function()
if wifi.sta.getip()==nil then
print("Waiting for connection...")
else
tmr.stop(1) -- maybe only stop it on successfull connection
startbot()
end
end)
#! /usr/bin/env python3
# very crude python3 script to upload an init.lua file to an esp8266 with nodemcu
# uses pyserial and uses my BusPirate(v4) in transparent UART mode
# (the only USB to TTL-UART that gave stable 3.3V power)
#
# the trick is to write commands to the nodemcu repl slowly since it can't handle it faster
# and replace the line separators in the unix file with \r when sending over serial
import sys, time
import serial
fp = open(sys.argv[1], 'rb')
ser = serial.Serial('/dev/bus-pirate', baudrate=9600)
for cmd in [b'file.open("init.lua","w")', b'file.writeline([[']:
ser.write(cmd + b'\r\n')
time.sleep(0.3) # can't do it faster
for line in fp:
ser.write(line.rstrip() + b'\r')
time.sleep(0.3) # can't do it faster
for cmd in [b']])', b'file.close()', b'node.restart()']:
ser.write(cmd + b'\r\n')
time.sleep(0.3) # can't do it faster
ser.close()
fp.close()
-- once the init.lua file is there, enter commands in the repl to connect to the wifi
-- the esp8266 will remember these settings and always connect automatically
wifi.setmode(wifi.STATION)
wifi.station.autoconnect(1)
wifi.sta.config("KIKA", "sz123456")
node.restart()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment