Last active
March 16, 2018 12:34
-
-
Save teos0009/39d602dd809fd53edf3a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#define SSID "enter the SSID" | |
#define PASS "enter the password" | |
#define DST_IP "220.181.111.85" //baidu.com | |
SoftwareSerial dbgSerial(10, 11); // RX, TX | |
void setup() | |
{ | |
//setup RGB LED as indicator instead of softserial | |
pinMode(3, OUTPUT); | |
pinMode(4, OUTPUT); | |
pinMode(5, OUTPUT); | |
pinMode(6, OUTPUT); | |
digitalWrite(3, HIGH); | |
digitalWrite(4, HIGH); | |
digitalWrite(5, HIGH); | |
digitalWrite(6, HIGH); | |
// Open serial communications and wait for port to open: | |
Serial.begin(9600); | |
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"); | |
//blink green 1sec | |
digitalWrite(5, LOW); | |
delay(1000); | |
digitalWrite(5, HIGH); | |
} | |
else | |
{ | |
dbgSerial.println("Module have no response."); | |
//blink blue 1sec | |
digitalWrite(3, LOW); | |
delay(1000); | |
digitalWrite(3, HIGH); | |
while(1); | |
} | |
delay(1000); | |
//connect to the wifi | |
boolean connected=false; | |
for(int i=0;i<5;i++) | |
{ | |
if(connectWiFi()) | |
{ | |
connected = true; | |
//blink blue 1sec | |
digitalWrite(6, LOW); | |
delay(1000); | |
digitalWrite(6, HIGH); | |
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; | |
} | |
} |
I agree with renexdev, I'm a newbie, this just made me more confused. I don't know how to fix it. Please review the code.
Wiring - RX from ESP to digital pin 10, same - TX to 11. Power supply from 5V step down to 3.3V
I think this schematic works - http://www.martyncurrey.com/wp-content/uploads/2015/01/Arduino-to-ESP8266.jpg
And yes it goes on AT+RST dbgSerial, not Serial
"if (!connected){while(1);}" maybe better put some code for reset and indicator reset, on this point arduino gets frozen and requests manual reset
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Do you misspell some Serial vs dbgSerial.. check the line when you send a commnad.. Serial.println("AT+RST");