Skip to content

Instantly share code, notes, and snippets.

@maxpromer
Created May 29, 2017 12:52
Show Gist options
  • Save maxpromer/0f9e7a6fc08d5b0631612a037dd00947 to your computer and use it in GitHub Desktop.
Save maxpromer/0f9e7a6fc08d5b0631612a037dd00947 to your computer and use it in GitHub Desktop.
#include<WiFi.h>
#define WIFI_STA_NAME "xxxxxxxxxx"
#define WIFI_STA_PASS "xxxxxxxxxx"
#define LED_PIN 23
WiFiServer server(80);
void setup() {
Serial.begin(115200);
pinMode(LED_BUILTIN, OUTPUT);
pinMode(LED_PIN, OUTPUT);
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(WIFI_STA_NAME);
WiFi.mode(WIFI_STA);
WiFi.begin(WIFI_STA_NAME, WIFI_STA_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
server.begin();
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("new client");
client.Stream::setTimeout(100);
while (client.connected()) {
if (client.available()) {
String msg = client.readString();
digitalWrite(LED_PIN, (msg == "ON") ? HIGH : LOW);
client.print("OK");
Serial.println("Set LED to " + (String)((msg == "ON") ? "ON" : "OFF"));
}
delay(1);
}
client.stop();
Serial.println("client disonnected");
}
delay(10);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment