arduino_esp8266(esp-01)

You could use the Hardware pins on the Arduino (using Serial instead of SoftwareSerial) but you would have to keep disconnecting the ESP every time you program the Arduino… and you would not have the benefit of the IDE serial monitor for debug purposes.

Best to use the SoftwareSerial pins 2&3 as per the sketch. Other pins can also be used, but easier to stick with commonly used ones.

The term SoftwareSerial stems from using digital pins to emulate a UART (True Hardware Serial)… of which there is only the one on an UNO (pins 0&1): https://www.arduino.cc/en/Reference/SoftwareSerial

#define BLYNK_PRINT Serial#include <ESP8266_Lib.h>#include <BlynkSimpleShieldEsp8266.h>// You should get Auth Token in the Blynk App.// Go to the Project Settings (nut icon).char auth[] = "YourAuthToken"; // Your WiFi credentials.// Set password to "" for open networks.char ssid[] = "YourNetworkName"; char pass[] = "YourPassword"; #include <SoftwareSerial.h>SoftwareSerial EspSerial(2, 3); // RX, TX// Your ESP8266 baud rate:#define ESP8266_BAUD 9600ESP8266 wifi(&EspSerial); void setup(){ // Debug console Serial.begin(9600); // Set ESP8266 baud rate EspSerial.begin(ESP8266_BAUD); delay(10); Blynk.begin(auth, wifi, ssid, pass); } void loop(){ Blynk.run(); }

https://www.instructables.com/id/Connecting-ESP8266-01-to-Arduino-UNOMEGA-and-BLYNK/

https://spidyhero.wordpress.com/2015/08/01/esp8266-esp-01-blynk-led-controller-thai-version/

AT command

http://room-15.github.io/blog/2015/03/26/esp8266-at-command-reference/

AT+CWMODE=3

AT+CWJAP="ssid","pwd"

AT+CIFSR - Get local IP address

http://www.myarduino.net/article/49/%E0%B8%AA%E0%B8%AD%E0%B8%99%E0%B9%83%E0%B8%8A%E0%B9%89%E0%B8%87%E0%B8%B2%E0%B8%99-arduino-wi-fi-module-esp8266-%E0%B9%81%E0%B8%9A%E0%B8%9A-tcp-client

สอนใช้งาน Arduino Wi-Fi Module ESP8266 แบบ TCP Client

ก่อนหน้านี้การติด Wi-Fi ให้กับ Arduino เป็นงานที่จะใช้กับอุปกรณ์ที่มีความสำคัญและราคาแพงเท่านั้น ไม่ติดกันเยอะเพราะ module Wi-Fi มีราคาแพงมาก ฟังก์ชัน Wi-Fi ที่อยากใช้จึงมีข้อจำกัดในด้านของราคา

แต่ตอนนี้สามารถทำได้แล้วด้วย Wi-Fi Module ESP8266 เป็นโมดูล WI-FI ด้วยเป็นโมดูลขนาดเล็ก มีราคาที่ไม่แพงของ โมดูล ESP8266 นี้จะทำให้เราสามารถติดสัญญาณ Wi-Fi ให้กับอุปกรณ์ Arduino ของเราได้ ต่อไปนี้ทุกอย่างจะสามารถเชื่อมต่อ WI-FI หรือเข้าสู่ยุคของ Wi-Fi Thinking

ESP8266 เป็นสินค้าจากบริษัท Expressif ที่เปิดตัวมาตั้งแต่ปลายปีที่แล้ว โดยใช้เทคโนโลยีการผลิต 40 นาโนเมตร จุดเด่นสำคัญคือราคาขายของชิปตัวนี้อยู่ที่หลักร้อยนิด ๆ เท่านั้นเอง

Wi-Fi Module ESP8266 ตัวชิปมาพร้อมกับ Firmware สั่งงานคำสั่งแบบ AT Command เพื่อเชื่อมต่อ TCP/IP ได้ทันที หรือถ้าต้องการพัฒนาซอฟต์แวร์บนตัวชิปโดยตรงก็สามารถใช้ GCC มาคอมไพล์ซอฟต์แวร์ได้

ข้อมูล Wi-Fi Module ESP8266

โมดูล Wi-Fi Module ESP8266 นี้ใช้ไฟ 3.3V ใช้กระแสที่ 70mA สูงสุดที่ 240mA แนะนำให้ใช้แหล่งจ่ายไฟ 300mA สั่งงานโดยใช้คำสั่ง AT Command ที่อยู่ใน Firmware สามารถอัพเกรด Firmware ได้

