Modifications

Sauter à la navigation Sauter à la recherche
3 587 octets ajoutés ,  5 mai 2016 à 14:32
Ligne 38 : Ligne 38 :  
{{ambox|text=The Lua documentation for the ESP8266 has GPIO #4 and #5 swapped so if #4/#5 aren't working for you, try swapping!}}
 
{{ambox|text=The Lua documentation for the ESP8266 has GPIO #4 and #5 swapped so if #4/#5 aren't working for you, try swapping!}}
   −
{| {{table}}
+
{| class="wikitable" border="1"
| align="center" style="background:#f0f0f0;"|'''Pin Notes'''
   
|-
 
|-
| ||
+
| align="center" | Pin Notes
|-
+
| align="center" | PCB/Arduino
|
+
| align="center" | NodeMCU/Lua
|-
+
|- style="font-size: 90%"
| PCB/Arduino
+
| align="left" | Pas de pullup!
|-
+
| align="left" | 0
| ||
+
| align="left" | 3
|-
+
|- style="font-size: 90%"
|
+
| align="left" |
|-
+
| align="left" | 2
| NodeMCU/Lua
+
| align="left" | 4
|-
+
|- style="font-size: 90%"
|  
+
| align="left" |
|-
+
| align="left" | 3
| No pullups!
+
| align="left" | 9
|-
+
|- style="font-size: 90%"
| ||
+
| align="left" |
|-
+
| align="left" | 4
|
+
| align="left" | 1
|-
+
|- style="font-size: 90%"
| 0
+
| align="left" |
|-
+
| align="left" | 5
| ||
+
| align="left" | 2
|-
+
|- style="font-size: 90%"
|
+
| align="left" |
|-
+
| align="left" | 9
| 3
+
| align="left" | 11
|-
+
|- style="font-size: 90%"
| ||
+
| align="left" |
|-
+
| align="left" | 10
|  
+
| align="left" | 12
|-
+
|- style="font-size: 90%"
| 2
+
| align="left" |
|-
+
| align="left" | 12
| ||
+
| align="left" | 6
|-
+
|- style="font-size: 90%"
|
+
| align="left" |
|-
+
| align="left" | 13
| 4
+
| align="left" | 7
|-
+
|- style="font-size: 90%"
| ||
+
| align="left" |
|-
+
| align="left" | 14
|
+
| align="left" | 5
|-
+
|- style="font-size: 90%"
| 3
+
| align="left" |
|-
+
| align="left" | 15
| ||
+
| align="left" | 8
|-
+
|- style="font-size: 90%"
|
+
| align="left" |
|-
+
| align="left" | 16
| 9
+
| align="left" | 0
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 4
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 1
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 5
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 2
  −
|-
  −
| ||
  −
|-
  −
|  
  −
|-
  −
| 9
  −
|-
  −
| ||
  −
|-
  −
|  
  −
|-
  −
| 11
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 10
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 12
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 12
  −
|-
  −
| ||
  −
|-
  −
|  
  −
|-
  −
| 6
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 13
  −
|-
  −
| ||
  −
|-
  −
|  
  −
|-
  −
| 7
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 14
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 5
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 15
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 8
  −
|-
  −
| ||
  −
|-
  −
|  
  −
|-
  −
| 16
  −
|-
  −
| ||
  −
|-
  −
|
  −
|-
  −
| 0
   
|}
 
|}
    +
So to set the pin #0 LED on and off (which would be pin #3 in Lua) first make it an output:
 +
 +
<nowiki>gpio.mode(3, gpio.OUTPUT)</nowiki>
 +
 +
Turn the LED on with:
 +
 +
<nowiki>gpio.write(3, gpio.LOW)</nowiki>
 +
 +
Et eteindre la LED:
 +
 +
<nowiki>gpio.write(3, gpio.HIGH)</nowiki>
 +
 +
You can make this a little more automated by running:
 +
 +
<nowiki>while 1 do
 +
  gpio.write(3, gpio.HIGH)
 +
  tmr.delay(1000000)  -- wait 1,000,000 us = 1 second
 +
  gpio.write(3, gpio.LOW)
 +
  tmr.delay(1000000)  -- wait 1,000,000 us = 1 second
 +
end</nowiki>
 +
 +
 +
The LED will now be blinking on and off.
 +
 +
Note that since its in a loop, its not possible to get it to stop via the interpretter. To stop it, click the '''Reset''' button again!
 +
 +
== Scanner le réseau WiFi et connexion ==
 +
We'll continue with a quick demo of scanning for WiFi and connecting.
 +
 +
Once you're back at the Lua prompt, set the ESP8266 into WiFi Client mode with
 +
 +
<nowiki>wifi.setmode(wifi.STATION)</nowiki>
 +
 +
Then you can run the scanner and have it print out the available AP's
 +
 +
<nowiki>-- print ap list
 +
function listap(t)
 +
      for k,v in pairs(t) do
 +
        print(k.." : "..v)
 +
      end
 +
end
 +
wifi.sta.getap(listap)</nowiki>
 +
 +
or for more detail...
 +
 +
<nowiki>-- print ap list
 +
function listap(t)
 +
      for ssid,v in pairs(t) do
 +
        authmode, rssi, bssid, channel = string.match(v, "(%d),(-?%d+),(%x%x:%x%x:%x%x:%x%x:%x%x:%x%x),(%d+)")
 +
        print(ssid,authmode,rssi,bssid,channel)
 +
      end
 +
end
 +
     
 +
wifi.sta.getap(listap)</nowiki>
 +
 +
We can connect to the access point with '''wifi.sta.config''' and '''wifi.sta.connect''' - it will take a second or two to complete the connection, you can query the module to ask the status with '''wifi.sta.status()''' - when you get a 5 it means the connection is completed and DHCP successful
 +
 +
<nowiki>wifi.sta.config("accesspointname","yourpassword")
 +
wifi.sta.connect()
 +
tmr.delay(1000000)  -- wait 1,000,000 us = 1 second
 +
print(wifi.sta.status())
 +
print(wifi.sta.getip())</nowiki>
 +
 +
== Exemple WebClient ==
 +
 +
Once you're got the IP address you can connect to adafruit, for example, and read a webpage and print it out:
 +
 +
<nowiki>sk=net.createConnection(net.TCP, 0)
 +
sk:on("receive", function(sck, c) print(c) end )
 +
sk:connect(80,"207.58.139.247")
 +
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost: www.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")
 +
</nowiki>
 +
 +
You can also have the module do DNS for you, just give it the hostname instead of IP address:
 +
 +
<nowiki>sk=net.createConnection(net.TCP, 0)
 +
sk:on("receive", function(sck, c) print(c) end )
 +
sk:connect(80,"www.adafruit.com")
 +
sk:send("GET /testwifi/index.html HTTP/1.1\r\nHost: www.adafruit.com\r\nConnection: keep-alive\r\nAccept: */*\r\n\r\n")</nowiki>
 +
 +
This is just a light overview of testing out your HUZZAH ESP breakout! For much more, check out NodeMCU's tutorial page [https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en https://github.com/nodemcu/nodemcu-firmware/wiki/nodemcu_api_en] for the details on what functions are available to you, as well as  [http://www.lua.org/ http://www.lua.org] to learn more about the Lua scripting language
 
{{FEATHER-ESP8266-TRAILER}}
 
{{FEATHER-ESP8266-TRAILER}}
29 917

modifications

Menu de navigation