Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Polish Lua examples #2846

Merged
merged 10 commits into from
Dec 30, 2019
4 changes: 3 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ addons:
packages:
- python-serial
- srecord
- luarocks
cache:
- directories:
- cache
Expand All @@ -33,4 +34,5 @@ script:
- echo "checking:"
- find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 echo
- find lua_modules lua_examples -iname "*.lua" -print0 | xargs -0 $LUACC -p
# - if [ "$OS" = "linux" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck.sh || true ; fi
- if [ "$OS" = "linux" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck-linux.sh; fi
- if [ "$OS" = "windows" ]; then bash "$TRAVIS_BUILD_DIR"/tools/travis/run-luacheck-windows.sh; fi
64 changes: 34 additions & 30 deletions lua_examples/adc_rgb.lua
Original file line number Diff line number Diff line change
@@ -1,43 +1,47 @@
--
-- Light sensor on ADC(0), RGB LED connected to gpio12(6) Green, gpio13(7) Blue & gpio15(8) Red.
-- Light sensor on adc0(A0), RGB LED connected to gpio12(D6) Green, gpio13(D7) Blue & gpio15(D8) Red.
-- This works out of the box on the typical ESP8266 evaluation boards with Battery Holder
--
-- It uses the input from the sensor to drive a "rainbow" effect on the RGB LED
-- Includes a very "pseudoSin" function
--
-- Required C Modules: adc, tmr, pwm

function led(r,Sg,b)
pwm.setduty(8,r)
pwm.setduty(6,g)
pwm.setduty(7,b)
local redLed, greenLed, blueLed = 8, 6, 7

local function setRGB(r,g,b)
pwm.setduty(redLed, r)
pwm.setduty(greenLed, g)
pwm.setduty(blueLed, b)
end

-- this is perhaps the lightest weight sin function in existance
-- Given an integer from 0..128, 0..512 appximating 256 + 256 * sin(idx*Pi/256)
-- this is perhaps the lightest weight sin function in existence
-- Given an integer from 0..128, 0..512 approximating 256 + 256 * sin(idx*Pi/256)
-- This is first order square approximation of sin, it's accurate around 0 and any multiple of 128 (Pi/2),
-- 92% accurate at 64 (Pi/4).
function pseudoSin (idx)
idx = idx % 128
lookUp = 32 - idx % 64
val = 256 - (lookUp * lookUp) / 4
if (idx > 64) then
val = - val;
end
return 256+val
-- 92% accurate at 64 (Pi/4).
local function pseudoSin(idx)
idx = idx % 128
local lookUp = 32 - idx % 64
local val = 256 - (lookUp * lookUp) / 4
if (idx > 64) then
val = - val;
end
return 256+val
end

pwm.setup(6,500,512)
pwm.setup(7,500,512)
pwm.setup(8,500,512)
pwm.start(6)
pwm.start(7)
pwm.start(8)
do
pwm.setup(redLed, 500, 512)
pwm.setup(greenLed,500, 512)
pwm.setup(blueLed, 500, 512)
pwm.start(redLed)
pwm.start(greenLed)
pwm.start(blueLed)

tmr.alarm(1,20,1,function()
idx = 3 * adc.read(0) / 2
r = pseudoSin(idx)
g = pseudoSin(idx + 43)
b = pseudoSin(idx + 85)
led(r,g,b)
idx = (idx + 1) % 128
end)
tmr.create():alarm(20, tmr.ALARM_AUTO, function()
local idx = 3 * adc.read(0) / 2
local r = pseudoSin(idx)
local g = pseudoSin(idx + 43) -- ~1/3rd of 128
local b = pseudoSin(idx + 85) -- ~2/3rd of 128
setRGB(r,g,b)
end)
end
25 changes: 14 additions & 11 deletions lua_examples/dcc/dcc.lua
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ local PIN = 2 -- GPIO4

local addr = 0x12a

CV = {[29]=0,
local CV = {[29]=0,
[1]=bit.band(addr, 0x3f), --CV_ACCESSORY_DECODER_ADDRESS_LSB (6 bits)
[9]=bit.band(bit.rshift(addr,6), 0x7) --CV_ACCESSORY_DECODER_ADDRESS_MSB (3 bits)
}
Expand Down Expand Up @@ -37,18 +37,18 @@ end

local function DCC_command(cmd, params)
if not is_new(cmd, params) then return end
if cmd == dcc.DCC_IDLE then
if cmd == dcc.DCC_IDLE then
return
elseif cmd == dcc.DCC_TURNOUT then
print("Turnout command")
print("Turnout command")
elseif cmd == dcc.DCC_SPEED then
print("Speed command")
print("Speed command")
elseif cmd == dcc.DCC_FUNC then
print("Function command")
print("Function command")
else
print("Other command", cmd)
end

for i,j in pairs(params) do
print(i, j)
end
Expand All @@ -69,18 +69,21 @@ local function CV_callback(operation, param)
elseif operation == dcc.CV_VALID then
oper = "Valid"
result = 1
elseif operation == CV_RESET then
elseif operation == dcc.CV_RESET then
oper = "Reset"
CV = {}
end
print(("[CV_callback] %s CV %d%s"):format(oper, param.CV, param.Value and "\tValue: "..param.Value or "\tValue: nil"))
print(("[CV_callback] %s CV %d%s")
:format(oper, param.CV, param.Value and "\tValue: "..param.Value or "\tValue: nil"))
return result
end

dcc.setup(PIN,
DCC_command,
dcc.MAN_ID_DIY, 1,
--bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT, dcc.FLAGS_DCC_ACCESSORY_DECODER, dcc.FLAGS_MY_ADDRESS_ONLY), -- Accessories (turnouts) decoder
bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT), -- Cab (train) decoder
dcc.MAN_ID_DIY, 1,
-- Accessories (turnouts) decoder:
--bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT, dcc.FLAGS_DCC_ACCESSORY_DECODER, dcc.FLAGS_MY_ADDRESS_ONLY),
-- Cab (train) decoder
bit.bor(dcc.FLAGS_AUTO_FACTORY_DEFAULT),
0, -- ???
CV_callback)
55 changes: 30 additions & 25 deletions lua_examples/email/read_email_imap.lua
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
-- was tested with an AOL and Time Warner cable email accounts (GMail and other services who do
-- not support no SSL access will not work).

