Last active
March 24, 2016 15:29
-
-
Save volca/69c559cfb50e5580668c to your computer and use it in GitHub Desktop.
Test wifi for cactus micro
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> | |
// Important!! We use pin 13 for enable esp8266 | |
#define WIFI_ENABLE_PIN 13 | |
#define DEBUG 1 | |
#define SSID "YOUR-WIFI-SSID" | |
#define PASS "YOUR-WIFI-SECRET" | |
char serialbuffer[100];//serial buffer for request command | |
int wifiConnected = 0; | |
SoftwareSerial mySerial(11, 12); // rx, tx | |
void setup() | |
{ | |
mySerial.begin(9600);//connection to ESP8266 | |
Serial.begin(9600); //serial debug | |
if(DEBUG) { | |
while(!Serial); | |
} | |
pinMode(13, OUTPUT); | |
digitalWrite(13, HIGH); | |
delay(1000);//delay | |
//set mode needed for new boards | |
mySerial.println("AT+RST"); | |
delay(3000);//delay after mode change | |
mySerial.println("AT+CWMODE=1"); | |
delay(300); | |
mySerial.println("AT+RST"); | |
delay(500); | |
} | |
boolean connectWifi() { | |
String cmd="AT+CWJAP=\""; | |
cmd+=SSID; | |
cmd+="\",\""; | |
cmd+=PASS; | |
cmd+="\""; | |
Serial.println(cmd); | |
mySerial.println(cmd); | |
for(int i = 0; i < 20; i++) { | |
Serial.print("."); | |
if(mySerial.find("OK")) | |
{ | |
wifiConnected = 1; | |
break; | |
} | |
delay(50); | |
} | |
Serial.println( | |
wifiConnected ? | |
"OK, Connected to WiFi." : | |
"Can not connect to the WiFi." | |
); | |
return wifiConnected; | |
} | |
void loop() | |
{ | |
if(!wifiConnected) { | |
mySerial.println("AT"); | |
delay(1000); | |
if(mySerial.find("OK")){ | |
Serial.println("Module Test: OK"); | |
connectWifi(); | |
} | |
} | |
if(!wifiConnected) { | |
delay(500); | |
return; | |
} | |
//output everything from ESP8266 to the Arduino Micro Serial output | |
while (mySerial.available() > 0) { | |
Serial.write(mySerial.read()); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment