Micro WebServer จาก ESP8266 และ Arduino

วันนี้ทางผมเอาตัวอย่างการใช้งาน #ESP8266 กับ สร้าง Wifi Web Server แบบราคา super สุดคุ้ม เป็นตัวอย่าง Web Site ง่ายๆ นะครับ ใครเอาใช้ ใครเอาขายไม่แบ่งเงิน แล้วมัน Hang อย่าพึงบ่นนะครับ
วงจร ต่อประมาณนี้ครับ แต่ที่เพิ่มไป คือ ต่อ VR ที่ A0 สำหรับ อ่านค่า
<img class="wp-image-6330 size-medium" src="http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-300×300.jpg" alt="ต่อเพียง 5 สาย ทำงานได้" width="300" height="300" srcset="http://www.ayarafun viagra prix en tunisie.com/wp-content/uploads/2014/09/IMG_20140928_123145-300×300.jpg 300w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-150×150.jpg 150w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-1024×1024.jpg 1024w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-50×50.jpg 50w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-100×100.jpg 100w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-500×500.jpg 500w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-1000×1000.jpg 1000w, http://www.ayarafun.com/wp-content/uploads/2014/09/IMG_20140928_123145-120×120.jpg 120w” sizes=”(max-width: 300px) 100vw, 300px” />
ต่อเพียง 5 สาย ทำงานได้
ตัวอย่างการทำ Web Server
#include <SoftwareSerial.h>
#define SSID "ssssssssss"
#define PASS "xxxxxxxxxx"
SoftwareSerial dbgSerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(115200);
Serial.setTimeout(500);
dbgSerial.begin(9600); //can't be faster than 19200 for softserial
//test if the module is ready
sendAndWait( "AT","OK",10);
//Change to mode 1
//sendAndWait("AT+CWMODE=1","no change","OK",100);
Serial.println("AT+CWMODE=1");
delay(100);
// connect to router
connectWiFi(SSID,PASS);
//set the multiple connection mode
sendAndWait("AT+CIPMUX=1","OK",100);
//set the server of port 80 check "no change" or "OK"
sendAndWait("AT+CIPSERVER=1,80","no change",100);
//print the ip addr
dbgSerial.println("ip address:");
Serial.println("AT+CIFSR");
delay(100);
while ( Serial.available() ) {
dbgSerial.write(Serial.read());
}
dbgSerial.println();
dbgSerial.println( "Start Webserver" );
}
void loop() {
while (Serial.available() >0 )
{
char c = Serial.read();
if (c == 71) {
dbgSerial.println("Send Web Request");
webserver();
delay(500);
}
}
}
void http(String output)
{
Serial.print("AT+CIPSEND=0,");
Serial.println(output.length());
if (Serial.find(">"))
{
//Serial.println(output);
sendAndWait(output,"SEND OK",10);
}
}
// Get the data from the WiFi module and send it to the debug serial port
boolean sendAndWait(String AT_Command, char *AT_Response, int wait){
dbgSerial.print(AT_Command);
Serial.println(AT_Command);
delay(wait);
while ( Serial.available() > 0 ) {
if ( Serial.find(AT_Response) ) {
dbgSerial.print(" --> ");
dbgSerial.println(AT_Response);
return 1;
}
}
dbgSerial.println(" fail!");
return 0;
}
void webserver(void) {
char temp1[10];
dtostrf(analogRead(A0),1,2,temp1);
http("<title>Ayarafun Webserver</title><H3>Welcome to My Real Micro Site Web Server</H3><p>Value A0 = "+ String(temp1) +"</p>");
//delay(1000);
sendAndWait("AT+CIPCLOSE=0","",500);
}
boolean connectWiFi(String NetworkSSID,String NetworkPASS)
{
String cmd = "AT+CWJAP=\"";
cmd += NetworkSSID;
cmd += "\",\"";
cmd += NetworkPASS;
cmd += "\"";
//dbgSerial.println(cmd);
//sendAndWait(cmd,"OK",10);
Serial.println(cmd);
delay(100);
while ( Serial.available() ) {
dbgSerial.write(Serial.read());
}
}
นาย สุธรรม แสงทรง
อ้างอิง
http://www.ayarafun.com/2014/09/micro-webserver-from-esp8266-and-arduino/

ไม่มีความคิดเห็น:
แสดงความคิดเห็น