require("imap")
local imap = require("imap")

local IMAP_USERNAME = "email@domain.com"
local IMAP_PASSWORD = "password"
Expand All @@ -25,21 +25,13 @@ local SSID_PASSWORD = "password"


local count = 0 -- we will send several IMAP commands/requests, this variable helps keep track of which one to send

-- configure the ESP8266 as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)

-- create an unencrypted connection
local imap_socket = net.createConnection(net.TCP,0)

local imap_socket, timer

---
-- @name setup
-- @description A call back function used to begin reading email
-- upon sucessfull connection to the IMAP server
function setup(sck)
local function setup(sck)
-- Set the email user name and password, IMAP tag, and if debugging output is needed
imap.config(IMAP_USERNAME,
IMAP_PASSWORD,
Expand All @@ -49,19 +41,16 @@ function setup(sck)
imap.login(sck)
end

imap_socket:on("connection",setup) -- call setup() upon connection
imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server

local subject = ""
local from = ""
local message = ""
local body = ""

---
-- @name do_next
-- @description A call back function for a timer alarm used to check if the previous
-- IMAP command reply has been processed. If the IMAP reply has been processed
-- this function will call the next IMAP command function necessary to read the email
function do_next()
local function do_next()

-- Check if the IMAP reply was processed
if(imap.response_processed() == true) then
Expand All @@ -75,15 +64,18 @@ function do_next()
count = count + 1
elseif (count == 1) then
-- After examining/selecting the INBOX folder we can begin to retrieve emails.
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT") -- Retrieve the SUBJECT of the first/newest email
-- Retrieve the SUBJECT of the first/newest email
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"SUBJECT")
count = count + 1
elseif (count == 2) then
subject = imap.get_header() -- store the SUBJECT response in subject
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM") -- Retrieve the FROM of the first/newest email
-- Retrieve the FROM of the first/newest email
imap.fetch_header(imap_socket,imap.get_most_recent_num(),"FROM")
count = count + 1
elseif (count == 3) then
from = imap.get_header() -- store the FROM response in from
imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num()) -- Retrieve the BODY of the first/newest email
-- Retrieve the BODY of the first/newest email
imap.fetch_body_plain_text(imap_socket,imap.get_most_recent_num())
count = count + 1
elseif (count == 4) then
body = imap.get_body() -- store the BODY response in body
Expand All @@ -92,9 +84,9 @@ function do_next()
else
-- display the email contents

-- create patterns to strip away IMAP protocl text from actual message
pattern1 = "(\*.+\}\r\n)" -- to remove "* n command (BODY[n] {n}"
pattern2 = "(%)\r\n.+)" -- to remove ") t1 OK command completed"
-- create patterns to strip away IMAP protocol text from actual message
local pattern1 = "%*.*}\n" -- to remove "* n command (BODY[n] {n}"
local pattern2 = "%)\n.+" -- to remove ") t1 OK command completed"

from = string.gsub(from,pattern1,"")
from = string.gsub(from,pattern2,"")
Expand All @@ -108,13 +100,26 @@ function do_next()
body = string.gsub(body,pattern2,"")
print("Message: " .. body)

tmr.stop(0) -- Stop the timer alarm
timer:stop() -- Stop the timer alarm
imap_socket:close() -- close the IMAP socket
collectgarbage() -- clean up
end
end

end

-- A timer alarm is sued to check if an IMAP reply has been processed
tmr.alarm(0,1000,1, do_next)
do
-- configure the ESP8266 as a station
wifi.setmode(wifi.STATION)
wifi.sta.config(SSID,SSID_PASSWORD)
wifi.sta.autoconnect(1)

-- create an unencrypted connection
imap_socket = net.createConnection(net.TCP,0)
imap_socket:on("connection",setup) -- call setup() upon connection
imap_socket:connect(IMAP_PORT,IMAP_SERVER) -- connect to the IMAP server

-- A timer alarm is sued to check if an IMAP reply has been processed
timer = tmr.create()
timer:alarm(1000, tmr.ALARM_AUTO, do_next)
end
58 changes: 27 additions & 31 deletions lua_examples/email/send_email_smtp.lua
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
---
-- Working Example: https://www.youtube.com/watch?v=CcRbFIJ8aeU
-- @description a basic SMTP email example. You must use an account which can provide unencrypted authenticated access.
-- This example was tested with an AOL and Time Warner email accounts. GMail does not offer unecrypted authenticated access.
-- @description a basic SMTP email example. You must use an account which can
-- provide unencrypted authenticated access.
-- This example was tested with an AOL and Time Warner email accounts.
-- GMail does not offer unencrypted authenticated access.
-- To obtain your email's SMTP server and port simply Google it e.g. [my email domain] SMTP settings
-- For example for timewarner you'll get to this page http://www.timewarnercable.com/en/support/faqs/faqs-internet/e-mailacco/incoming-outgoing-server-addresses.html
-- For example for timewarner you'll get to this page
-- http://www.timewarnercable.com/en/support/faqs/faqs-internet/e-mailacco/incoming-outgoing-server-addresses.html
-- To Learn more about SMTP email visit:
-- SMTP Commands Reference - http://www.samlogic.net/articles/smtp-commands-reference.htm
-- See "SMTP transport example" in this page http://en.wikipedia.org/wiki/Simple_Mail_Transfer_Protocol
-- @author Miguel

require("base64")

-- The email and password from the account you want to send emails from
local MY_EMAIL = "esp8266@domain.com"
local EMAIL_PASSWORD = "123456"
Expand All @@ -37,18 +38,18 @@ wifi.sta.autoconnect(1)
local email_subject = ""
local email_body = ""
local count = 0

local timer

local smtp_socket = nil -- will be used as socket to email server

-- The display() function will be used to print the SMTP server's response
function display(sck,response)
local function display(_, response)
print(response)
end

-- The do_next() function is used to send the SMTP commands to the SMTP server in the required sequence.
-- I was going to use socket callbacks but the code would not run callbacks after the first 3.
function do_next()
local function do_next()
if(count == 0)then
count = count+1
local IP_ADDRESS = wifi.sta.getip()
Expand All @@ -58,10 +59,10 @@ function do_next()
smtp_socket:send("AUTH LOGIN\r\n")
elseif(count == 2) then
count = count + 1
smtp_socket:send(base64.enc(MY_EMAIL).."\r\n")
smtp_socket:send(encoder.toBase64(MY_EMAIL).."\r\n")
elseif(count == 3) then
count = count + 1
smtp_socket:send(base64.enc(EMAIL_PASSWORD).."\r\n")
smtp_socket:send(encoder.toBase64(EMAIL_PASSWORD).."\r\n")
elseif(count==4) then
count = count+1
smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
Expand All @@ -82,26 +83,27 @@ function do_next()
smtp_socket:send(message.."\r\n.\r\n")
elseif(count==8) then
count = count+1
tmr.stop(0)
timer:stop()
smtp_socket:send("QUIT\r\n")
else
smtp_socket:close()
end
end

-- The connectted() function is executed when the SMTP socket is connected to the SMTP server.
-- The connected() function is executed when the SMTP socket is connected to the SMTP server.
-- This function will create a timer to call the do_next function which will send the SMTP commands
-- in sequence, one by one, every 5000 seconds.
-- You can change the time to be smaller if that works for you, I used 5000ms just because.
function connected(sck)
tmr.alarm(0,5000,1,do_next)
local function connected()
timer = tmr.create()
timer:alarm(5000, tmr.ALARM_AUTO, do_next)
end

-- @name send_email
-- @description Will initiated a socket connection to the SMTP server and trigger the connected() function
-- @param subject The email's subject
-- @param body The email's body
function send_email(subject,body)
local function send_email(subject,body)
count = 0
email_subject = subject
email_body = body
Expand All @@ -111,19 +113,13 @@ function send_email(subject,body)
smtp_socket:connect(SMTP_PORT,SMTP_SERVER)
end

-- Send an email
send_email(
"ESP8266",
[[Hi,
How are your IoT projects coming along?
Best Wishes,
ESP8266]])









do
-- Send an email
send_email(
"ESP8266",
[[Hi,
How are your IoT projects coming along?
Best Wishes,
ESP8266]]
)
end
Loading