2015年4月1日 星期三

NodeMCU固件- node/wifi/net/uart module



參考連結: NodeMCU API指令 


node module

node.led  --deprecated
node.key --deprecated
node.readvdd33 --deprecated

node.input
node.output
node.compile
node.info
node.chipid
node.flashid
node.heap
node.restart

> print(node.info())
0 9 6 10492404 1458415 4096 2 40000000
==>SDK Version : 0.9.6
==>chipid : 10492494
==>flashid:1458415  (0x1640EF) 0xEF代表Winbond , 0x4016對應32Mbits
==>flashsize=4096 KBytes
==>flashmode=2 (DIO)

==>flashspeed=40000000 (40MHz)


http://code.coreboot.org/svn/flashrom/trunk/flashchips.h

::1458400 (0x1640E0) => 32Mbits
::1458415 (0x1640EF) => 32Mbits
#define WINBOND_NEX_ID  0xEF /* Winbond (ex Nexcom) serial flashes */
#define WINBOND_NEX_W25Q32_V 0x4016 /* W25Q32BV; W25Q32FV in SPI mode (default) */

::1327343 (0x1440EF) => 8Mbits
#define WINBOND_NEX_W25Q80_V 0x4014 /* W25Q80BV */



::1261768 (0x1340C8) => 4Mbits
#define GIGADEVICE_ID  0xC8 /* GigaDevice */
#define GIGADEVICE_GD25Q40 0x4013 /* Same as GD25QB */




> print(node.heap())
34288
>
==>剩餘可用memory : 34288 Bytes

wifi module

wifi.getmode
wifi.setmode
wifi.startsmart
wifi.stopsmart
wifi.sleeptype

wifi.sta module

wifi.sta.config
wifi.sta.connect
wifi.sta.disconnect
wifi.sta.autoconnect
wifi.sta.getip
wifi.sta.getmac
wifi.sta.setip
wifi.sta.getap : scan and get AP list
wifi.sta.status
wifi.stat.getbroadcast

wif.iap module

wifi.ap.config
wifi.ap.getip
wifi.ap.setip
wifi.ap.getmac
wifi.ap.setmac
wifi.ap.getbroadcast


uart module

baud = 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 74880, 115200, 230400, 460800, 921600, 1843200, 2686400. 
心得: Baud Rate可以儘量設高點,太低有時Lua command出錯"
> print("Heap=".. node.heap())
tmr.delay(1000)
print(no>> de.info())

stdin:2: ')' expected (to close '(' at line 1) near 'de'
由上看出,若輸出不夠快則輸入值會影響輸輸入,此應該是Lua的Bug吧!!!!!????(待查證)

註: 各廠USB-Serial不見得支援所有Baud Rate, 例如PL2303 在Ubuntu12.04下不支援 18432002686400,不知是硬體還是軟體限制? 
補充: 同樣Ubuntu環境用FT232RL可設達2686400, 貴一點還是值得的......
補充: 同樣Ubuntu環境用CH341不支援 1843200 及2686400
setup uart  in init.lua 
>print(uart.setup(0,921600,8,0,1,1))
921600

uart.setup
uart.on
uart.write

https://github.com/nodemcu/nodemcu-firmware/issues/15
https://github.com/nodemcu/nodemcu-firmware/issues/57

等待資料接收,最大可設255 Bytes, 下面範例為接收4個bytes

uart.on("data", 4, 
      function(data)
        print("receive from uart:", data)
        if data=="quit" then 
          uart.on("data") 
        end        
    end, 0)
    -- when '\r' is received.
    uart.on("data", "\r", 
      function(data)
        print("receive from uart:", data)
        if data=="quit\r" then 
          uart.on("data") 
        end        
    end, 0)












net module

net.createServer
net.createConnection

net.server module
net.server:listen
net.server.stop

net.socket module
net.socket:connect
net.socket.send
net.socket.on
net.socket.close
net.socket.dns


----------------------------------------------------------------------------------------------------------------

>wifi.setmode(wifi.SOFTAP)
>print(wifi.getmode())
2
>print(node.chipid())
10393897
>print("ESP_"..string.format("%X",node.chipid()))
ESP_9E9929
>print(wifi.ap.getip())
192.168.4.1 255.255.255.0 192.168.4.1
>print(wifi.ap.getbroadcast())
192.168.4.255



NodeMCU 的SoftAp  Name= ESP_XXXXXX 聯入(XXXXXX表示ChipID) , 預設:不需密碼

NodeMCU硬件- NodeMCU Devkit



參考資料來源:
https://github.com/nodemcu/nodemcu-devkit


Devkit 0.9與1.0腳位定義有差異,主因是因為ESP-12E多了6支引腳




Devkit 0.9 : ESP-12 + CH340G
DEVKIT


SCH



Devkit 1.0 : ESP-12E + CP2102









https://raw.githubusercontent.com/nodemcu/nodemcu-devkit/master/Documents/NODEMCU_DEVKIT_SCH.png


node.key : --deprecated
node.led  : --deprecated
node.readvdd33 : --deprecated, moved to adc.readvdd33()


node.dsleep()

描述

进入睡眠模式,计时时间之后唤醒

语法

node.dsleep(us)

-注意: 如需使用此功能,需要将esp8266的PIN32(RST)和PIN8(XPD_DCDC)短接。


物聯網開放平台 - QQ物聯

ESP8266 開發相關連結


資料連結:

ESP8266 官網
ESP8266 BBS
SDK Download
Espressif GitHub
Unofficial Espressif GitHub

ESP8266 GitHub

NodeMCU 官網
NodeMCU BBS
NodeMCU GitHub

ai-thinker論壇
AI-THINKER SDK

http://www.esp8266.com/index.php

Espruino - ESP8266