Specification

  • Module power 3.3V, regular current consumption at 70ma, peak current at 240mA (300mA must be able to provided)

  • +20Dbm power, 100M max transmitting distance on ideal circumstance.

  • It is common and correct to see some random error data when module is power up, and end up with "ready" (Turn baud rate to 115200 can see this actual debug data, this is used for firmware updating)

IC Features

  • 802.11 b / g / n

  • WIFI @ 2.4 GHz, supports WPA / WPA2 security mode

  • Ultra-small size module 11.5mm * 11.5mm

  • Built-in 10 bit precision ADC

  • Built-in TCP / IP protocol stack

  • Built-in TR switch, balun, LNA, power amplifier and matching network

  • Built-in PLL, voltage regulator and power management components

  • 802.11b mode + 19.5dBm output power

  • Supports antenna diversity

  • Off leakage current is less than 10uA

  • Built-in low-power 32-bit CPU: can double as an application processor

  • SDIO 2.0, SPI, UART

  • STBC, 1x1 MIMO, 2x1 MIMO

  • The guard interval A-MPDU, the polymerization of the A-MSDU and 0.4 s of

  • Within 2ms of the wake, connect and transfer data packets

  • Standby power consumption is less than 1.0mW (DTIM3)

  • Operating temperature range -40 ~ 125 ℃

Wi-Fi Module ESP8266 มีหลายรุ่นให้เลือกใช้ตามความเหมาะสม ตามรูป ในบทความนี้จะใช้รุ่น ESP-01

โมดูล มีการจัดขาตามรูปนี้

เวลาต่อ บางคนอาจจะต่อไม่สะดวก เพราะมองกลับด้านต่อวงจร จึงทำรูปจัดขามองจากด้านที่เห็นขา ดังนี้

อุปกรณ์ที่ใช้

เริ่มต้นให้เราต่ออุปกรณ์ตามรูปข้างล่างนี้ เพื่อเชื่อมต่อกับ ESP8266 ผ่าน arduino

  • Vcc-3.3V

  • Gnd-Gnd

  • CH_PD-3.3V

  • TX-TX(ขา 1)

  • Rx-RX(ขา 0)

เมื่อต่อตามรูปเสร็จแล้ว ให้เราอัพโหลดโค๊ดตัวอย่างไฟกระพริบ

void setup() {

// initialize digital pin 13 as an output.

pinMode(13, OUTPUT);

}

// the loop function runs over and over again forever

void loop() {

digitalWrite(13, HIGH); // turn the LED on (HIGH is the voltage level)

delay(1000); // wait for a second

digitalWrite(13, LOW); // turn the LED off by making the voltage LOW

delay(1000); // wait for a second

}

เมื่ออัพโหลดโค๊ดตัวอย่างไฟกระพริบเสร็จแล้ว ให้ไปที่ serial monitor กำหนดอัตราส่งข้อมูลเป็น 115200 และปรับช่องในรูปให้เป็น Both NL&CR ต่อมาให้เราพิมพ์คำสั่ง AT แล้วกด Enter esp8266จะตอบกับมาว่า ok แปลว่าเรา เชื่อมต่อสำเร็จแล้ว จะได้รูปตามด้านล่าง

คราวนี้เรามาลองทดสอบคำสั่ง AT Command ที่มีให้เราใช้งานใน ESP8266 Wi-Fi Module โดยอ้างอิงจาก เว็บไซต์http://www.electrodragon.com/w/Wi07c ลองดูที่คำสั่ง AT+GMR เป็นคำสั่งเช็คเวอร์ชัน เมื่อพิมพ์แล้วจะได้ผลลัพธ์ออกมาดังนี้ จากรูปจะเห็นว่าเวอร์ชัน Firmware เป็น 0016091 หรือเวอร์ชัน 0.91 ตอนนี้มีเวอร์ชัน 0.9.2.2 ซึ่งเราสามารถเลือกอัพเดทเวอร์ชัน Firmware ได้ เมื่ออัพเดทแล้วจะเป็น 0018000902 พิมคำสั่ง AT+GMR แล้วจะได้รูปตามด้านล่าง

ต่อมาให้ Upload Firmware ใหม่ โหลด 2 โปรแกรมข้างล่างนี้

ต่ออุปกรณ์ตามรูปข้างล่างนี้ เพื่อ Upload Firmware ใหม่

อัพโหลดโค๊ดข้างล่างนี้ลงไปใน arduino ก่อน Upload Firmware ใหม่

int ch_pd = 3;

int io0 = 2;

