ESP8266: Difference between revisions

From Medien Wiki
mNo edit summary
mNo edit summary
Line 70: Line 70:
<source lang="c">AT+UART=9600,8,1,0,0</source>
<source lang="c">AT+UART=9600,8,1,0,0</source>
Sets the ESP8266 to 9600 baud, 8 databits, 1 stop bit no parity bit and no flow control.
Sets the ESP8266 to 9600 baud, 8 databits, 1 stop bit no parity bit and no flow control.
<source lang="c">
/*
  Software serial multple serial test
Receives from the hardware serial, sends to software serial.
Receives from software serial, sends to hardware serial.
The circuit:
* RX is digital pin 10 (connect to TX of other device)
* TX is digital pin 11 (connect to RX of other device)
Note:
Not all pins on the Mega and Mega 2560 support change interrupts,
so only the following can be used for RX:
10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69
Not all pins on the Leonardo support change interrupts,
so only the following can be used for RX:
8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).
created back in the mists of time
modified 25 May 2012
by Tom Igoe
based on Mikal Hart's example
This example code is in the public domain.
*/
#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }
  Serial.println("Goodnight moon!");
  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT+RST");
}
void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}
</source>

Revision as of 15:45, 3 May 2016

Testing and using a ESP8266 board

Electronic Setup

http://www.uni-weimar.de/medien/wiki/images/Esp-201-cheatsheet.svg

Supply the board with 3.3V on VCC, best run it with a voltage source or regulator that can at least deliver 300mA (many arduino boards have 3.3V regulators on board, but they deliver less than 300mA). Initial testes just with a ftdi usb to serial converters show unstable wifi connections, sometimes even board startup is impossible (the esp8266 board draws too much current).

Testing the Board with AT Commands

full reference of all AT commands: http://bbs.espressif.com/download/file.php?id=256

first steps:

AT+RST - resets the board:
ATE1 (verbose)
ATE0 (non verbose)

pre-setup to either join a network (station mode) or create a new network acces point (AP)

AT+CWMODE=1 station mode (join an existing wifi network)
AT+CWMODE=2 access point mode (create a new wifi network)
AT+CWMODE=3 access point mode + station mode (create a wifi and join an other one at the same time)

select a network around you and join it:

AT+CWLAP list available networks around you that you could join.
AT+CWJAP="SSID","Your Password" (join a network with the ssid (visible name) and your wifi password)

or if you don't want to join an existing network, create your own:

AT+CWSAP_DEF="GoAway!!!","myAss!!!",1,0 (creates a wifi with the name GoAway!!! and an optional password: myAss!!! on channel 1. 
0 means "no encryption", so the password that was set before as myAss!!!" is not used here. (instead 0 setting to 3 uses wpa2_psk, so the password would be used) 
AT+CWDHCP_DEF=1 (turn on DHCP, so others can automatically get an IP address when they connect to your wifi)

either as a station or an AP create a simple server others can connect to:

AT+CIPMUX=1 (1: server accepts multiple connection, 0: single connections only)
AT+CIPSERVER=1,80 (1: create 0:destroy server, 80: listening on port 80)

now you can try to connect to your server, eg with your browser (not a good idea, but still interesting...) we need the ip address for that.

find the ip of your esp8266 board:

AT+CIFSR (wait until you get the below output)
+CIFSR:APIP,"192.168.4.1"
+CIFSR:APMAC,"1a:fe:34:d2:47:65"
+CIFSR:STAIP,"0.0.0.0"
+CIFSR:STAMAC,"18:fe:34:d2:47:65"

The first line shows the IP address of your board. you can now enter it in a browser: http://192.168.4.1:80 other convenient tests: in the shell: telnet, netcat in puredata: netsend

using the esp8266 with software serial

AT+UART=9600,8,1,0,0

Sets the ESP8266 to 9600 baud, 8 databits, 1 stop bit no parity bit and no flow control.

/*
  Software serial multple serial test

 Receives from the hardware serial, sends to software serial.
 Receives from software serial, sends to hardware serial.

 The circuit:
 * RX is digital pin 10 (connect to TX of other device)
 * TX is digital pin 11 (connect to RX of other device)

 Note:
 Not all pins on the Mega and Mega 2560 support change interrupts,
 so only the following can be used for RX:
 10, 11, 12, 13, 50, 51, 52, 53, 62, 63, 64, 65, 66, 67, 68, 69

 Not all pins on the Leonardo support change interrupts,
 so only the following can be used for RX:
 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

 created back in the mists of time
 modified 25 May 2012
 by Tom Igoe
 based on Mikal Hart's example

 This example code is in the public domain.

 */
#include <SoftwareSerial.h>

SoftwareSerial mySerial(10, 11); // RX, TX

void setup() {
  // Open serial communications and wait for port to open:
  Serial.begin(57600);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }


  Serial.println("Goodnight moon!");

  // set the data rate for the SoftwareSerial port
  mySerial.begin(9600);
  mySerial.println("AT+RST");
}

void loop() { // run over and over
  if (mySerial.available()) {
    Serial.write(mySerial.read());
  }
  if (Serial.available()) {
    mySerial.write(Serial.read());
  }
}