Skip to content

Instantly share code, notes, and snippets.

@palmerabollo
Created May 14, 2018 23:34
Show Gist options
  • Save palmerabollo/ed379dcc5af0c0440da69a53e02e8b69 to your computer and use it in GitHub Desktop.
Save palmerabollo/ed379dcc5af0c0440da69a53e02e8b69 to your computer and use it in GitHub Desktop.
HTTP server with ESP32/ESP8266
// Legacy HTTP-based approach.
// For it to work, you need to call server.begin(); in the setup function and declare a WiFiServer server(80);
// see https://github.com/espressif/arduino-esp32/blob/master/libraries/WiFi/examples/SimpleWiFiServer/SimpleWiFiServer.ino
if (incomingClient) {
Serial.println("client connect");
digitalWrite(LED_PIN, LOW); // LOW = ON
while (incomingClient.connected()) {
if (incomingClient.available() > 0) {
char command = incomingClient.read();
switch (command) {
case '0':
digitalWrite(RELAY_PIN, LOW);
break;
case '1':
digitalWrite(RELAY_PIN, HIGH);
break;
case '\n':
incomingClient.stop();
break;
}
}
}
incomingClient.stop();
Serial.println("client disconnect");
digitalWrite(LED_PIN, HIGH); // HIGH = OFF
}
@palmerabollo
Copy link
Author

palmerabollo commented May 15, 2018

client

  WiFiClient client;
  if (!client.connect(HOST, PORT)) {
    Serial.println("connection failed");
    return;
  }

  client.print("temp:");
  client.print(temperature);
  client.stop();

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment