Hi, I created my first IoT device (WeMosD1 with 5V relay)....it's just a test.
It works great, I can turn on/off a 110vac Lamp from my iPhone.
The device and my iphone are currently all on the house SSID.
Being an Arduino newbie, my question is can I go anywhere and use my iPhone to turn on the Lamp.
For example, let's say the WeMos / Relay with a 5 volt wall cube plugged-in remain at my home.
If I go miles from my home, can I use my iphone "there", to access the WeMos 192.168.1.223 server remotely and turn the lamp on/off ?
If not...... do I need more hardware and/or an edit to my sketch?
This is all new to me....I was thrilled to use my iphone to turn this lamp on. As they say on the internet, it opens up endless possibilities.
Should you need it, I've attached a photo of my hardware and included my Sketch
/*
ESP8266 (WeMosD1) WiFi Relay Control
learnelectronics
05 JUN 2017
www.youtube.com/c/learnelectronics
arduino0169@gmail.com
*/
// (NOTE: with LED loop)
// (SerialMonitor OK "Server Listening")
// (iPhone>Safari>click Apple ICON> MUST VIP "type-in the 192#" OK "Led pin is REALLY now: on"
//
#include <ESP8266WebServer.h>
#include <ESP8266WiFi.h>
const char* ssid = "my_SSID";
const char* password = "my_PASSWORD";
int ledPin = D5;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL : ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
}
void loop() {
// Check if a client has connected
WiFiClient client = server.available();
if (!client) {
return;
}
// Wait until the client sends some data
Serial.println("new client");
while (!client.available()) {
delay(1);
}
// Read the first line of the request
String request = client.readStringUntil('\r');
Serial.println(request);
client.flush();
// Match the request
int value = LOW;
if (request.indexOf("/LED=ON") != -1) {
digitalWrite(ledPin, HIGH);
value = HIGH;
}
if (request.indexOf("/LED=OFF") != -1) {
digitalWrite(ledPin, LOW);
value = LOW;
}
// Return the response
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println(""); // do not forget this one
client.println("<!DOCTYPE HTML>");
client.println("<html>");
client.print("The LED pin is now: ");
if (value == HIGH) {
client.print("On");
} else {
client.print("Off");
}
client.println("<br><br>");
client.println("Click <a href=\"/LED=ON\">here</a> Turn LED ON<br>"); //THIS APPEARS ON iPHONE
client.println("<br><br>");
client.println("Click <a href=\"/LED=OFF\">here</a> Turn LED OFF<br>"); //THIS APPEARS ON iPHONE
client.println("</html>");
delay(1);
Serial.println("Client disconnected");
Serial.println("");
}

