LED’s Control through TTY

The Plan

Now that I have been able to:

  • Auto start a server using the ESP8266 and Arduino (previous post)
  • Send simple data back and forth through ESP8266 on my Wifi network (previous post)
  • Parse data from a serial port (previous post), I am going to mash them together and control some LED’s .

HW setup

I added a blue LED to make the demo more fun.  Here is what I have now….

Update12_08_14

Note: I stopped using the 9V battery (as I had posted earlier).  It was getting drained to fast so using the 5V output from the Arduino board that is then regulated down to 3.3V using my home mode variable voltage regulator (described in my previous post: Mini Power supply from LM317T)

SW setup

The code is pretty straight forward if you have been following my previous post referenced above in “The Plan” section.  I start up the sever –> wait for incoming string –> if some key words are found, do something to IO’s connected to 3 LED’s.  The actual code is at the bottom of this post.

Testing

The testing steps will be:

  1. Start up the Arduino and ESP8266 with the code below loaded.  This should get my ESP8266 on my Wifi network.
  2. Use TeraTerm to open a raw TCP/IP connection
  3. Type in key words to control various LED.
  4. Watch LED light show.

Results

All my steps and functions worked pretty much as I had expected.  Best to just show the results in a video…

Code used for this post

/*
Try to start the server autonomously
 */
#include <SoftwareSerial.h>
#include <string.h>

#define TIMEOUT 5000 // mS
#define GREENLED 4
#define REDLED 5
#define BLUELED 3

SoftwareSerial mySerial(7, 6); // RX, TX



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

 int CommandStep = 1;
 
 //This initializes the Wifi Module as a server 
 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); 
 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);
 //----------------------------------------------------
 
}

void loop(){
 String IncomingString="";
 char SingleChar;
 boolean StringReady = false;
 

 while (mySerial.available()) 
 {
 IncomingString=mySerial.readString();
 StringReady= true; 
 }
 
 //********** Only run when string is available ************//
 if (StringReady){
 Serial.println("Received String: " + IncomingString); 

 if ((IncomingString.indexOf("xmas") != -1) || (IncomingString.indexOf("christmas") != -1 )) {
 int loopdelay = 50;
 for (int x=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") != -1)) {
 Serial.println("Someone trying to control RED LED");
 ProcessLEDCommand(REDLED, IncomingString);
 }

 if ((IncomingString.indexOf("green") != -1) || (IncomingString.indexOf("all") != -1)) {
 Serial.println("Someone trying to control GREEN LED");
 ProcessLEDCommand(GREENLED, IncomingString);
 }
 if ((IncomingString.indexOf("blue") != -1) || (IncomingString.indexOf("all") != -1)){
 Serial.println("Someone trying to control YELLOW LED");
 ProcessLEDCommand(BLUELED, IncomingString);
 }
 }



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

void ProcessLEDCommand(int LEDPin, String IncomingString){
 if (IncomingString.indexOf("on") != -1) {
 Serial.println("Someone trying to turn on LED");
 digitalWrite(LEDPin,HIGH);
 }
 if (IncomingString.indexOf("off") != -1) {
 Serial.println("Someone trying to turn off LED");
 digitalWrite(LEDPin,LOW);
 }
 if (IncomingString.indexOf("blink") != -1) {
 Serial.println("Someone trying to blink LED");
 BlinkLED(LEDPin,5,200); 
 }
}


void BlinkLED(int LEDPin, int NumberOfBlinks, int OnDuration)
{
 for (int x=1; x <= NumberOfBlinks ; x ++){
 digitalWrite(LEDPin,HIGH);
 delay(OnDuration);
 digitalWrite(LEDPin,LOW);
 delay(OnDuration); 
 }
}


boolean SendCommand(String cmd, String ack, boolean halt_on_fail)
{
 mySerial.println(cmd); // Send "AT+" command to module

 // Otherwise wait for ack.
 if (!echoFind(ack)) // timed out waiting for ack string
 if (halt_on_fail)
 errorHalt(cmd+" failed");// Critical failure halt.
 else
 return false; // Let the caller handle it.
 return true; // ack blank or ack found
}

// Read characters from WiFi module and echo to serial until keyword occurs or timeout.
boolean echoFind(String keyword)
{
 byte current_char = 0;
 byte keyword_length = keyword.length();
 
 // Fail if the target string has not been sent by deadline.
 long deadline = millis() + TIMEOUT;
 while(millis() < deadline)
 {
 if (mySerial.available())
 {
 char ch = mySerial.read();
 Serial.write(ch);
 if (ch == keyword[current_char])
 if (++current_char == keyword_length)
 {
 Serial.println();
 return true;
 }
 }
 }
 return false; // Timed out
}

// Print error message and loop stop.
void errorHalt(String msg)
{
 Serial.println(msg);
 Serial.println("HALT");
 digitalWrite(REDLED,HIGH);
 digitalWrite(GREENLED,HIGH);
 while(true){};
}
Advertisements

2 thoughts on “LED’s Control through TTY

  1. Pingback: Mini Server on Arduino + ESP8266 | PetesTechProjects

  2. Pingback: LED’s Control Through ESP8266 + Arduino Web Page | PetesTechProjects

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s