Différences entre versions de « FEATHER-ESP8266-NodeMCU-Lua »
Ligne 4 : | Ligne 4 : | ||
== Préambule == | == Préambule == | ||
− | + | Chaque carte Feather HUZZAH ESP8266 est livré préprogrammé avec l'interpréteur Lua de NodeMCU. Au moment de la traduction de ce tuto, c'est la version '''NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4''' qui est distribué mais vous pourriez avoir une version plus récente. | |
− | + | L'interpréteur Lua s'exécute sur l'ESP8266 et vous pouvez saisir des commandes et lire le résultat via le port série. Pour pouvoir télécharger du code sur l'ESP8266 et utiliser la console série, connectez un câble micro-USB sur le Feather HUZZAH et sur le port USB de votre ordinateur. [https://www.silabs.com/products/mcu/Pages/USBtoUARTBridgeVCPDrivers.aspx Installez le pilote USB CP2104 pour voir apparaître le port USB/Série (et qu'il soit correctement accessible)] | |
− | [http://www.esp8266.com/ | + | [http://www.esp8266.com/ N'oubliez pas de visiter esp8266.com pour vous tenir au courant des dernières nouvelles/logiciel/etc sur l'ESP8266!] |
− | {{ambox-stop|text= | + | {{ambox-stop|text=N'oubliez pas d'installer le Pilote USB pour le convertisseur USB-vers-Serie CP2104! }} |
== Ouvrir une console série == | == Ouvrir une console série == |
Version du 5 mai 2016 à 14:46
En cours de traduction/élaboration. |
Préambule
Chaque carte Feather HUZZAH ESP8266 est livré préprogrammé avec l'interpréteur Lua de NodeMCU. Au moment de la traduction de ce tuto, c'est la version NodeMCU 0.9.5 build 20150318 powered by Lua 5.1.4 qui est distribué mais vous pourriez avoir une version plus récente.
L'interpréteur Lua s'exécute sur l'ESP8266 et vous pouvez saisir des commandes et lire le résultat via le port série. Pour pouvoir télécharger du code sur l'ESP8266 et utiliser la console série, connectez un câble micro-USB sur le Feather HUZZAH et sur le port USB de votre ordinateur. Installez le pilote USB CP2104 pour voir apparaître le port USB/Série (et qu'il soit correctement accessible)
N'oubliez pas d'installer le Pilote USB pour le convertisseur USB-vers-Serie CP2104! |
Ouvrir une console série
En cours de traduction/élaboration. |
Next up, on your computer, use a serial console program such as CoolTerm (Mac) or Putty (Windows) or screen (linux). Teraterm seems to dislike the initial 74400bps data stream from the ESP8266 so you can try it but you'll possibly need to reset the terminal software.
Connect up to the COM or Serial port used by your cable, at 9600 Baud
Make sure you have turned off any hardware handshake or flow control
Crédit: AdaFruit Industries www.adafruit.com
Once the terminal software is connected, click the Reset button on the Feather HUZZAH ESP8266 board to reset it and have it print out the welcome message:
Crédit: AdaFruit Industries www.adafruit.com
If you don't get this message, first check that the red/blue leds flickered when you press the reset button. If they didnt, make sure you've got the right baud rate selected in the software (9600)
Hello World
Ok we can now turn on an LED. There is a red LED on each board, connected to GPIO #0
NodeMCU's pinouts are not the same as the Arduino/gcc pinouts. We print the Arduino pinouts on the board so watch out! |
The Lua documentation for the ESP8266 has GPIO #4 and #5 swapped so if #4/#5 aren't working for you, try swapping! |
Pin Notes | PCB/Arduino | NodeMCU/Lua |
Pas de pullup! | 0 | 3 |
2 | 4 | |
3 | 9 | |
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:
gpio.mode(3, gpio.OUTPUT)
Turn the LED on with:
gpio.write(3, gpio.LOW)
Et eteindre la LED:
gpio.write(3, gpio.HIGH)
You can make this a little more automated by running:
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
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
wifi.setmode(wifi.STATION)
Then you can run the scanner and have it print out the available AP's
-- print ap list function listap(t) for k,v in pairs(t) do print(k.." : "..v) end end wifi.sta.getap(listap)
or for more detail...
-- 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)
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
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())
Exemple WebClient
Once you're got the IP address you can connect to adafruit, for example, and read a webpage and print it out:
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")
You can also have the module do DNS for you, just give it the hostname instead of IP address:
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")
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 for the details on what functions are available to you, as well as http://www.lua.org to learn more about the Lua scripting language
Source: Adafruit Feather ESP8266 créé par LadyAda pour AdaFruit Industries. Crédit [www.adafruit.com AdaFruit Industries]
Traduit par Meurisse D. pour MCHobby.be
Traduit avec l'autorisation d'AdaFruit Industries - Translated with the permission from Adafruit Industries - www.adafruit.com
Toute référence, mention ou extrait de cette traduction doit être explicitement accompagné du texte suivant : « Traduction par MCHobby (www.MCHobby.be) - Vente de kit et composants » avec un lien vers la source (donc cette page) et ce quelque soit le média utilisé.
L'utilisation commercial de la traduction (texte) et/ou réalisation, même partielle, pourrait être soumis à redevance. Dans tous les cas de figures, vous devez également obtenir l'accord du(des) détenteur initial des droits. Celui de MC Hobby s'arrêtant au travail de traduction proprement dit.