Sei sulla pagina 1di 8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

PetesTechProjects

LEDsControlthroughTTY

ThePlan
NowthatIhavebeenableto:
AutostartaserverusingtheESP8266andArduino(previouspost
(https://petestechprojects.wordpress.com/2014/12/03/autoesp8266serverinit/))
SendsimpledatabackandforththroughESP8266onmyWifinetwork(previouspost
(https://petestechprojects.wordpress.com/2014/11/30/simplecommunicationthrough
esp8266wifimodule/))
Parsedatafromaserialport(previouspost
(https://petestechprojects.wordpress.com/2014/12/04/arduinoparsingforoccurrenceof
stringfromserialport/)),IamgoingtomashthemtogetherandcontrolsomeLEDs.

HWsetup
IaddedablueLEDtomakethedemomorefun.HereiswhatIhavenow.

https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

1/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

(https://petestechprojects.files.wordpress.com/2014/12/update12_08_14.jpg)
Note:Istoppedusingthe9Vbattery(asIhadpostedearlier).Itwasgettingdrainedtofastso
usingthe5VoutputfromtheArduinoboardthatisthenregulateddownto3.3Vusingmy
homemodevariablevoltageregulator(describedinmypreviouspost:MiniPowersupply
fromLM317T(https://petestechprojects.wordpress.com/2014/12/01/minipowersupplyfrom
lm317t/))

SWsetup
Thecodeisprettystraightforwardifyouhavebeenfollowingmypreviouspostreferenced
aboveinThePlansection.Istartupthesever>waitforincomingstring>ifsomekey
wordsarefound,dosomethingtoIOsconnectedto3LEDs.Theactualcodeisatthebottom
ofthispost.

Testing
Thetestingstepswillbe:
1.StartuptheArduinoandESP8266withthecodebelowloaded.Thisshouldgetmy

https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

2/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

1.StartuptheArduinoandESP8266withthecodebelowloaded.Thisshouldgetmy
ESP8266onmyWifinetwork.
2.UseTeraTermtoopenarawTCP/IPconnection
3.TypeinkeywordstocontrolvariousLED.
4.WatchLEDlightshow.

Results
AllmystepsandfunctionsworkedprettymuchasIhadexpected.Besttojustshowtheresults
inavideo

TTY Control of LED's

Codeusedforthispost

https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

3/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

/*
Trytostarttheserverautonomously
*/
#include<SoftwareSerial.h>
#include<string.h>

#defineTIMEOUT5000//mS
#defineGREENLED4
#defineREDLED5
#defineBLUELED3

SoftwareSerialmySerial(7,6);//RX,TX

voidsetup()
{
pinMode(REDLED,OUTPUT);
pinMode(GREENLED,OUTPUT);
Serial.begin(9600);
mySerial.begin(9600);
mySerial.setTimeout(100);

intCommandStep=1;

//ThisinitializestheWifiModuleasaserver
BlinkLED(REDLED,CommandStep,50);
SendCommand("AT+RST","Ready",true);
BlinkLED(GREENLED,CommandStep,50);
CommandStep++;

BlinkLED(REDLED,CommandStep,50);
SendCommand("AT+GMR","OK",true);
BlinkLED(GREENLED,CommandStep,50);
CommandStep++;

delay(5000);

BlinkLED(REDLED,CommandStep,50);
SendCommand("AT+CIFSR","OK",true);
BlinkLED(GREENLED,CommandStep,50);
CommandStep++;

BlinkLED(REDLED,CommandStep,50);
https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

4/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

SendCommand("AT+CIPMUX=1","OK",true);
BlinkLED(GREENLED,CommandStep,50);
CommandStep++;

BlinkLED(REDLED,CommandStep,50);
SendCommand("AT+CIPSERVER=1,22","OK",true);
BlinkLED(GREENLED,CommandStep,50);
//

voidloop(){
StringIncomingString="";
charSingleChar;
booleanStringReady=false;

while(mySerial.available())
{
IncomingString=mySerial.readString();
StringReady=true;
}

//**********Onlyrunwhenstringisavailable************//
if(StringReady){
Serial.println("ReceivedString:"+IncomingString);

if((IncomingString.indexOf("xmas")!=1)||(IncomingString.indexOf("christ
intloopdelay=50;
for(intx=1;x<=50;x++){
digitalWrite(REDLED,HIGH);
delay(loopdelay);
digitalWrite(GREENLED,HIGH);
delay(loopdelay);
digitalWrite(BLUELED,HIGH);
delay(loopdelay);
digitalWrite(REDLED,LOW);
delay(loopdelay);
digitalWrite(GREENLED,LOW);
delay(loopdelay);
digitalWrite(BLUELED,LOW);
}
}

if((IncomingString.indexOf("red")!=1)||(IncomingString.indexOf("all")!
Serial.println("SomeonetryingtocontrolREDLED");
ProcessLEDCommand(REDLED,IncomingString);
}
https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

5/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

if((IncomingString.indexOf("green")!=1)||(IncomingString.indexOf("all")
Serial.println("SomeonetryingtocontrolGREENLED");
ProcessLEDCommand(GREENLED,IncomingString);
}
if((IncomingString.indexOf("blue")!=1)||(IncomingString.indexOf("all")
Serial.println("SomeonetryingtocontrolYELLOWLED");
ProcessLEDCommand(BLUELED,IncomingString);
}
}

//************************************************************
}

voidProcessLEDCommand(intLEDPin,StringIncomingString){
if(IncomingString.indexOf("on")!=1){
Serial.println("SomeonetryingtoturnonLED");
digitalWrite(LEDPin,HIGH);
}
if(IncomingString.indexOf("off")!=1){
Serial.println("SomeonetryingtoturnoffLED");
digitalWrite(LEDPin,LOW);
}
if(IncomingString.indexOf("blink")!=1){
Serial.println("SomeonetryingtoblinkLED");
BlinkLED(LEDPin,5,200);
}
}

voidBlinkLED(intLEDPin,intNumberOfBlinks,intOnDuration)
{
for(intx=1;x<=NumberOfBlinks;x++){
digitalWrite(LEDPin,HIGH);
delay(OnDuration);
digitalWrite(LEDPin,LOW);
delay(OnDuration);
}
}

booleanSendCommand(Stringcmd,Stringack,booleanhalt_on_fail)
{
mySerial.println(cmd);//Send"AT+"commandtomodule

//Otherwisewaitforack.
if(!echoFind(ack))//timedoutwaitingforackstring
https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

6/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects

if(halt_on_fail)
errorHalt(cmd+"failed");//Criticalfailurehalt.
else
returnfalse;//Letthecallerhandleit.
returntrue;//ackblankorackfound
}

//ReadcharactersfromWiFimoduleandechotoserialuntilkeywordoccursor
booleanechoFind(Stringkeyword)
{
bytecurrent_char=0;
bytekeyword_length=keyword.length();

//Failifthetargetstringhasnotbeensentbydeadline.
longdeadline=millis()+TIMEOUT;
while(millis()<deadline)
{
if(mySerial.available())
{
charch=mySerial.read();
Serial.write(ch);
if(ch==keyword[current_char])
if(++current_char==keyword_length)
{
Serial.println();
returntrue;
}
}
}
returnfalse;//Timedout
}

//Printerrormessageandloopstop.
voiderrorHalt(Stringmsg)
{
Serial.println(msg);
Serial.println("HALT");
digitalWrite(REDLED,HIGH);
digitalWrite(GREENLED,HIGH);
while(true){};
}

https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

7/8

11/03/2016

LEDsControlthroughTTY|PetesTechProjects
Abouttheseads(https://wordpress.com/abouttheseads/)

ThisentrywaspostedinUncategorizedandtaggedArduino,ESP8266.Bookmarkthepermalink.

2thoughtsonLEDsControlthroughTTY
Pingback:MiniServeronArduino+ESP8266|PetesTechProjects
Pingback:LEDsControlThroughESP8266+ArduinoWebPage|PetesTechProjects

BlogatWordPress.com.|TheSilesiaTheme.

https://petestechprojects.wordpress.com/2014/12/08/ledscontrolthroughtty/

8/8

Potrebbero piacerti anche