Wednesday, March 30, 2016

Run diagnosis of NodeMCU/ESP8266


To run diagnosis of NodeMCU/ESP8266, we can call the function WiFi.printDiag(Serial), pass with Serial port as the parameter, it will send the diagnostic info to Serial port.

remark: this apply on NodeMCU (suppose on other standalone ESP8266 also) with ESP8266 core for Arduino.

Example:
#include <ESP8266WiFi.h>

const char* ssid = "arduino-er";
const char* password = "12345678";
byte mac[6];

void setup() {
  Serial.begin(115200);

  Serial.print("\nRun diagnostic...\n");
  WiFi.printDiag(Serial);
  Serial.println();
  
  Serial.print("\nStart...\n");
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[0],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.println(mac[5],HEX);
  Serial.println();

  WiFi.begin(ssid, password);
  Serial.print("Connecting WIFI ");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("connected");  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:

}

sample output:

Thursday, March 24, 2016

NodeMCU exercise: get my IP address

NodeMCU (ESP8266) + ESP8266 core for Arduino example to get my IP address after connected to WiFi.


#include <ESP8266WiFi.h>

const char* ssid = "your WiFi ssid";
const char* password = "xxxxxxxx";
byte mac[6];

void setup() {
  Serial.begin(115200);

  Serial.print("\nStart...\n");
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[0],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.println(mac[5],HEX);
  Serial.println();
  
  WiFi.begin(ssid, password);
  Serial.print("Connecting WIFI ");
  
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
 
  Serial.println("");
  Serial.println("connected");  
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
}

void loop() {
  // put your main code here, to run repeatedly:

}

Tuesday, March 22, 2016

Read NodeMCU MAC address using Arduino IDE with esp8266 library

With esp8266 board installed to Arduino IDE, we can program NodeMCU using Arduino IDE directly.

Here is a example to read the MAC address of NodeMCU. Because we are going to read MAC only, so no need to begin WiFi and connect to any network actually.

NodeMCU_getMAC.ino
#include <ESP8266WiFi.h>

//const char* ssid = "ssid";
//const char* password = "password";
byte mac[6];

void setup() {
  Serial.begin(9600);
  //WiFi.begin(ssid, password);

  Serial.print("\nStart...\n");
  WiFi.macAddress(mac);
  Serial.print("MAC: ");
  Serial.print(mac[0],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.println(mac[5],HEX);
}

void loop() {
  // put your main code here, to run repeatedly:

}


Next:
- Get my IP address

Sunday, March 20, 2016

Blink NodeMCU on-board LED using Arduino IDE with ESP8266 core for Arduino, and more examples


To program NodeMCU in Arduino IDE, we have to install esp8266 board (ESP8266 core for Arduino) to Arduino IDE.

Add Additional Board Manager URL for ESP8266 board:
> File > Preference

Add "http://arduino.esp8266.com/stable/package_esp8266com_index.json" in Additional Board Manager URLs.


Add ESP8266 board to Arduino IDE:
- Open Boards Manager in Arduino IDE
- Search "esp8266" or "NodeMCU", you will find "esp8266 by ESP8266 Community". Install it.


Test:
Once esp8266 board installed, you can find an example to blink the on-board LED.
File > Examples > ESP8266 > Blink

/*
 ESP8266 Blink by Simon Peter
 Blink the blue LED on the ESP-01 module
 This example code is in the public domain
 
 The blue LED on the ESP-01 module is connected to GPIO1 
 (which is also the TXD pin; so we cannot use Serial.print() at the same time)
 
 Note that this sketch uses LED_BUILTIN to find the pin with the internal LED
*/

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);     // Initialize the LED_BUILTIN pin as an output
}

// the loop function runs over and over again forever
void loop() {
  digitalWrite(LED_BUILTIN, LOW);   // Turn the LED on (Note that LOW is the voltage level
                                    // but actually the LED is on; this is because 
                                    // it is acive low on the ESP-01)
  delay(1000);                      // Wait for a second
  digitalWrite(LED_BUILTIN, HIGH);  // Turn the LED off by making the voltage HIGH
  delay(2000);                      // Wait for two seconds (to demonstrate the active low LED)
}

You can upload it to NodeMCU, to toggle the on-board LED.


notice:
- Once the ModeMCU programmed, the original firmware will be erased. To restore the original firmware with Lua shell, you have to flash the firmware again.

Control GPIO (external IO pins) of NodeMCU (ESP8266) with ESP8266 core for Arduino

Next:
Read NodeMCU MAC address using Arduino IDE with esp8266 library
Get my IP address
Run diagnosis
NodeMCU to read analog input, A0
NodeMCU act as WiFi client to update dweet.io
Display on 128x64 I2C OLED, using Adafruit SSD1306 and GFX libraries
esp8266-OLED, another esp8266-Arduino library for I2C-OLED displays
NodeMCU/ESP8266 act as AP (Access Point) and simplest Web Server
NodeMCU/ESP8266 act as AP (Access Point) and web server to control GPIO
NodeMCU/ESP8266 implement WebSocketsServer to control RGB LED
NodeMCU/ESP8266 WebSocketsServer, load html from separate file in flash file system
Read Buttons and write LEDs
NodeMCU/ESP8266 Arduino Core analog output PWM
Simple http server to output PWM, to set color/brightness of RGB LED
Install Arduino IDE on Raspberry Pi/Raspbian Jessie with PIXEL and Add Arduino core for ESP8266 to Arduino IDE (run on Raspberry Pi/Raspbian Jessie with PIXEL)

Saturday, March 19, 2016

Test NodeMCU onboard LED in Lua shell

Once flashed update firmware to NodeMCU, we can test the onboard LED in Lua shell.


- Connect NodeMCU and run PuTTY to enter its Lua shell.
- Enter this code (in red) to test the onboard LED:

NodeMCU 0.9.6 build 20150704  powered by Lua 5.1.4
lua: cannot open init.lua
> gpio.mode(0, gpio.OUTPUT)
> gpio.write(0, gpio.HIGH)
> print(gpio.read(0))
1
> gpio.write(0, gpio.LOW)
> print(gpio.read(0))
0
>



This video show how to:


First run NodeMCU and flash firmware


The below videos show first run NodeMCU in Windows 10, download and flash update firmware.

This video show how to find the NodeMCU connected port, and connect it with PuTTY.


Most probably, NodeMCU ship WITHOUT update firmware. This video show how to download and flash firmware on NodeMCU. To download firmware and flasher, visit https://github.com/nodemcu, and follow the steps shown in the video.
(May be you have to press and hold the on-board FLASH button, then click the Flash button of ESP8266Flasher, once flash started, you can release on-board FLASH button.)


If everything OK, you should able to connect NodeMCU with PuTTY, and enter Lua shell.
(If NodeMCU have no response in PuTTY terminal, try to press the RST button on NodeMCU.)




Next:
Test NodeMCU onboard LED in Lua shell