Created
December 28, 2014 08:38
-
-
Save teos0009/60d9d10b9441cc2bc0a2 to your computer and use it in GitHub Desktop.
SSR and Arduino Mega with ESP8266
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
//mod from https://github.com/Doomhammer458/ESP8266-LED-Server/blob/master/esp8266LED.ino | |
//#include <SoftwareSerial.h> | |
//use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). | |
#define SSID "the_ssid_here" | |
#define PASS "the_password_here" | |
#define DST_IP "220.181.111.85" //baidu.com | |
//SoftwareSerial dbgSerial(10, 11); // RX, TX | |
#define acPin 7 | |
char myChar; | |
void setup() | |
{ | |
// Open serial communications and wait for port to open: | |
//serial 2 is to esp8266 | |
Serial2.begin(9600);//9600 (mine), 57600, 115200 | |
Serial2.setTimeout(1000); | |
//serial 0 is to usb | |
Serial.begin(115200); | |
//mod1 otherwise module cant be ready | |
while(!Serial); | |
while(!Serial2); | |
Serial.println("ESP8266 control AC Demo on Mega2560"); | |
//mod1 otherwise module cant be ready | |
while(Serial2.available()>0) | |
Serial2.read(); | |
delay(1000); | |
//test if the module is ready | |
Serial2.println("AT+RST"); | |
delay(1000); | |
//delay(1000); | |
Serial.println("Resetting module"); | |
//mod1 | |
//Serial2.flush(); | |
if(Serial2.find("ready")) | |
//if(Serial2.find("Ready")||Serial2.find("ready")) | |
{ | |
//dbgSerial.println("Module is ready"); | |
Serial.println("Module is ready"); | |
} | |
else | |
{ | |
//dbgSerial.println("Module have no response."); | |
Serial.println("Module have no response."); | |
while(1);//hang here | |
} | |
delay(1000); | |
//connect to the wifi | |
boolean connected=false; | |
for(int i=0;i<5;i++) | |
{ | |
if(connectWiFi()) | |
{ | |
Serial.println(" WiFi connected"); | |
connected = true; | |
break; | |
} | |
} | |
if (!connected){ | |
Serial.println("No WiFi connected"); | |
while(1); | |
} | |
delay(5000); | |
//print the ip addr | |
Serial2.println("AT+CIFSR"); | |
Serial.println("IP address:"); | |
while (Serial2.available()) | |
Serial.write(Serial2.read()); | |
//set the single connection mode | |
//Serial2.println("AT+CIPMUX=0");//default single conn | |
Serial2.println("AT+CIPMUX=1");//led server multi conn | |
delay(1000); | |
Serial2.println("AT+CIPSERVER=1,8266"); | |
delay(1000); | |
Serial.println("Starting TCP Server"); | |
} | |
void loop() | |
{ | |
/* | |
//esp8266 as browser to open baidu.com | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += DST_IP; | |
cmd += "\",80"; | |
Serial2.println(cmd); | |
//dbgSerial.println(cmd); | |
Serial.println(cmd); | |
if(Serial2.find("Error")) return; | |
cmd = "GET / HTTP/1.0\r\n\r\n"; | |
Serial2.print("AT+CIPSEND="); | |
Serial2.println(cmd.length()); | |
if(Serial2.find(">")) | |
{ | |
//dbgSerial.print(">"); | |
Serial.print(">"); | |
}else | |
{ | |
Serial2.println("AT+CIPCLOSE"); | |
//dbgSerial.println("connect timeout"); | |
Serial.println("connect timeout"); | |
delay(1000); | |
return; | |
} | |
Serial2.print(cmd); | |
delay(2000); | |
//Serial.find("+IPD"); | |
while (Serial2.available()) | |
{ | |
char c = Serial2.read(); | |
//dbgSerial.write(c); | |
Serial.write(c); | |
//if(c=='\r') dbgSerial.print('\n'); | |
if(c=='\r') Serial.print('\n'); | |
} | |
//dbgSerial.println("===="); | |
Serial.println("===="); | |
delay(1000); | |
*/ | |
//waiting for cmd "LED" | |
if (Serial2.available()>0){ | |
if(Serial2.find("LED")){ | |
setLED(); | |
} | |
} | |
} | |
boolean connectWiFi() | |
{ | |
Serial2.println("AT+CWMODE=1"); | |
String cmd="AT+CWJAP=\""; | |
cmd+=SSID; | |
cmd+="\",\""; | |
cmd+=PASS; | |
cmd+="\""; | |
//dbgSerial.println(cmd); | |
Serial2.println(cmd); | |
Serial.println(cmd); | |
delay(2000); | |
if(Serial2.find("OK")) | |
{ | |
//dbgSerial.println("OK, Connected to WiFi."); | |
Serial.println("OK, Connected to WiFi."); | |
return true; | |
} | |
else | |
{ | |
//dbgSerial.println("Can not connect to the WiFi."); | |
Serial.println("Can not connect to the WiFi."); | |
return false; | |
} | |
} | |
void setLED(){ | |
int red = Serial.parseInt(); | |
int green = Serial.parseInt(); | |
int blue = Serial.parseInt(); | |
Serial.print("LED set. Red: "); | |
Serial.print(red); | |
Serial.print(" Green "); | |
Serial.print(green); | |
Serial.print(" Blue "); | |
Serial.println(blue); | |
//colorWipe(strip.Color(red,green,blue),200); | |
//simple test AC on/off for 2sec upon receive the command "LED" | |
Serial.println("Set AC on/off"); | |
digitalWrite(acPin,HIGH); | |
delay(2000); | |
digitalWrite(acPin,LOW); | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment