20160526_jeffrey - silenceuncrio/diary GitHub Wiki

Index

  • 0915 - hot to burn code through u-boot
  • 0930 - by SD card
  • 0940 - nodemcu
  • 1110 - m300 Downloading an image to MMC/SD
  • 1305 - NodeMCU tmr adc module
  • 1510 - sample ADC
  • 1540 - imx-test
  • 1545 - NodeMCU 連 公司 AP 發 mail
  • 1725 - 偵測聲音達到某個強度便發 email

0915

今天先回頭來做一下

hot to burn code through u-boot

  • by SD card
  • by tftp

0930

by SD card 的部分可以條列如下

  • sudo dd if=<image name>.sdcard of=/dev/sd<x> bs=1M && sync
  • <image name> - under /tmp/deploy/images/imx6ulevk/
  • <x> - use cat /proc/partitions to make sure device node of your SD card

by tftp 的部分可以緩一下... 時間還很充裕

0940

玩一下 nodemcu

逛一下 https://github.com/nodemcu

這個 Online firmware custom build 再看一次還是覺得驚訝

NodeMCU custom buildsSelect modules to include
勾選了 ADC module, 填了 mail, 按 start your build

過了一下收到 mail 後下載 firmware, 然後利用 NodeMCU Flasher burn 到 NodeMCU 去

記得是用 ESPlorer 來做開發的

目前已經在 notebook 上準備好 NodeMCU 的開發環境 - 利用 ESPlore

1110

回到 m300 - Downloading an image to MMC/SD

1305

再玩一下 NodeMCU

目前只用到 tmr 和 adc 兩個 module

init.lua

tmr.alarm(0, 500, tmr.ALARM_AUTO, function()
  val = adc.read(0)
  print(val)
end)

這邊的意思是每 0.5 秒會 sample ADC 並將值印出來

1510

目前是放一段流行音樂來做實驗
調整播放期的音量來觀察 sample 到的 ADC 值
音樂是有節奏的
所有有些當下 sample 到的值會誤以為沒有音樂
做了改寫如下

x = 0
v = 0

tmr.alarm(0, 100, tmr.ALARM_AUTO, function()
  if x < 10 then
    x = x + 1
    a = adc.read(0)
    v = v + a
  else
    print(v/10)
    x = 0
    v = 0
  end  
end)

每秒抓十次做個平均再當成是該秒的 sample 值

下一階段可以來搞個觸發機制發 mail 出去
今天就先這樣
要做正事了

1540

幫一下 jammy 準備一個啥 imx-test
MACHINE=imx6ulevk source fsl-setup-release.sh -b build_jammy
bitbake imx-test

預計要兩個小時吧

1545

回頭看 NodeMCU

先讓 NodeMCU 連到公司的 AP

ip = wifi.sta.getip()
print(ip)
--nil
wifi.setmode(wifi.STATION)
wifi.sta.config("ISMS-2G", "proscendisms")
ip = wifi.sta.getip()
print(ip)

發個 mail

lua_examples 有一些範例可以參考

1725

下面的 code 已經可偵測聲音達到某個強度便發 email

init.lua

---
-- 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.
-- 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
-- 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 = "[email protected]"
local EMAIL_PASSWORD = "abc456"

-- The SMTP server and port of your email provider.
-- If you don't know it google [my email provider] SMTP settings
local SMTP_SERVER = "mail.proscend.com"
local SMTP_PORT = "25"

-- The account you want to send email to
local mail_to = "[email protected]"

-- Your access point's SSID and password
local SSID = "ISMS-2G"
local SSID_PASSWORD = "proscendisms"

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

-- These are global variables. Don't change their values
-- they will be changed in the functions below
local email_subject = ""
local email_body = ""
local count = 0


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)
     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()
            if(count == 0)then
                count = count+1
                local IP_ADDRESS = wifi.sta.getip()
                smtp_socket:send("HELO "..IP_ADDRESS.."\r\n")
            elseif(count==1) then
                count = count+1
                smtp_socket:send("AUTH LOGIN\r\n")
            elseif(count == 2) then
                count = count + 1
                smtp_socket:send(base64.enc(MY_EMAIL).."\r\n")
            elseif(count == 3) then
                count = count + 1
                smtp_socket:send(base64.enc(EMAIL_PASSWORD).."\r\n")
            elseif(count==4) then
                count = count+1
               smtp_socket:send("MAIL FROM:<" .. MY_EMAIL .. ">\r\n")
            elseif(count==5) then
                count = count+1
               smtp_socket:send("RCPT TO:<" .. mail_to ..">\r\n")
            elseif(count==6) then
                count = count+1
               smtp_socket:send("DATA\r\n")
            elseif(count==7) then
                count = count+1
                local message = string.gsub(
                "From: \"".. MY_EMAIL .."\"<"..MY_EMAIL..">\r\n" ..
                "To: \"".. mail_to .. "\"<".. mail_to..">\r\n"..
                "Subject: ".. email_subject .. "\r\n\r\n"  ..
                email_body,"\r\n.\r\n","")
                
                smtp_socket:send(message.."\r\n.\r\n")
            elseif(count==8) then
               count = count+1
                 tmr.stop(0)
                 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.
-- 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)
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)
     count = 0
     email_subject = subject
     email_body = body
     smtp_socket = net.createConnection(net.TCP,0)
     smtp_socket:on("connection",connected)
     smtp_socket:on("receive",display)
     smtp_socket:connect(SMTP_PORT,SMTP_SERVER)
end



x = 0
v = 0

send_once = 0

tmr.alarm(0, 100, tmr.ALARM_AUTO, function()
  if x < 10 then
    x = x + 1
    a = adc.read(0)
    v = v + a
  else
    sound = v/10
    print(sound)
    if send_once == 0 then
      if sound > 250 then
        -- Send an email
        send_email(
          "sound sensor",
          [[Hi,
          some sound level more than 250!
          BR,
          sound sensor]])      
      end      
    end
    x = 0
    v = 0
  end  
end)

base64.lua

-- Lua 5.1+ base64 v3.0 (c) 2009 by Alex Kloss <[email protected]>
-- licensed under the terms of the LGPL2

local moduleName = ... 
local M = {}
_G[moduleName] = M

-- character table string
local b='ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/'
 
-- encoding
function M.enc(data)
    return ((data:gsub('.', function(x) 
        local r,b='',x:byte()
        for i=8,1,-1 do r=r..(b%2^i-b%2^(i-1)>0 and '1' or '0') end
        return r;
    end)..'0000'):gsub('%d%d%d?%d?%d?%d?', function(x)
        if (#x < 6) then return '' end
        local c=0
        for i=1,6 do c=c+(x:sub(i,i)=='1' and 2^(6-i) or 0) end
        return b:sub(c+1,c+1)
    end)..({ '', '==', '=' })[#data%3+1])
end
 
-- decoding
function M.dec(data)
    data = string.gsub(data, '[^'..b..'=]', '')
    return (data:gsub('.', function(x)
        if (x == '=') then return '' end
        local r,f='',(b:find(x)-1)
        for i=6,1,-1 do r=r..(f%2^i-f%2^(i-1)>0 and '1' or '0') end
        return r;
    end):gsub('%d%d%d?%d?%d?%d?%d?%d?', function(x)
        if (#x ~= 8) then return '' end
        local c=0
        for i=1,8 do c=c+(x:sub(i,i)=='1' and 2^(7-i) or 0) end
        return string.char(c)
    end))
end

return M

morris 和 charlie 看得超開心的

還不錯
這種被需要的感覺

不過要注意不能影響到 m300 的進度

⚠️ **GitHub.com Fallback** ⚠️