Difference between revisions of "Arduino Environment and Esp8266"
m (Daniel moved page Arduino Enfironment and Esp8266 to Arduino Environment and Esp8266) |
|||
(8 intermediate revisions by the same user not shown) | |||
Line 2: | Line 2: | ||
Arduino IDE 1.6.x or newer is needed (I tried it on version 1.6.8). | Arduino IDE 1.6.x or newer is needed (I tried it on version 1.6.8). | ||
− | + | * Open the Arduino IDE and go to "File -> Preferences" | |
− | + | ||
− | + | [[File:esp8266_arduino_00.png]] | |
+ | |||
+ | * In the field "Additional Boards Manager URLs" introduce <code>http://arduino.esp8266.com/stable/package_esp8266com_index.json</code> and click OK. | ||
+ | |||
+ | [[File:esp8266_arduino_01.png]] | ||
+ | |||
+ | * Then go to "Tools -> Boards -> Boards Manager" and in the box "Type" select “Contributed”. | ||
+ | |||
+ | [[File:esp8266_arduino_02.png]] | ||
+ | |||
+ | * Select the “esp8266 by ESP8266 Community” package and click the “Install” button. | ||
+ | |||
+ | [[File:esp8266_arduino_03.png]] | ||
+ | |||
+ | * Now you can select the board for developing from "Tools -> Boards->ESP8266 Modules" and choose the board you want to program. | ||
+ | |||
+ | [[File:esp8266_arduino_04.png]] | ||
+ | |||
+ | = Test Programs = | ||
+ | |||
+ | == Blink == | ||
+ | This example makes the led attached to pin 16 (D0 of the NodeMCU 1.0 board) blink and prints the number of iterations done in the serial terminal. | ||
+ | For the pin mapping see [[NodeMCU#Pin_Mapping_for_Arduino_IDE]] | ||
+ | |||
+ | <syntaxhighlight lang="C" line="0" > | ||
+ | int pin = 16; // D0 pin in NodeMCU | ||
+ | int i = 0; | ||
+ | |||
+ | // the setup function runs once when you press reset or power the board | ||
+ | void setup() { | ||
+ | Serial.begin(115200); | ||
+ | |||
+ | // initialize digital pin 16 (D0) as an output. | ||
+ | pinMode(pin, OUTPUT); | ||
+ | } | ||
+ | |||
+ | // the loop function runs over and over again forever | ||
+ | void loop() { | ||
+ | Serial.print("Line "); | ||
+ | Serial.println(i); | ||
+ | i++; | ||
+ | digitalWrite(pin, HIGH); // turn the LED off by making the voltage HIGH | ||
+ | delay(100); // wait for a second | ||
+ | digitalWrite(pin, LOW); // turn the LED on by making the voltage LOW | ||
+ | delay(900); // wait for a second | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == Connecting to Wifi Access Point == | ||
+ | |||
+ | <syntaxhighlight lang="C" line="1"> | ||
+ | #include <ESP8266WiFi.h> | ||
+ | |||
+ | const char* SSID_NAME = "..." // WiFi SSID Name | ||
+ | const char* SSID_PASS = "..." // WiFi SSID Password | ||
+ | |||
+ | void setup() { | ||
+ | ... | ||
+ | |||
+ | waitMaxWifi = 0; | ||
+ | WiFi.mode(WIFI_STA); | ||
+ | WiFi.begin(SSID_NAME, SSID_PASS); | ||
+ | |||
+ | while (WiFi.status() != WL_CONNECTED && waitMaxWifi < 30) { | ||
+ | delay(1000); // Wait or do something useful | ||
+ | } | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | ... | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | == ESP8266 as Access Point == | ||
+ | |||
+ | <syntaxhighlight lang="C" line="1"> | ||
+ | #include <ESP8266WiFi.h> | ||
+ | |||
+ | const char* SSID_NAME = "ESP8266AP"; // WiFi AP SSID Name | ||
+ | const char* SSID_PASS = "0123456789"; // WiFi AP SSID Password | ||
+ | |||
+ | void setup() { | ||
+ | ... | ||
+ | |||
+ | WiFi.mode(WIFI_AP); | ||
+ | WiFi.softAP(SSID_NAME, SSID_PASS); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | ... | ||
+ | } | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | == Timers == | ||
+ | |||
+ | === Timer 0 Example === | ||
+ | |||
+ | <syntaxhighlight lang="C" line="0"> | ||
+ | void timer0_ISR (void){ | ||
+ | // Do some work | ||
+ | ... | ||
+ | // Set-up the next interrupt cycle | ||
+ | timer0_write(ESP.getCycleCount() + 80000000); //80Mhz -> 80*10^6 = 1 second | ||
+ | } | ||
+ | |||
+ | void setup() { | ||
+ | ... | ||
+ | |||
+ | noInterrupts(); | ||
+ | timer0_isr_init(); | ||
+ | timer0_attachInterrupt(timer0_ISR); | ||
+ | timer0_write(ESP.getCycleCount() + 80000000); //80Mhz -> 80*10^6 = 1 second | ||
+ | interrupts(); | ||
+ | } | ||
+ | |||
+ | void loop() { | ||
+ | ... | ||
+ | } | ||
+ | </syntaxhighlight> |
Latest revision as of 11:00, 12 May 2016
Contents
Installation
Arduino IDE 1.6.x or newer is needed (I tried it on version 1.6.8).
- Open the Arduino IDE and go to "File -> Preferences"
- In the field "Additional Boards Manager URLs" introduce
http://arduino.esp8266.com/stable/package_esp8266com_index.json
and click OK.
- Then go to "Tools -> Boards -> Boards Manager" and in the box "Type" select “Contributed”.
- Select the “esp8266 by ESP8266 Community” package and click the “Install” button.
- Now you can select the board for developing from "Tools -> Boards->ESP8266 Modules" and choose the board you want to program.
Test Programs
Blink
This example makes the led attached to pin 16 (D0 of the NodeMCU 1.0 board) blink and prints the number of iterations done in the serial terminal. For the pin mapping see NodeMCU#Pin_Mapping_for_Arduino_IDE
1 int pin = 16; // D0 pin in NodeMCU
2 int i = 0;
3
4 // the setup function runs once when you press reset or power the board
5 void setup() {
6 Serial.begin(115200);
7
8 // initialize digital pin 16 (D0) as an output.
9 pinMode(pin, OUTPUT);
10 }
11
12 // the loop function runs over and over again forever
13 void loop() {
14 Serial.print("Line ");
15 Serial.println(i);
16 i++;
17 digitalWrite(pin, HIGH); // turn the LED off by making the voltage HIGH
18 delay(100); // wait for a second
19 digitalWrite(pin, LOW); // turn the LED on by making the voltage LOW
20 delay(900); // wait for a second
21 }
Connecting to Wifi Access Point
1 #include <ESP8266WiFi.h>
2
3 const char* SSID_NAME = "..." // WiFi SSID Name
4 const char* SSID_PASS = "..." // WiFi SSID Password
5
6 void setup() {
7 ...
8
9 waitMaxWifi = 0;
10 WiFi.mode(WIFI_STA);
11 WiFi.begin(SSID_NAME, SSID_PASS);
12
13 while (WiFi.status() != WL_CONNECTED && waitMaxWifi < 30) {
14 delay(1000); // Wait or do something useful
15 }
16 }
17
18 void loop() {
19 ...
20 }
ESP8266 as Access Point
1 #include <ESP8266WiFi.h>
2
3 const char* SSID_NAME = "ESP8266AP"; // WiFi AP SSID Name
4 const char* SSID_PASS = "0123456789"; // WiFi AP SSID Password
5
6 void setup() {
7 ...
8
9 WiFi.mode(WIFI_AP);
10 WiFi.softAP(SSID_NAME, SSID_PASS);
11 }
12
13 void loop() {
14 ...
15 }
Timers
Timer 0 Example
1 void timer0_ISR (void){
2 // Do some work
3 ...
4 // Set-up the next interrupt cycle
5 timer0_write(ESP.getCycleCount() + 80000000); //80Mhz -> 80*10^6 = 1 second
6 }
7
8 void setup() {
9 ...
10
11 noInterrupts();
12 timer0_isr_init();
13 timer0_attachInterrupt(timer0_ISR);
14 timer0_write(ESP.getCycleCount() + 80000000); //80Mhz -> 80*10^6 = 1 second
15 interrupts();
16 }
17
18 void loop() {
19 ...
20 }