8.萬用模組

本版程式碼提供開機時自動偵錯,並將偵錯結果顯示在LCD銀幕上

而且將ESP8266的啟動速度最大化,

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // 若LCD 無法啟用,請將0X3F 改為0X27

#include <SoftwareSerial.h>

#define RX 3 //ESP8266 的 TX接3

#define TX 2 //ESP8266 RX接2

SoftwareSerial esp8266(RX,TX);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define SSID "---------------" //這裡填入你家AP的SSID

#define PASSWORD "--------------" //這裡填入你家AP的密碼

#define CIPMUX 1 //多重連線模式(0:OFF 1:ON)

#define SERVER 1 //Server模式(0:OFF 1:ON)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

String response="";

String ip;

int set;

int m = 0 ;

void lcdmsg(String msg){

lcd.setCursor(0,1);

lcd.print(" ");

lcd.setCursor(0,1);

lcd.print(msg);

}

void boot(){

esp8266.println(F("AT+RST"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("ready")) {

lcdmsg("boot success");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

esp8266.println(F("AT+CWMODE=1"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Station Mode");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

String cmd="AT+CWJAP=\"";

cmd+=SSID;

cmd+="\",\"";

cmd+=PASSWORD;

cmd+="\"";

cmd+="\r\n";

esp8266.print(cmd);

esp8266.flush();

while (!esp8266.available()) {}

do{

lcdmsg("clienting...");

}while(!esp8266.find("OK"));

lcdmsg("Link ");

lcd.print(SSID);

if(CIPMUX==1){

esp8266.println(F("AT+CIPMUX=1"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("CIPMUX=1");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

if(SERVER==1){

esp8266.println(F("AT+CIPSERVER=1,80"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server On");

}else{

lcdmsg("ESP8266 Unlink...");

}

}else{

esp8266.println(F("AT+CIPSERVER=0"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server Off");

boot();

}else{

lcdmsg("ESP8266 Unlink...");

boot();}}}}

void getip(){

int head=0,foot=0;

String ip = "";

esp8266.println(F("AT+CIFSR"));

esp8266.flush();

while(!esp8266.available()){}

while(esp8266.available()){

char c = esp8266.read();

ip += c;

delay(5);}

for(int i=15 ; i<30 ; i++){

if(ip.charAt(i)=='I'&&ip.charAt(i+1)=='P'){head=i;}}

for(int i=head+5 ; i<head+21 ; i++){

if(ip.charAt(i)=='"'){foot=i;}}

for(int i=0 ; i<foot-head-4 ; i++){

lcd.setCursor(i,1);

lcd.print(ip.charAt(head+i+4));

}}

void setup()

{

Serial.begin(9600);

esp8266.begin(9600);

lcd.init();

lcd.backlight();

lcd.clear();

boot();

getip();

}

void loop(){

}

實際上,ESP8266 開機之後就會連線上一回設定的AP,若要節省記憶體空間,程式碼可以修改成如下

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // 若LCD 無法啟用,請將0X3F 改為0X27

#include <SoftwareSerial.h>

#define RX 3 //ESP8266 的 TX接3

#define TX 2 //ESP8266 RX接2

SoftwareSerial esp8266(RX,TX);

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

#define CIPMUX 1 //多重連線模式(0:OFF 1:ON)

#define SERVER 1 //Server模式(0:OFF 1:ON)

//////////////////////////////////////////////////////////////////////////////////////////////////////////////

String response="";

String ip;

int set;

int m = 0 ;

void lcdmsg(String msg){

lcd.setCursor(0,1);

lcd.print(" ");

lcd.setCursor(0,1);

lcd.print(msg);

}

void boot(){

esp8266.println(F("AT+RST"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("WIFI GOT IP")) {

lcdmsg("boot success");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

if(CIPMUX==1){

esp8266.println(F("AT+CIPMUX=1"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("CIPMUX=1");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

if(SERVER==1){

esp8266.println(F("AT+CIPSERVER=1,80"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server On");

}else{

lcdmsg("ESP8266 Unlink...");

}

}else{

esp8266.println(F("AT+CIPSERVER=0"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server Off");

boot();

}else{

lcdmsg("ESP8266 Unlink...");

boot();}}}}

void getip(){

int head=0,foot=0;

String ip = "";

esp8266.println(F("AT+CIFSR"));

esp8266.flush();

while(!esp8266.available()){}

while(esp8266.available()){

char c = esp8266.read();

ip += c;

delay(5);}

for(int i=15 ; i<30 ; i++){

if(ip.charAt(i)=='I'&&ip.charAt(i+1)=='P'){head=i;}}

for(int i=head+5 ; i<head+21 ; i++){

if(ip.charAt(i)=='"'){foot=i;}}

for(int i=0 ; i<foot-head-4 ; i++){

lcd.setCursor(i,1);

lcd.print(ip.charAt(head+i+4));

}}

void setup()

{

Serial.begin(9600);

esp8266.begin(9600);

lcd.init();

lcd.backlight();

lcd.clear();

boot();

getip();

}

void loop(){

}

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // 若LCD 無法啟用,請將0X3F 改為0X27

#include <SoftwareSerial.h>

#define RX 3 //ESP8266 的 TX接3

#define TX 2 //ESP8266 RX接2

SoftwareSerial esp8266(RX,TX);

//////////////////////////////////////////////////////////////////

#define SSID "--------------" //這裡填入你家AP的SSID

#define PASSWORD "---------------" //這裡填入你家AP的密碼

#define CIPMUX 1 //多重連線模式(0:OFF 1:ON)

#define SERVER 1 //Server模式(0:OFF 1:ON)

//////////////////////////////////////////////////////////////////

String response="";

String ip;

int set;

int m = 0 ;

int debug=0;

void lcdmsg(String msg){

lcd.setCursor(0,1);

lcd.print(" ");

lcd.setCursor(0,1);

lcd.print(msg);

}

void boot(){

esp8266.println(F("AT+RST"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("ready")) {

lcdmsg("boot success");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

esp8266.println(F("AT+CWMODE=1"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Station Mode");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

String cmd="AT+CWJAP=\"";

cmd+=SSID;

cmd+="\",\"";

cmd+=PASSWORD;

cmd+="\"";

cmd+="\r\n";

esp8266.print(cmd);

esp8266.flush();

while (!esp8266.available()) {}

do{

lcdmsg("clienting...");

}while(!esp8266.find("OK"));

lcdmsg("Link ");

lcd.print(SSID);

if(CIPMUX==1){

esp8266.println(F("AT+CIPMUX=1"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("CIPMUX=1");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

if(SERVER==1){

esp8266.println(F("AT+CIPSERVER=1,80"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server On");

}else{

lcdmsg("ESP8266 Unlink...");

}

}else{

esp8266.println(F("AT+CIPSERVER=0"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server Off");

boot();

}else{

lcdmsg("ESP8266 Unlink...");

boot();}}}}

void getip(){

int head=0,foot=0;

String ip = "";

esp8266.println(F("AT+CIFSR"));

esp8266.flush();

while(!esp8266.available()){}

while(esp8266.available()){

char c = esp8266.read();

ip += c;

delay(5);}

for(int i=15 ; i<30 ; i++){

if(ip.charAt(i)=='I'&&ip.charAt(i+1)=='P'){head=i;}}

for(int i=28 ; i<45 ; i++){

if(ip.charAt(i)=='"'){foot=i;}}

for(int i=0 ; i<foot-head-4 ; i++){

lcd.setCursor(i,1);

lcd.print(ip.charAt(head+i+4));

}}

void sendmsg(String command,int sw){//放在loop裡面

esp8266.print(command);

esp8266.flush();

String message="";

debug=0;

while (!esp8266.available()) {}

while(esp8266.available()){

char c = esp8266.read();

message += c ;

delay(5);

}

Serial.println(message);

if(sw==1){

m=message.length();

for(int i=0 ; i<=m ;i++){

if(message.charAt(i)=='R'&&message.charAt(i+1)=='e'&&message.charAt(i+2)=='c'&&message.charAt(i+3)=='v'){debug=1;}}

}

sw=0;

}

void setup()

{

Serial.begin(9600);

esp8266.begin(9600);

lcd.init();

lcd.backlight();

lcd.clear();

boot();

getip();

}

void loop(){

if (esp8266.available()) {

if (esp8266.find("+IPD,")) { // 若接收到"+IPD,",代表有用戶連線了…

float x0 = analogRead(A0); // 讀取A0類比值

float x1 = analogRead(A1); // 讀取A0類比值

float x2 = analogRead(A2); // 讀取A0類比值

float x3 = analogRead(A3); // 讀取A0類比值

float x4 = analogRead(A4); // 讀取A0類比值

float x5 = analogRead(A5); // 讀取A0類比值

byte connID = esp8266.read()-48; // 讀取連線編號(1~5)並轉成數字[建議這裡先說明最多 5 個連線]

// 建立HTML內

String HTML= "HTTP/1.1 200 OK\r\n";

HTML+="Content-Type: text/html\r\n\r\n" ;

HTML+= "<!doctype html>\r\n<html>\r\n<head><meta charset=\"utf-8\">";

// HTML+= "<link rel=\"shortcut icon\"href=\"http://study.sce.ntnu.edu.tw/OnlineMTC/favicon.ico\">";

// HTML+="<meta http-equiv=\"refresh\"content=\"15\">";

HTML+="<title>ESP8266</title></head>";

HTML+="<body><h1>Arduino</h1><p>A0:";

HTML+= x0; // 顯示A0類比腳位值

HTML+="</p><p>A1:";

HTML+= x1; // 顯示A1類比腳位值

HTML+="</p><p>A2:";

HTML+= x2; // 顯示A2類比腳位值

HTML+="</p><p>A3:";

HTML+= x3; // 顯示A3類比腳位值

HTML+="</p><p>A4:";

HTML+= x4; // 顯示A4類比腳位值

HTML+="</p><p>A5:";

HTML+= x5; // 顯示A5類比腳位值

HTML+="</p></body></html>";

// 建立AT+CIPSEND命令字串

String cipSend = "AT+CIPSEND=";

cipSend += connID; // 附加連線編號

cipSend += ",";

cipSend += HTML.length(); // 取得HTML內容的長度

cipSend +="\r\n";

sendmsg(cipSend,0);

sendmsg(HTML,1);

String cipClose = "AT+CIPCLOSE=";

cipClose+=connID; // 附加連線編號

cipClose+="\r\n";

if(debug==1){

do{

esp8266.print(cipClose);

Serial.println("data ok");

}while(!esp8266.find("OK"));

// 送出「中斷連線」命令

} }

}

}

精簡過後的版本

#include <Wire.h>

#include <LiquidCrystal_I2C.h>

LiquidCrystal_I2C lcd(0x3F,16,2); // 若LCD 無法啟用,請將0X3F 改為0X27

#include <SoftwareSerial.h>

#define RX 3 //ESP8266 的 TX接3

#define TX 2 //ESP8266 RX接2

SoftwareSerial esp8266(RX,TX);

int debug=0;

void lcdmsg(String msg){

lcd.setCursor(0,1);

lcd.print(" ");

lcd.setCursor(0,1);

lcd.print(msg);

}

void boot(){

esp8266.println(F("AT+RST"));

esp8266.flush();

lcdmsg("booting...");

unsigned long t1=millis();

do{

if(millis()>t1+15000){boot();}

}while(!esp8266.find("IP"));

lcdmsg("boot success");

esp8266.println(F("AT+CIPMUX=1"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("CIPMUX=1");

}else{

lcdmsg("ESP8266 Unlink...");

boot();

}

esp8266.println(F("AT+CIPSERVER=1,80"));

esp8266.flush();

while (!esp8266.available()) {}

if (esp8266.find("OK")) {

lcdmsg("Server On");

}else{

lcdmsg("ESP8266 Unlink...");

}}

void getip(){

int head=0,foot=0;

String ip = "";

esp8266.println(F("AT+CIFSR"));

esp8266.flush();

while(!esp8266.available()){}

while(esp8266.available()){

char c = esp8266.read();

ip += c;

delay(5);}

for(int i=15 ; i<30 ; i++){

if(ip.charAt(i)=='I'&&ip.charAt(i+1)=='P'){head=i;}}

for(int i=head+5 ; i<head+21 ; i++){

if(ip.charAt(i)=='"'){foot=i;}}

for(int i=0 ; i<foot-head-4 ; i++){

lcd.setCursor(i,1);

lcd.print(ip.charAt(head+i+4));

}}

void sendmsg(String command,int sw){//放在loop裡面

esp8266.print(command);

esp8266.flush();

String message="";

debug=0;

while (!esp8266.available()) {}

while(esp8266.available()){

char c = esp8266.read();

message += c ;

delay(5);

}

Serial.println(message);

if(sw==1){

for(int i=0 ; i<=message.length() ;i++){

if(message.charAt(i)=='R'&&message.charAt(i+1)=='e'&&message.charAt(i+2)=='c'&&message.charAt(i+3)=='v'){debug=1;}}

}sw=0; }

void setup(){

Serial.begin(9600);

esp8266.begin(9600);

lcd.init();

lcd.backlight();

lcd.clear();

boot();

getip();

}

void loop(){

if (esp8266.available()) {

if (esp8266.find("+IPD,")) { // 若接收到"+IPD,",代表有用戶連線了…

float x0 = analogRead(A0); // 讀取A0類比值

float x1 = analogRead(A1); // 讀取A0類比值

float x2 = analogRead(A2); // 讀取A0類比值

float x3 = analogRead(A3); // 讀取A0類比值

byte connID = esp8266.read()-48; // 讀取連線編號(1~5)並轉成數字[建議這裡先說明最多 5 個連線]

// 建立HTML內

String HTML= "HTTP/1.1 200 OK\r\n";

HTML+="Content-Type: text/html\r\n\r\n" ;

HTML+= "<!doctype html>\r\n<html>\r\n<head><meta charset=\"utf-8\">";

HTML+="<title>ESP8266</title></head>";

HTML+="<body><h1>Arduino</h1><p>A0:";

HTML+= x0; // 顯示A0類比腳位值

HTML+="</p><p>A1:";

HTML+= x1; // 顯示A1類比腳位值

HTML+="</p><p>A2:";

HTML+= x2; // 顯示A2類比腳位值

HTML+="</p><p>A3:";

HTML+= x3; // 顯示A3類比腳位值

HTML+="</p></body></html>";

// 建立AT+CIPSEND命令字串

String cipSend = "AT+CIPSEND=";

cipSend += connID; // 附加連線編號

cipSend += ",";

cipSend += HTML.length(); // 取得HTML內容的長度

cipSend +="\r\n";

sendmsg(cipSend,0);

sendmsg(HTML,1);

// 送出「中斷連線」命令

String cipClose = "AT+CIPCLOSE=";

cipClose+=connID; // 附加連線編號

cipClose+="\r\n";

if(debug==1){

do{

esp8266.print(cipClose);

Serial.println("data ok");

}while(!esp8266.find("OK"));

}}}}