1
#define id "qwerty"
#define password"123456789"
int relayPin= 4;
void setup()
{
  Serial.begin(115200);

  Serial.println("AT");
  pinMode(13, OUTPUT);
  pinMode(relayPin, OUTPUT);
  delay(3000);

  if (Serial.find("OK")) {
    Serial.println("AT+CWMODE=1");
    delay(2000);
    String connectionCommand= String("AT+CWJAP=\"") + id+ "\",\"" + password+ "\"";
    Serial.println(connectionCommand);
    delay(5000);
    Serial.println("AT+CIFSR");
    delay(50);
    Serial.println(Serial.read());
  }

  Serial.print("AT+CIPMUX=1\r\n");
  delay(200);
  Serial.print("AT+CIPSERVER=1,80\r\n");
  delay(1000);
}
void loop() {
  if (Serial.available() > 0) {
    if (Serial.find("+IPD,")) {
      String metin = "<head> Hello World </head>";
      metin += "<br><a href=\" ?pin=on\"><button type='button'>ON</button></a>";
      metin += "<br><a href=\" ?pin=off\"><button type='button'>OFF</button></a>";
      String cipsend = "AT+CIPSEND=";
      cipsend += "0";
      cipsend += ",";
      cipsend += metin.length();
      cipsend += "\r\n";
      Serial.print(cipsend);
      delay(500);
      Serial.println(text);
      lock_open_close();
      //Serial.println("AT+CIPCLOSE=0");

    }
  }
}

void lock_open_close() {
  String coming= "";
  char readonserial;
  while (Serial.available() > 0) {
    readonserial= Serial.read();
    coming+= readonserial;

  }
  Serial.println(coming);
  if ((coming.indexOf(":GET /?pin=on") > 1)) {
    digitalWrite(13, HIGH);
    digitalWrite(relayPin, HIGH);
    delay(200);
  }
  if ((coming.indexOf(":GET /?pin=off") > 1)) {
    digitalWrite(13, LOW);
    digitalWrite(relayPin, LOW);
    delay(200);
  }
}

Here's my code. I connected ESP8266 with a relay and a solenoid lock, so that I am expecting to control the lock with the buttons. But ESP8266 disconnects itself. Any suggestions?

Thanks a lot. Sincerely, Y.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.