void setup() {

pinMode(ch_pd, OUTPUT);

pinMode(io0, OUTPUT);

digitalWrite(io0,LOW);

digitalWrite(ch_pd,LOW);

delay(1000);

digitalWrite(ch_pd, HIGH);

}

void loop()

{

}

1. เปิดโปรแกรม esp8266_flasher

2. กด Button “Bin” และ Browse File ไปที V0.9.2.2 AT Firmware.bin

กดปุ่ม Bin

เลือกไฟล์ V0.9.2.2 AT Firmware.bin

กำหนด Comport เป็น Port ของ Arduino (สามารถดู Comport ได้ที Device Manager)

เลือก Comport ให้ตรงกบที่บอร์ด Arduino เชื่อมต่ออยู่

กดปุ่ม Download

รอจน Flash Firmware เสร็จ

เมื่อเราอัพโหลดซอฟต์แวร์ เราก็สามารถ อัพโหลดโค๊ด ด้วยอัตราส่งข้อมูล 9600 ได้แล้ว

ต่อมาเราจะตั้งค่า esp8266 แบบ Client Server ให้เชื่อมต่อกับเราเตอร์

ต่ออุปกรณ์ตามรูปข้างล่างนี้

โหลด Library TEE_ESP_WIFI.rar

อัพโหลดโค๊ดข้างล่างนี้ลงไปใน arduino เพื่อ set esp8266 ให้เป็น Client และเชื่อมต่อ กับ เราเตอร์ ถ้าเชื่อมกับเราเตอร์ต่อสำเร็จ จะได้ ip ที่เราเตอร์ แจกให้ esp8266

ใส่ชื่อเราเตอร์ที่ String ssid="stk";

ใส่ password เราเตอร์ที่ String pass="stk123456";

#include<TEE_ESP_WIFI.h> //////

#include <SoftwareSerial.h> //////

#define pinEN 9

#define ESP_Rx 10

#define ESP_Tx 11

String ssid="stk";

String pass="stk123456";

String port="8000";

ESP wifi(ESP_Rx,ESP_Tx,pinEN);

void print_debug(String data)

{Serial.print(data);}

void setup()

{

Serial.begin(9600);

wifi.begin(9600);

Serial.println("EPS8226");

wifi.Event_debug = print_debug;

wifi.reset();

wifi.setmode(STATION);

wifi.disconnectAP();

wifi.connectAP(ssid,pass);

wifi.multipleconnect(MULTIPLE);

wifi.startserver("8000");

String ip = wifi.myip();

}

String data="";

void loop()

{

if(wifi.available())

{

Serial.write(wifi.read());

}

if(Serial.available())

{

char input = Serial.read();

data += input;

if(input==0x0D)

{

wifi.print(0,data);

data="";

}

}

}

ต่อมาเราจะใช้มือถือควบคุมเปิดปิดไฟ LED ผ่าน wifi เมื่อเราส่งค่า "LED ON" หลอดLEDไฟจะติด และ เมื่อเราส่งค่า "LED OFF" หลอดLEDไฟจะดับ เราจะนำ ip address จากโค๊ดก่อนหน้านี้มาใช้งาน ให้ต่ออุปกรณ์ตามรูปด้านล่าง

อัพโหลดโค๊ดนี้ลงไปใน arduino

#include<TEE_ESP_WIFI.h> /////

#include <SoftwareSerial.h> //////

#define pinEN 9

#define ESP_Rx 10

#define ESP_Tx 11

#define LED 7

String ssid="My_AP_EPS8266";

String pwd ="12345678";

ESP wifi(ESP_Rx,ESP_Tx,pinEN);

void print_debug(String data)

{Serial.print(data);}

void setup()

{

pinMode(LED, OUTPUT);

digitalWrite(LED,LOW);

Serial.begin(9600);

wifi.begin(9600);

Serial.println("ESP8226");

wifi.Event_debug = print_debug;

wifi.reset();

wifi.setmode(STATION_AP);

if(wifi.setAP(ssid,pwd,1,WPA_WPA2_PSK))

{

wifi.multipleconnect(MULTIPLE);

wifi.startserver("8000");

String ip = wifi.myip();

}

}

void loop()

{

String data = wifi.readstringdata();

if(data.length())

{

if(data=="LED ON"

{

digitalWrite(LED,HIGH);

}

if(data=="LED OFF")

{

digitalWrite(LED,LOW);

}

Serial.println(data);

}

}

ติดตั้ง โปรแกรม TCP test tool จาก App Store

เปิด Program TCP test tool ตังค่า IP และ Port

พิมพ์ “LED ON” และ “LED OFF” แล้วกด Send LED สามารถ On / Off ได้ตามคาสั่ง