Pada android bisa menggunakan perintah seperti di bawah ini atau buka browser dengan menuliskan alamat perintahnya.
Full Code pada Arduino nya:
Penting untuk menyamakan settingannya pada declare.h
Full Code librarynya:
Declare.h
uartWifiUno.cpp
uartWifiUno.h
Atau tanpa library-library di atas(lewat AT Command)
URL url = new URL(arduinoCommandURl);
InputStream is = new InputStreamReader(url.openStream(), "UTF-8"));
is.read();
Full Code pada Arduino nya:
Penting untuk menyamakan settingannya pada declare.h
/*
ESP8266 library
When you use it with MEGA board, the connection should be like these:
ESP8266_TX->RX1(D19)
ESP8266_RX->TX1(D18)
ESP8266_CH_PD->3.3V
ESP8266_VCC->3.3V
ESP8266_GND->GND
When you want to output the debug information, please use DebugSerial. For example,
DebugSerial.println("hello");
Note: The size of message from ESP8266 is too big for arduino sometimes, so the library can't receive the whole buffer because
the size of the hardware serial buffer which is defined in HardwareSerial.h is too small.
Open the file from \arduino\hardware\arduino\avr\cores\arduino\HardwareSerial.h.
See the follow line in the HardwareSerial.h file.
#define SERIAL_BUFFER_SIZE 64
The default size of the buffer is 64. Change it into a bigger number, like 256 or more.
*/
//#define SSID "dlink"
//#define PASSWORD "0814931872"
#define SSID "SmartHome"
#define PASSWORD "0816452400"
#include "uartWIFIUNO.h"
#include <SoftwareSerial.h>
#include <LiquidCrystal.h>
#include "declare.h"
void setup()
{
pinMode(ledPin, OUTPUT);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
//pinMode(ESP8266_CHPD, OUTPUT);
//digitalWrite(ESP8266_CHPD,LOW);
digitalWrite(relay1,LOW);
digitalWrite(relay2,LOW);
digitalWrite(relay3,LOW);
digitalWrite(relay4,LOW);
DebugSerial.println("ESP8266 Server v0.2");
// set up the LCD's number of columns and rows:
lcd.begin(16, 2);
lcd.print("ESP8266 Control"); // Print a message to the LCD.
lcd.setCursor(0,1); // set the cursor to (16,1):
lcd.print("Init..."); // Print a message to the LCD.
if(!wifi.begin())
{
DebugSerial.println("Begin error");
}
bool b = wifi.Initialize(STA, SSID, PASSWORD);
if(!b)
{
DebugSerial.println("Init error");
resetFunc();
}
delay(8000); //make sure the module can have enough time to get an IP address
String ipstring = wifi.showIP();
DebugSerial.println(ipstring); //show the ip address of module
lcd.setCursor(0,0); // set the cursor to (16,1):
lcd.print(ipstring);
delay(1000);
wifi.confMux(1);
delay(100);
if(wifi.confServer(1,80))
DebugSerial.println("Server is set up");
lcd.setCursor(0,1); // set the cursor to (16,1):
lcd.print("Start Server");
lcd.setCursor(0,1); // set the cursor to (16,1):
lcd.print("online ");
}
void loop()
{
char buf[500];
int iLen = wifi.ReceiveMessage(buf);
if(iLen > 0)
{
DebugSerial.print(buf);
//delay(100);
if (strncmp(buf, "GET /?status1=1", 15) == 0) {
//DebugSerial.print("RELAY 1 ON");
digitalWrite(relay1, HIGH);
device1 = true;
}
else if (strncmp(buf, "GET /?status1=0", 15) == 0) {
//DebugSerial.print("RELAY 1 OFF");
digitalWrite(relay1, LOW);
device1 = false;
}
if (strncmp(buf, "GET /?status2=1", 15) == 0) {
//DebugSerial.print("RELAY 2 ON");
digitalWrite(relay2, HIGH);
device2 = true;
}
else if (strncmp(buf, "GET /?status2=0", 15) == 0) {
//DebugSerial.print("RELAY 2 OFF");
digitalWrite(relay2, LOW);
device2 = false;
}
if (strncmp(buf, "GET /?status3=1", 15) == 0) {
//DebugSerial.print("RELAY 3 ON");
digitalWrite(relay3, HIGH);
device3 = true;
}
else if (strncmp(buf, "GET /?status3=0", 15) == 0) {
//DebugSerial.print("RELAY 3 OFF");
digitalWrite(relay3, LOW);
device3 = false;
}
if (strncmp(buf, "GET /?status4=1", 15) == 0) {
//DebugSerial.print("RELAY 4 ON");
digitalWrite(relay4, HIGH);
device4 = true;
}
else if (strncmp(buf, "GET /?status4=0", 15) == 0) {
//DebugSerial.print("RELAY 4 OFF");
digitalWrite(relay4, LOW);
device4 = false;
}
String cmd;
cmd = "HTTP/1.1 200 OK\r\n";
cmd += "Content-Type: text/html\r\n";
cmd += "Connection: close\r\n";
cmd += "Refresh: 15\r\n";
cmd += "\r\n";
cmd += "<!DOCTYPE HTML>\r\n";
cmd += "<html>\r\n";
//cmd += "<header><title>ESP8266 Webserver</title><h1>\"ESP8266 Web Server Control\"</h1></header>";
if(device1){
cmd +=("<br/><h2>Device1 : ON</h2>");
}
else{
cmd +=("<br/><h2>Device1 : OFF</h2>");
}
if(device2){
cmd +=("<br/><h2>Device2 : ON</h2>");
}
else{
cmd +=("<br/><h2>Device2 : OFF</h2>");
}
if(device3){
cmd +=("<br/><h2>Device3 : ON</h2>");
}
else{
cmd +=("<br/><h2>Device3 : OFF</h2>");
}
if(device4){
cmd +=("<br/><h2>Device4 : ON</h2>");
}
else{
cmd +=("<br/><h2>Device4 : OFF</h2>");
}
cmd += "<html>\r\n";
wifi.Send(chlID,cmd);
//delay(100);
wifi.closeMux(chlID);
//delay(1000);
}
}
Full Code librarynya:
Declare.h
// initialize the library with the numbers of the interface pins
LiquidCrystal lcd(12, 11, 5, 4, 3, 2);
WIFIUNO wifi;
extern int chlID;
// set pin numbers:
const int ledPin = 13; // the number of the LED pin
const int relay1 = 22;
const int relay2 = 23;
const int relay3 = 24;
const int relay4 = 25;
boolean device1 = false;
boolean device2 = false;
boolean device3 = false;
boolean device4 = false;
const int ESP8266_CHPD = 8;
boolean readyy = false;
int result = 0;
int OK = 1;
int TIMEOUT = 2 ;
int CANCEL = 3;
int NOT_FOUND = 4;
int i;
int m=0;
int lp = 0;
char inputChar[500];
int len = 0;
//String serverip="192.168.1.37";
String port="80";
String cmd;
String myIP;
int currentID;
int ch_id, packet_len;
char *pb;
void(* resetFunc) (void) = 0; //declare reset function at address 0
uartWifiUno.cpp
#include "uartWIFIUNO.h"
// For the UNO let's use softserial for the ESP Wifi module instead of debug
#ifdef UNO
SoftwareSerial mySerial(_DBG_RXPIN_,_DBG_TXPIN_);
#endif
#ifdef DEBUG
#define DBG(message) DebugSerial.print(message)
#define DBGLN(message) DebugSerial.println(message)
#define DBGW(message) DebugSerial.write(message)
#else
#define DBG(message)
#define DBGLN(message)
#define DBGW(message)
#endif // DEBUG
int8_t chlID; //client id(0-4)
bool wifiPresent; //track is wifi card is present
bool WIFIUNO::begin(void)
{
boolean result = false;
//_cell.begin(9600);
_cell.begin(19200);
DebugSerial.begin(debugBaudRate);
_cell.flush();
_cell.setTimeout(10000);
println(F("AT+RST"));
result = _cell.find("eady");
if(result)
{
DBGLN(F("Module is ready"));
wifiPresent = true;
}
else
{
DBGLN(F("Module have no response"));
wifiPresent = false;
}
return wifiPresent;
}
/*************************************************************************
//Initialize port
mode: setting operation mode
STA: Station
AP: Access Point
AT_STA: Access Point & Station
chl: channel number
ecn: encryption
OPEN 0
WEP 1
WAP_PSK 2
WAP2_PSK 3
WAP_WAP2_PSK 4
return:
true - successfully
false - unsuccessfully
***************************************************************************/
bool WIFIUNO::Initialize(byte mode, String ssid, String pwd, byte chl, byte ecn)
{
if (!wifiPresent) return false;
if (mode == STA)
{ bool b = confMode(mode);
if (!b)
{
return false;
}
Reset();
confJAP(ssid, pwd);
}
else if (mode == AP)
{
bool b = confMode(mode);
if (!b)
{
return false;
}
Reset();
confSAP(ssid, pwd, chl, ecn);
}
else if (mode == AP_STA)
{
bool b = confMode(mode);
if (!b)
{
return false;
}
Reset();
confJAP(ssid, pwd);
confSAP(ssid, pwd, chl, ecn);
}
return true;
}
/*************************************************************************
//Set up tcp or udp connection
type: tcp or udp
addr: ip address
port: port number
a: set multiple connection
0 for sigle connection
1 for multiple connection
id: id number(0-4)
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::ipConfig(byte type, String addr, int port, boolean a, byte id)
{
if (!wifiPresent) return false;
boolean result = false;
if (a == 0 )
{
confMux(a);
long timeStart = millis();
while (1)
{
long time0 = millis();
if (time0 - timeStart > 5000)
{
break;
}
}
result = newMux(type, addr, port);
}
else if (a == 1)
{
confMux(a);
long timeStart = millis();
while (1)
{
long time0 = millis();
if (time0 - timeStart > 5000)
{
break;
}
}
result = newMux(id, type, addr, port);
}
return result;
}
/*************************************************************************
//receive message from wifi
buf: buffer for receiving data
chlID: <id>(0-4)
return: size of the buffer
***************************************************************************/
int WIFIUNO::ReceiveMessage(char *buf)
{
if (!wifiPresent) return 0;
//+IPD,<len>:<data>
//+IPD,<id>,<len>:<data>
String data = "";
if (_cell.available()>0)
{
unsigned long start;
start = millis();
char c0 = _cell.read();
while((c0 == '\r' || c0 == '\n') &&
(_cell.available()>0))
c0 = _cell.read();
if (c0 == '+')
{
while (millis()-start<5000)
{
if (_cell.available()>0)
{
char c = _cell.read();
data += c;
}
if (data.indexOf("\nOK")!=-1)
{
break;
}
}
int sLen = strlen(data.c_str());
int i,j;
for (i = 0; i <= sLen; i++)
{
if (data[i] == ':')
{
break;
}
}
boolean found = false;
for (j = 4; j <= i; j++)
{
if (data[j] == ',')
{
found = true;
break;
}
}
int iSize;
DBG(data);
if(found ==true)
{
String _id = data.substring(4, j);
chlID = _id.toInt();
String _size = data.substring(j+1, i);
iSize = _size.toInt();
//DBG(_size);
String str = data.substring(i+1, i+1+iSize);
strcpy(buf, str.c_str()); //DBG(str);
}
else
{ String _size = data.substring(4, i);
iSize = _size.toInt();
//DBGLN(iSize);
String str = data.substring(i+1, i+1+iSize);
strcpy(buf, str.c_str());
//DBG(str);
}
return iSize;
}
}
return 0;
}
//////////////////////////////////////////////////////////////////////////
/*************************************************************************
//reboot the wifi module
***************************************************************************/
void WIFIUNO::Reset(void)
{
if (!wifiPresent) return;
println(F("AT+RST"));
unsigned long start;
start = millis();
while (millis()-start<5000) {
if(_cell.find("eady")==true)
{
DBGLN(F("reboot wifi is OK"));
break;
}
}
}
/*********************************************
*********************************************
*********************************************
WIFIUNO Function Commands
*********************************************
*********************************************
*********************************************
*/
/*************************************************************************
//inquire the current mode of wifi module
return: string of current mode
Station
AP
AP+Station
***************************************************************************/
String WIFIUNO::showMode()
{
if (!wifiPresent) return "";
String data;
println(F("AT+CWMODE?"));
unsigned long start;
start = millis();
while (millis()-start<2000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1)
{
break;
}
}
if(data.indexOf("1")!=-1)
{
return "Station";
}else if(data.indexOf("2")!=-1)
{
return "AP";
}else if(data.indexOf("3")!=-1)
{
return "AP+Station";
}
}
/*************************************************************************
//configure the operation mode
a: 1 - Station
2 - AP
3 - AP+Station
return:
true - successfully
false - unsuccessfully
***************************************************************************/
bool WIFIUNO::confMode(byte a)
{
if (!wifiPresent) return false;
String data;
print(F("AT+CWMODE="));
println(String(a));
unsigned long start;
start = millis();
while (millis()-start<2000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1)
{
return true;
}
if (data.indexOf("ERROR")!=-1 || data.indexOf("busy")!=-1)
{
while(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
DBGLN(data);
return false;
}
}
}
/*************************************************************************
//show the list of wifi hotspot
return: string of wifi information
encryption,SSID,RSSI
***************************************************************************/
String WIFIUNO::showAP(void)
{
if (!wifiPresent) return "";
String data;
_cell.flush();
println(F("AT+CWLAP"));
delay(1000);
while(1);
unsigned long start;
start = millis();
while (millis()-start<8000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
{
break;
}
}
if(data.indexOf("ERROR")!=-1)
{
return "ERROR";
}
else{
char head[4] = {0x0D,0x0A};
char tail[7] = {0x0D,0x0A,0x0D,0x0A};
data.replace("AT+CWLAP","");
data.replace("OK","");
data.replace("+CWLAP","WIFIUNO");
data.replace(tail,"");
data.replace(head,"");
return data;
}
}
/*************************************************************************
//show the name of current wifi access port
return: string of access port name
AP:<SSID>
***************************************************************************/
String WIFIUNO::showJAP(void)
{
if (!wifiPresent) return "";
_cell.flush();
println(F("AT+CWJAP?"));
String data;
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
{
break;
}
}
char head[4] = {0x0D,0x0A};
char tail[7] = {0x0D,0x0A,0x0D,0x0A};
data.replace("AT+CWJAP?","");
data.replace("+CWJAP","AP");
data.replace("OK","");
data.replace(tail,"");
data.replace(head,"");
return data;
}
/*************************************************************************
//configure the SSID and password of the access port
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::confJAP(String ssid , String pwd)
{
if (!wifiPresent) return false;
print(F("AT+CWJAP="));
print(F("\"")); //"ssid"
print(ssid);
print(F("\""));
print(F(","));
print(F("\"")); //"pwd"
print(pwd);
println(F("\""));
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.find("OK")==true)
{
return true;
}
}
return false;
}
/*************************************************************************
//quite the access port
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::quitAP(void)
{
if (!wifiPresent) return false;
println(F("AT+CWQAP"));
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.find("OK")==true)
{
return true;
}
}
return false;
}
/*************************************************************************
//show the parameter of ssid, password, channel, encryption in AP mode
return:
mySAP:<SSID>,<password>,<channel>,<encryption>
***************************************************************************/
String WIFIUNO::showSAP()
{
if (!wifiPresent) return "";
println(F("AT+CWSAP?"));
String data;
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("ERROR")!=-1 )
{
break;
}
}
char head[4] = {0x0D,0x0A};
char tail[7] = {0x0D,0x0A,0x0D,0x0A};
data.replace("AT+CWSAP?","");
data.replace("+CWSAP","mySAP");
data.replace("OK","");
data.replace(tail,"");
data.replace(head,"");
return data;
}
/*************************************************************************
//configure the parameter of ssid, password, channel, encryption in AP mode
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::confSAP(String ssid , String pwd , byte chl , byte ecn)
{
if (!wifiPresent) return false;
print(F("AT+CWSAP="));
print(F("\"")); //"ssid"
print(ssid);
print(F("\""));
print(F(","));
print(F("\"")); //"pwd"
print(pwd);
print(F("\""));
print(F(","));
print(String(chl));
print(F(","));
println(String(ecn));
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.find("OK")==true )
{
return true;
}
}
return false;
}
/*********************************************
*********************************************
*********************************************
TPC/IP Function Command
*********************************************
*********************************************
*********************************************
*/
/*************************************************************************
//inquire the connection status
return: string of connection status
<ID> 0-4
<type> tcp or udp
<addr> ip
<port> port number
***************************************************************************/
String WIFIUNO::showStatus(void)
{
if (!wifiPresent) return "";
println(F("AT+CIPSTATUS"));
String data;
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1)
{
break;
}
}
char head[4] = {0x0D,0x0A};
char tail[7] = {0x0D,0x0A,0x0D,0x0A};
data.replace("AT+CIPSTATUS","");
data.replace("OK","");
data.replace(tail,"");
data.replace(head,"");
return data;
}
/*************************************************************************
//show the current connection mode(sigle or multiple)
return: string of connection mode
0 - sigle
1 - multiple
***************************************************************************/
String WIFIUNO::showMux(void)
{
if (!wifiPresent) return "";
String data;
println(F("AT+CIPMUX?"));
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1)
{
break;
}
}
char head[4] = {0x0D,0x0A};
char tail[7] = {0x0D,0x0A,0x0D,0x0A};
data.replace("AT+CIPMUX?","");
data.replace("+CIPMUX","showMux");
data.replace("OK","");
data.replace(tail,"");
data.replace(head,"");
return data;
}
/*************************************************************************
//configure the current connection mode(sigle or multiple)
a: connection mode
0 - sigle
1 - multiple
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::confMux(boolean a)
{
if (!wifiPresent) return false;
print(F("AT+CIPMUX="));
println(String(a));
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.find("OK")==true )
{
return true;
}
}
return false;
}
/*************************************************************************
//Set up tcp or udp connection (signle connection mode)
type: tcp or udp
addr: ip address
port: port number
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::newMux(byte type, String addr, int port)
{
if (!wifiPresent) return false;
String data;
print(F("AT+CIPSTART="));
if(type>0)
{
print(F("\"TCP\""));
}else
{
print(F("\"UDP\""));
}
print(F(","));
print(F("\""));
print(addr);
print(F("\""));
print(F(","));
println(String(port));
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 || data.indexOf("ERROR")!=-1)
{
return true;
}
}
return false;
}
/*************************************************************************
//Set up tcp or udp connection (multiple connection mode)
type: tcp or udp
addr: ip address
port: port number
id: id number(0-4)
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::newMux( byte id, byte type, String addr, int port)
{
if (!wifiPresent) return false;
print(F("AT+CIPSTART="));
print(F("\""));
print(String(id));
print(F("\""));
if(type>0)
{
print(F("\"TCP\""));
}
else
{
print(F("\"UDP\""));
}
print(F(","));
print(F("\""));
print(addr);
print(F("\""));
print(F(","));
println(String(port));
String data;
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("ALREAY CONNECT")!=-1 )
{
return true;
}
}
return false;
}
/*************************************************************************
//send data in sigle connection mode
str: string of message
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::Send(String str)
{
if (!wifiPresent) return false;
print(F("AT+CIPSEND="));
println(String(str.length()));
unsigned long start;
start = millis();
bool found;
while (millis()-start<5000) {
if(_cell.find(">")==true )
{
found = true;
break;
}
}
if(found)
{
print(str);
}
else
{
closeMux();
return false;
}
String data;
start = millis();
while (millis()-start<5000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("SEND OK")!=-1)
{
return true;
}
}
return false;
}
/*************************************************************************
//send data in multiple connection mode
id: <id>(0-4)
str: string of message
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::Send(byte id, String str)
{
if (!wifiPresent) return false;
print(F("AT+CIPSEND="));
print(String(id));
print(F(","));
println(String(str.length()));
unsigned long start;
start = millis();
bool found;
while (millis()-start<5000) {
if(_cell.find(">")==true )
{
found = true;
break;
}
}
if(found)
_cell.print(str);
else
{
closeMux(id);
return false;
}
String data;
start = millis();
while (millis()-start<5000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("SEND OK")!=-1)
{
return true;
}
}
return false;
}
/*************************************************************************
//Close up tcp or udp connection (sigle connection mode)
***************************************************************************/
void WIFIUNO::closeMux(void)
{
if (!wifiPresent) return;
println(F("AT+CIPCLOSE"));
String data;
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("Linked")!=-1 || data.indexOf("ERROR")!=-1 || data.indexOf("we must restart")!=-1)
{
break;
}
}
}
/*************************************************************************
//Set up tcp or udp connection (multiple connection mode)
id: id number(0-4)
***************************************************************************/
void WIFIUNO::closeMux(byte id)
{
if (!wifiPresent) return;
print(F("AT+CIPCLOSE="));
println(String(id));
String data;
unsigned long start;
start = millis();
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("Link is not")!=-1 || data.indexOf("Cant close")!=-1)
{
break;
}
}
}
/*************************************************************************
//show the current ip address
return: string of ip address
***************************************************************************/
String WIFIUNO::showIP(void)
{
if (!wifiPresent) return "";
String data;
unsigned long start;
for(int a=0; a<3;a++)
{
println(F("AT+CIFSR"));
start = millis();
while (millis()-start<3000) {
while(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("AT+CIFSR")!=-1)
{
break;
}
}
if(data.indexOf(".") != -1)
{
break;
}
data = "";
}
//DBGLN(data);
char head[4] = {0x0D,0x0A};
char tail[7] = {0x0D,0x0D,0x0A};
data.replace("AT+CIFSR","");
data.replace(tail,"");
data.replace(head,"");
return data;
}
/*************************************************************************
////set the parameter of server
mode:
0 - close server mode
1 - open server mode
port: <port>
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::confServer(byte mode, int port)
{
if (!wifiPresent) return false;
print(F("AT+CIPSERVER="));
print(String(mode));
print(F(","));
println(String(port));
String data;
unsigned long start;
start = millis();
boolean found = false;
while (millis()-start<3000) {
if(_cell.available()>0)
{
char a =_cell.read();
data=data+a;
}
if (data.indexOf("OK")!=-1 || data.indexOf("no charge")!=-1)
{
found = true;
break;
}
}
return found;
}
/*************************************************************************
//// Set the CIPSERVER timeout.
timeout: <timeout>
return:
true - successfully
false - unsuccessfully
***************************************************************************/
boolean WIFIUNO::setTimeout(int timeout)
{
_cell.print(F("AT+CIPSTO="));
_cell.println(String(timeout));
String data;
unsigned long start;
start = millis();
boolean found = false;
while(millis()-start<3000){
if(_cell.available()>0)
{
data += _cell.read();
}
if(data.indexOf("OK")!=-1 || data.indexOf("no change")!=-1)
{
found = true;
break;
}
}
return found;
}
/*********************************************
*********************************************
*********************************************
Utility Functions
*********************************************
*********************************************
*********************************************
*/
/*************************************************************************
//print and println
param: text to send either as string or as F("value")
***************************************************************************/
void WIFIUNO::print(const __FlashStringHelper *ifsh)
{
DBG(ifsh);
_cell.print(ifsh);
}
void WIFIUNO::print(const String &s)
{
DBG(s);
_cell.print(s);
}
void WIFIUNO::println(const __FlashStringHelper *ifsh)
{
DBGLN(ifsh);
_cell.println(ifsh);
}
void WIFIUNO::println(const String &s)
{
DBGLN(s);
_cell.println(s);
}
uartWifiUno.h
/*
ESP8266 library
Created by Stan Lee(Lizq@iteadstudio.com)
2014/10/8
Modified version
V1.0 released the first version of ESP8266 library
For UNO let's use SoftSerial for the ESP8266 if we can so we don't
have to keep unplugging the module when we program the Arduino
- Don Eduardo 12/12/2014
*/
#define DEBUG //uncomment to enable debugging
//#define UNO //uncomment this line when you use it with UNO board or Mini
#define MEGA //uncomment this line when you use it with MEGA board
//#define LEO //uncomment this line when you use it with Leonardo or Micro board
#ifndef __UARTWIFI_H__
#define __UARTWIFI_H__
#include <Arduino.h>
//#include "NilRTOS.h"
//Set UNO for ESP on D2/D3 softserial and debug on the UART
#ifdef UNO
#include <SoftwareSerial.h>
#define _DBG_RXPIN_ 10
#define _DBG_TXPIN_ 11
#define _cell mySerial
#define DebugSerial Serial
#define debugBaudRate 19200
extern SoftwareSerial mySerial;
#endif
//set MEGA for ESP on D18/D19 UART and debug on D0/D1 UART
#ifdef MEGA
#define _cell Serial1
#define DebugSerial Serial
#define debugBaudRate 19200
#endif
//set LEO for ESP on D0/D1 UART and debug on native USB serial #ifdef LEO
#define _cell Serial1
#define DebugSerial Serial
#define debugBaudRate 9600
#endif
//The way of encrypstion
#define OPEN 0
#define WEP 1
#define WAP_PSK 2
#define WAP2_PSK 3
#define WAP_WAP2_PSK 4
//Communication mode
#define TCP 1
#define tcp 1
#define UDP 0
#define udp 0
#define OPEN 1
#define CLOSE 0
//The type of initialized WIFI
#define STA 1
#define AP 2
#define AP_STA 3
#define SERIAL_TX_BUFFER_SIZE 128
#define SERIAL_RX_BUFFER_SIZE 128
class WIFIUNO
{
public:
bool begin(void);
//Initialize port
bool Initialize(byte mode, String ssid, String pwd, byte chl = 1, byte ecn = 2);
boolean ipConfig(byte type, String addr, int port, boolean a = 0, byte id = 0);
boolean Send(String str); //send data in sigle connection mode
boolean Send(byte id, String str); //send data int multiple connection mode
int ReceiveMessage(char *buf);
/*=================WIFI Function Command=================*/
void Reset(void); //reset the module
bool confMode(byte a); //set the working mode of module
boolean confJAP(String ssid , String pwd); //set the name and password of wifi
boolean confSAP(String ssid , String pwd , byte chl , byte ecn); //set the parametter of SSID, password, channel, encryption in AP mode.
String showMode(void); //inquire the current mode of wifi module
String showAP(void); //show the list of wifi hotspot
String showJAP(void); //show the name of current wifi access port
boolean quitAP(void); //quit the connection of current wifi
String showSAP(void); //show the parameter of ssid, password, channel, encryption in AP mode
/*================TCP/IP commands================*/
String showStatus(void); //inquire the connection status
String showMux(void); //show the current connection mode(sigle or multiple)
boolean confMux(boolean a); //set the connection mode(sigle:0 or multiple:1)
boolean newMux(byte type, String addr, int port); //create new tcp or udp connection (sigle connection mode)
boolean newMux(byte id, byte type, String addr, int port); //create new tcp or udp connection (multiple connection mode)(id:0-4)
void closeMux(void); //close tcp or udp (sigle connection mode)
void closeMux(byte id); //close tcp or udp (multiple connection mode)
String showIP(void); //show the current ip address
boolean confServer(byte mode, int port); //set the parameter of server
boolean setTimeout(int timeout); // Set the CIPSERVER timeout
private:
void print(const __FlashStringHelper *);
void print(const String &);
void println(const __FlashStringHelper *);
void println(const String &);
};
#endif
Atau tanpa library-library di atas(lewat AT Command)
#include <SoftwareSerial.h>
#define SSID "xxxxxxxx"
#define PASS "xxxxxxxx"
#define DST_IP "220.181.111.85" //baidu.com
SoftwareSerial dbgSerial(10, 11); // RX, TX
void setup()
{
// Open serial communications and wait for port to open:
Serial.begin(57600);
Serial.setTimeout(5000);
dbgSerial.begin(9600); //can't be faster than 19200 for softserial
dbgSerial.println("ESP8266 Demo");
//test if the module is ready
Serial.println("AT+RST");
delay(1000);
if(Serial.find("ready"))
{
dbgSerial.println("Module is ready");
}
else
{
dbgSerial.println("Module have no response.");
while(1);
}
delay(1000);
//connect to the wifi
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){while(1);}
delay(5000);
//print the ip addr
/*Serial.println("AT+CIFSR");
dbgSerial.println("ip address:");
while (Serial.available())
dbgSerial.write(Serial.read());*/
//set the single connection mode
Serial.println("AT+CIPMUX=0");
}
void loop()
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",80";
Serial.println(cmd);
dbgSerial.println(cmd);
if(Serial.find("Error")) return;
cmd = "GET / HTTP/1.0\r\n\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">"))
{
dbgSerial.print(">");
}else
{
Serial.println("AT+CIPCLOSE");
dbgSerial.println("connect timeout");
delay(1000);
return;
}
Serial.print(cmd);
delay(2000);
//Serial.find("+IPD");
while (Serial.available())
{
char c = Serial.read();
dbgSerial.write(c);
if(c=='\r') dbgSerial.print('\n');
}
dbgSerial.println("====");
delay(1000);
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
dbgSerial.println(cmd);
Serial.println(cmd);
delay(2000);
if(Serial.find("OK"))
{
dbgSerial.println("OK, Connected to WiFi.");
return true;
}else
{
dbgSerial.println("Can not connect to the WiFi.");
return false;
}
}
3 comments:
Very useful and precious thing as there is very low ratio of people which are used it. But in baking items use of this fruit is very essential and looks very great.
Video player for Mac
Cari situs terpercaya yang punya pelayanan terbaik nan ramah dan memiliki bonus terbesar di Indonesia? Langsung aja akses situsnya di Q168BET Berbagai Bonus Dan Promo New Member Berbagai Bonus Dan Promo New Member :
❂ Welcome Bonus SLOT ONLINE 100%
☯ Welcome Bonus SPORTSBOOK 100%
♠ Welcome Bonus KASINO ONLINE 100%
♪ WB New Member 4x TO
✪ Bonus Next Deposit 10%
✿ BONUS CB DAN ROLL MINGGUAN
- Minimal depo/wd: 25rb/50rb
Whatsapp : 0822 3210 0848
Link : www.Q168bet (DOT) com
Klik : Bonus New Member 100% - Q168BET | Agen Judi Game Slot Online Terpercaya di Indonesia
Seluruh Indonesia
Post a Comment