Go Down

Topic: Wifi control with esp8266 (Read 381 times) previous topic - next topic

Rieny

Hey,

I am currently working on an LED strip project. I have a boring closet with open cabinets that I want to pimp with an LED strip that I can control with my smartphone.

I bought an esp8266 esp-01 to connect to my arduino Mega, which is connected to the led strip. The code I am now using, is a code I found online however it doesn't really do exactly what I want and can't seem to fix it. I want to be able to enter my IP address together with a command via a URL and then have the arduino read the Serial monitor and have it know what to do. However right now it saves the incoming command in a string variable and than I use indexOf() to find that command in that string. If it is found an if statement makes sure that the command is executed.

Code: [Select]

#define DELAY 5000 // mS
#define LED 3

const byte button = 53;
int button_state = 0;
int state;

void setup()
{
  pinMode(LED,OUTPUT);
  pinMode(button,INPUT);

  Serial.begin(115200);
  Serial1.begin(115200);

   SendCommand("AT+RST", "Ready");
   delay(DELAY);
   SendCommand("AT+CWMODE=1","OK");
   SendCommand("AT+CIFSR", "OK");
   SendCommand("AT+CIPMUX=1","OK");
   SendCommand("AT+CIPSERVER=1,80","OK");
}

void loop(){
 button_state = digitalRead(button);
 
 if(button_state == HIGH){
    Serial1.println("AT+CIPSEND=0,23");
    Serial1.println("<h1>Button was pressed!</h1>");
    delay(1000);
    SendCommand("AT+CIPCLOSE=0","OK");
  }
 
 String IncomingString="";
 boolean StringReady = false;

 
 while (Serial1.available())
  {
   IncomingString=Serial1.readString();
   StringReady= true;
  }
 
  if (StringReady){
   
    Serial.println("Received String: " + IncomingString);
   
  if (IncomingString.indexOf("LED=1") != -1) {
    state = 1;
  }
  if (IncomingString.indexOf("LED=2") != -1) {
    state = 2;
   }
   if (IncomingString.indexOf("LED=3") != -1) {
    state = 3;
   }
   if (IncomingString.indexOf("LED=4") != -1) {
    state = 4;
   }
  }
  switch(state)
  {
    case 2:
      pulse();
      break;
      case 1:
      analogWrite(LED,49);
      break;
      case 3:
      analogWrite(LED,255);
      break;
      case 4:
      analogWrite(LED,230);
      break;
  }
 
}

boolean SendCommand(String command, String response)
{
  Serial1.println(command); // Send "AT+" command to module
  if (!echoFind(response))// timed out waiting for response string
    {
      return true; // response found
    }
}

boolean echoFind(String keyword){
 byte current_char = 0;
 byte keyword_length = keyword.length();
 long deadline = millis() + DELAY;
 while(millis() < deadline) //set a timer of 5 seconds. If no response within 5 seconds end function
 {
  if (Serial1.available()) //if Serial1.available == true
  {
    char ch = Serial1.read();
    Serial.write(ch);
    if (ch == keyword[current_char])
      if (++current_char == keyword_length){
       Serial.println();
       return true;
    }
   }
  }
 return false; // Timed out
}

void pulse()
{
  for(int i = 0; i < 255; i++)
  {
    analogWrite(LED, i);
    delay(50);
    if (Serial1.available()) break;
  }
  for(int i = 255; i > 0; i--)
  {
    analogWrite(LED,i);
    delay(50);
    if (Serial1.available()) break;
  }
}


So if I enter "[IP-address]/LED=1" the entire text that comes in (it includes some random stuff I don't understand) is saved in IncomingString. Then with indexOf() it searches IncomingString for a known command, in this case it will find "LED=1" and start the programm associated with LED=1. However I want to send for example RGB values. However if I were to do that in the same way I'd need 255 if statements for R, B and G. That sounds like the worst possible code ever.

Does anyone know how to write this code better?
Thanks in advance

Go Up