ซื้อ Mega Wifi ได้ที่นี่ 

 

สรุปขั้นตอนสำคัญดังนี้

บอร์ดนี้ประกอบด้วย 2 ส่วนได้แก่ Mega 2560 และ ESP8266

ซึ่ง communicate กันด้วย Serial3 (TX3/RX3) สรุปขั้นตอนการใช้งานได้แก่

  1.  เขียนโปรแกรมผ่าน arduino  IDE เซต บอร์ดเป็น Mega 2560 เซต dip switch 3,4 on เพื่อโปรแกรมเข้าส่ง Mega 2560
  2. ทำการเซต dip switch 5,6,7 on และกด mode เพื่อ flash firmware ของ ESP8266 เข้าไป
  3. ทำการเซต dip switch 1,2,3,4 ให้ on และที่เหลือเป็น off เพื่อให้ ESP คุยกับ mega ได้ผ่าน Serial3 และสามารถโปรแกรม Mega  ผ่าน USB ได้

อ้างอิงผัง dip switch ของบอร์ดเป็นดังนี้

1 2 3 4 5 6 7 8
CH340 connect to ESP8266 (upload sketch) OFF OFF OFF OFF ON ON ON NoUSE
CH340 connect to ESP8266 (connect) OFF OFF OFF OFF ON ON OFF NoUSE
CH340 connect to ATmega2560 (upload sketch) OFF OFF ON ON OFF OFF OFF NoUSE
CH340 connect to ESP8266 ON ON ON ON OFF OFF OFF NoUSE
Mega2560+ESP8266 ON ON OFF OFF OFF OFF OFF NoUSE
All modules work independently OFF OFF OFF OFF OFF OFF OFF NoUSE

ตัว switch เป็นดังรูป

 

ถ้าดันไปทางซ้ายเป็น on, ตัวอย่างในรูปเป็น แบบ 1,2,3,4 => ON , ที่เหลือ OFF

การ flash firmware ESP บน windows

-ให้ดาวน์โหลด fireware จาก https://github.com/espressif/ESP8266_NONOS_SDK/releases/tag/version_3.0.4 ทำการแตกไฟล์ออกมา

-ใช้ esp tool จาก python3

pip3 install esptool

 

จากนั้นทำการติดตั้ง firmware ด้วยคำสั่ง esptool

esptool write_flash –flash_mode dio –flash_size 2MB-c1 0x0 bin/boot_v1.7.bin 0x01000 bin/at/1024+1024/user1.2048.new.5.bin 0x1fb000 bin/blank.bin 0x1fc000 bin/esp_init_data_default_v08.bin 0xfe000 bin/blank.bin 0x1fe000 bin/blank.bin

 

จากนั้นเปิด dip switch 5,6 = ON ที่เหลือปิดหมด

เพื่อ communicate กับ ESP และทดสอบด้วย AT command

คู่มือ AT command

https://www.espressif.com/sites/default/files/documentation/4a-esp8266_at_instruction_set_en.pdf

ทำการเปิด serial monitor เซตบอร์ดเป็น esp8266 generic เลือก port ที่ถูกต้อง

ทำการ set baudrate 115520

รัน AT command ดังนี้

เป็นอันว่า firmware เรียบร้อย

จากนั้นเซต dip switch 1,2,3,4= ON, ที่เหลือ OFF

จากนั้นทำการเขียนโค้ด ใน IDE ดังนี้ โค้ดแปลงจาก ref ด้านบนเพื่อใช้ GY-61

#define NAME_OF_SSID  ........"  // ใส่ค่า SSID
#define PASSWORD_OF_SSID " ....."   // ค่า password ของ SSID

#include <WiFiEspAT.h>

WiFiServer server(80);
int x,y,z;

void read_gy_61()

{

x = analogRead(A0); // อ่านค่าจากขาอะนาล็อก A0
y = analogRead(A1); // อ่านค่าจากขาอะนาล็อก A1
z = analogRead(A2); // อ่านค่าจากขาอะนาล็อก A2
Serial.print("accelerations are x, y, z: ");
Serial.print(x, DEC); // แสดงค่าความเร่งแกน X เป็นเลขฐาน 10 
Serial.print(" ");
Serial.print(y, DEC); //แสดงค่าความเร่งแกน Y เป็นเลขฐาน 10
Serial.print(" ");
Serial.println(z, DEC); // แสดงค่าความเร่งแกน Z เป็นเลขฐาน 10


}
void setup()
{
Serial.begin(115200);
while (!Serial)
;

Serial3.begin(115200);
WiFi.init(Serial3);
if (WiFi.status() == WL_NO_MODULE)
{
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true)
;
}
WiFi.begin(NAME_OF_SSID, PASSWORD_OF_SSID);
Serial.println("Waiting for connection to WiFi");
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print('.');
}
Serial.println();
server.begin();
IPAddress ip = WiFi.localIP();
Serial.println();
Serial.println("Connected to WiFi network.");
Serial.print("To access the server, enter \"http://");
Serial.print(ip);
Serial.println("/\" in web browser.");
}
void loop()
{
 
read_gy_61();
WiFiClient client = server.available();
if (client)
{
IPAddress ip = client.remoteIP();
Serial.print("new client ");
Serial.println(ip);
while (client.connected())
{
if (client.available())
{
String line = client.readStringUntil('\n');
line.trim();
Serial.println(line);
// if you've gotten to the end of the HTTP header (the line is blank),
// the http request has ended, so you can send a reply
if (line.length() == 0)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("<h4>X: ");
client.print(x);
client.print("</h4>");
client.print("<h4>Y: ");
client.print(y);
client.print("</h4>");
client.print("<h4>Z: ");
client.print(z);
client.print("</h4>");
client.println("</html>");
client.flush();
break;
}
}
}
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}

(ของเดิมใช้ BME 280 I2c)

จากนั้นเมื่อการ upload เข้าไป
ทำการ เปิด serial monitor จะเห็นค่าที่อ่านได้


จากนั้นทำการเปิดเว็บที่ address http://10.5.1.80
จะได้การแสดงผลที่ update realtime ได้


ไว้คราวหน้าจะลองดูการต่อกับ i2c แสดงผลบ้าง

 ซื้อ Mega Wifi ราคาดี https://mespace.org/product/arduino-mega2560-wifi/ 

ที่มา

https://www.gabrielcsapo.com/arduino-web-server-mega-2560-r3-built-in-esp8266/