ESP8266 Arduino

From ElectroDragon
(Redirected from ESP8266 arduino setup)
Jump to: navigation, search


Install in Arduino IDE

Installation

Supported Board Supported Board

  • Adafruit HUZZAH ESP8266 (ESP-12)
  • NodeMCU 0.9
  • NodeMCU 1.0
  • Olimex MOD-WIFI-ESP8266-DEV
  • Olimex MOD-WIFI-ESP8266
  • SparkFun ESP8266 Thing
  • SweetPea ESP-210
  • Generic ESP8266 modules

Board Config

Board CPU frequency Flash frequency Flash size upload speed Flash mode Debug mode / level Reset method
ESP-12F, ESP-12S 80mhz 40mhz 4M (3M SPIFFS) 921600 DIO Disable / none nodemcu

Code Examples

  • Choose correct board
  • enter into bootloader mode of the board first
Esp8266 arduino selection.png

Examples Sketchs

  • WifiScan - find close by wifi spot
  • WifiClient - connect to wifi
  • Wifiwebserver - build a simple web server
http://server_ip/gpio/0 - address to set gpio 2 to low
http://server_ip/gpio/1 - address to set gpio 2 to high


Demo code - Blink esp on board io12 by at-commands

#include <SoftwareSerial.h>
 
const byte rxPin = 3; // Wire this to Tx Pin of ESP8266
const byte txPin = 2; // Wire this to Rx Pin of ESP8266
 
// We'll use a software serial interface to connect to ESP8266
SoftwareSerial ESP8266 (rxPin, txPin);
 
void setup() {
  Serial.begin(115200);
  ESP8266.begin(115200); // Change this to the baudrate used by ESP8266
  delay(1000); // Let the module self-initialize
  pinMode(12, OUTPUT);  // set the io pin 16 to output
}
 
void loop() {
  Serial.println("Sending an AT command...");
  ESP8266.println("AT+CIOWRITE=12,1");
  delay(30);
  while (ESP8266.available()){
     String inData = ESP8266.readStringUntil('\n');
     Serial.println("Got reponse from ESP8266: " + inData);
  } 
  delay(1000);
  ESP8266.println("AT+CIOWRITE=12,0");
  delay(30);
  while (ESP8266.available()){
     String inData = ESP8266.readStringUntil('\n');
     Serial.println("Got reponse from ESP8266: " + inData);
  }
  delay(1000); 
}

Demo code - Connect WIFI, Send Data

  • In this case, the wifi module still connect to hardware serial (software serial port can not higher than 19200 baud rate), and another software serial port should be created on arduino and print out via another serial port
  • So the connection should be
Wifi's uart to arduino hardware uart; 
arduino's software UART to another serial port device, for example like FTDI basic, CP2102 breakout, etc, and this serial port device can connect to PC to read data
  • change the SSID and password in code for your wifi router
 
#include <SoftwareSerial.h>
#define SSID        "xxxxxxxx"
#define PASS        "xxxxxxxx"
#define DST_IP      "220.181.111.85"    //baidu.com
SoftwareSerial dbgSerial(10, 11); // RX, TX
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  Serial.setTimeout(5000);
  dbgSerial.begin(9600);  //can't be faster than 19200 for softserial
  dbgSerial.println("ESP8266 Demo");
  //test if the module is ready
  Serial.println("AT+RST");
  delay(1000);
  if (Serial.find("ready"))
  {
    dbgSerial.println("Module is ready");
  }
  else
  {
    dbgSerial.println("Module have no response.");
    while (1);
  }
  delay(1000);
  //connect to the wifi
  boolean connected = false;
  for (int i = 0; i < 5; i++)
  {
    if (connectWiFi())
    {
      connected = true;
      break;
    }
  }
  if (!connected) {
    while (1);
  }
  delay(5000);
  //print the ip addr
  /*Serial.println("AT+CIFSR");
  dbgSerial.println("ip address:");
  while (Serial.available())
    dbgSerial.write(Serial.read());*/
  //set the single connection mode
  Serial.println("AT+CIPMUX=0");
}
void loop()
{
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += DST_IP;
  cmd += "\",80";
  Serial.println(cmd);
  dbgSerial.println(cmd);
  if (Serial.find("Error")) return;
  cmd = "GET / HTTP/1.0\r\n\r\n";
  Serial.print("AT+CIPSEND=");
  Serial.println(cmd.length());
  if (Serial.find(">"))
  {
    dbgSerial.print(">");
  } else
  {
    Serial.println("AT+CIPCLOSE");
    dbgSerial.println("connect timeout");
    delay(1000);
    return;
  }
  Serial.print(cmd);
  delay(2000);
  //Serial.find("+IPD");
  while (Serial.available())
  {
    char c = Serial.read();
    dbgSerial.write(c);
    if (c == '\r') dbgSerial.print('\n');
  }
  dbgSerial.println("====");
  delay(1000);
}
boolean connectWiFi()
{
  Serial.println("AT+CWMODE=1");
  String cmd = "AT+CWJAP=\"";
  cmd += SSID;
  cmd += "\",\"";
  cmd += PASS;
  cmd += "\"";
  dbgSerial.println(cmd);
  Serial.println(cmd);
  delay(2000);
  if (Serial.find("OK"))
  {
    dbgSerial.println("OK, Connected to WiFi.");
    return true;
  } else
  {
    dbgSerial.println("Can not connect to the WiFi.");
    return false;
  }
}

Arduino Serial Extension Shield Configuration

  • Flash -> ON | OFF -> Set flash mode, flash LED will turn on
  • Arduino Serial -> D1/D0 | D3/D2 - Arduino serial switch between softserial or hardware serial
  • Serial Switch -> Arduino or standalone output


Common Used Arduino ESP8266 Library