http://bbs.e4asoft.com/

http://www.nodemcu.com/docs/node-module/

https://github.com/pfalcon/esp-open-sdk



NodeMCU固件- i2C/SPI/Uart/1-wire控制


參考連結: NodeMCU API指令


https://github.com/nodemcu/nodemcu-firmware/tree/84a9ab35a8eb9b4ae2229bba7b2626c2a285818d/app/driver



I2C
    id=0
    sda=1
    scl=2

    -- initialize i2c, set pin1 as sda, set pin2 as scl
    i2c.setup(id,sda,scl,i2c.SLOW)

    -- user defined function: read from reg_addr content of dev_addr
    function read_reg(dev_addr, reg_addr)
      i2c.start(id)
      i2c.address(id, dev_addr ,i2c.TRANSMITTER)
      i2c.write(id,reg_addr)
      i2c.stop(id)
      i2c.start(id)
      i2c.address(id, dev_addr,i2c.RECEIVER)
      c=i2c.read(id,1)
      i2c.stop(id)
      return c
    end

    -- get content of register 0xAA of device 0x77
    reg = read_reg(0x77, 0xAA)
    print(string.byte(reg))



UART
    -- when 4 chars is received.
    uart.on("data", 4, 
      function(data)
        print("receive from uart:", data)
        if data=="quit" then 
          uart.on("data") 
        end        
    end, 0)
    -- when '\r' is received.
    uart.on("data", "\r", 
      function(data)
        print("receive from uart:", data)
        if data=="quit\r" then 
          uart.on("data") 
        end        
    end, 0)


1-Wire
-- 18b20 Example
pin = 9
ow.setup(pin)
count = 0
repeat
  count = count + 1
  addr = ow.reset_search(pin)
  addr = ow.search(pin)
  tmr.wdclr()
until((addr ~= nil) or (count > 100))
if (addr == nil) then
  print("No more addresses.")
else
  print(addr:byte(1,8))
  crc = ow.crc8(string.sub(addr,1,7))
  if (crc == addr:byte(8)) then
    if ((addr:byte(1) == 0x10) or (addr:byte(1) == 0x28)) then
      print("Device is a DS18S20 family device.")
        repeat
          ow.reset(pin)
          ow.select(pin, addr)
          ow.write(pin, 0x44, 1)
          tmr.delay(1000000)
          present = ow.reset(pin)
          ow.select(pin, addr)
          ow.write(pin,0xBE,1)
          print("P="..present)  
          data = nil
          data = string.char(ow.read(pin))
          for i = 1, 8 do
            data = data .. string.char(ow.read(pin))
          end
          print(data:byte(1,9))
          crc = ow.crc8(string.sub(data,1,8))
          print("CRC="..crc)
          if (crc == data:byte(9)) then
             t = (data:byte(1) + data:byte(2) * 256) * 625
             t1 = t / 10000
             t2 = t % 10000
             print("Temperature="..t1.."."..t2.."Centigrade")
          end                   
          tmr.wdclr()
        until false
    else
      print("Device family is not recognized.")
    end
  else
    print("CRC is not valid!")
  end
end

SPI
id: spi id number.
mode: MASTER or SLAVE(not supported yet).
cpol: CPOL_LOW or CPOL_HIGH, clock polarity.
cpha: CPHA_HIGH or CPHA_LOW, clock phase.
databits: DATABITS_8 or DATABITS_16.
clock: spi clock (not supported yet).


物聯網開放平台- Thingspeak



相關連結:

ThingSpeak官網
GitHub - ThingSpeak
https://thingspeak.com/docs/tutorials/
https://thingspeak.com/docs
http://community.thingspeak.com/forum/

ThingSpeak

ThingSpeak is an open source “Internet of Things” application and API to store and retrieve data from things using HTTP over the Internet or via a Local Area Network. With ThingSpeak, you can create sensor logging applications, location tracking applications, and a social network of things with status updates.

Quick Start
  1. Sign Up for a ThingSpeak account
  2. Create a new Channel by going to your Channels page and clicking Create New Channel
  3. Update your Channel via URL:
    https://api.thingspeak.com/update?api_key=YOUR_CHANNEL_API_KEY&field1=7
  4. View your Channel feed:
    https://api.thingspeak.com/channels/YOUR_CHANNEL_ID/feeds.json
  5. Follow a tutorial for common devices and applications


物聯網開放平台- lewei50 (樂為物聯)


樂聯網 - https://www.lewei50.com/


乐联网平台操作指南

1.1       乐联网简介
乐联网(http://open.lewei50.com),通过开放的API,可以让您轻松的将各类传感器、测量设备或工业仪器仪表接入该平台,并可以通过开发应用来监管和控制它们。我们的平台为您提供传感器云服务,让您无需繁琐的编程,便可以将自己的各类测量或控制设备实现网络功能,快速开启物联网应用。而工业仪器仪表的无缝接入和强大的数据存储、查询和分析能力可以帮助一些传统行业快速部署自己的物联网应用,实现传统企业向物联网企业转型。
乐联网的主要功能有个人门户功能,数据存储和分析(结合行业专家可延展为专家系统),工业仪表无缝接入和手机App功能。


Open Data
Big Data


透個建立設備,及添加Sensor及Controller ,我們可以用雲端物聯平台進行遠端監控及資料收集及分析.





IOT可定時向Lewei50 樂聯網發送資料,內容如下:(溫溼度數據發送)

"
POST /api/V1/gateway/UpdateSensors/02 HTTP/1.1
Host: www.lewei50.com
Content-Length: ?????
userkey: 这里写入自己的KEY文件

[{"Name":"T","Value":30},{"Name":"H","Value": 40%}]
"


数据查看:https://www.lewei50.com/u/g/8646