Using WeMosD1 and Relay, Can I control from anywhere

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("");

}

No experience with your board but a ESP32 can do what you are suggesting.

I believe it will be the same with your board.

Look at Dashboards or ThingSpeak.

Just keep an eye on the current needed to operate the relay , it may make your board’s regulator get a bit hot.

If not using cloud based comms, you can poke a NAT hole in your firewall to reach the local processor from the internet.

have a look at Control-ESP8266-Over-the-Internet-from-Anywhere

It is just a pretty poor version of an ESP8266 board. Not at all compatible with a UNO for which it is supposed to be an alternative. Missing multiple I/O functions.

I would usually say that one really hopes he is not using "Vin" or the "Barrel jack"! :roll_eyes:

Except ...

This board actually has a real switchmode regulator in it! Take a look!

So that is not a problem at all! That part will work just fine from "Vin" or the "Barrel jack! :sunglasses:

I have not used this for a while, but did use it working on a project with a friend who was a beginner and it worked pretty well:

Thought something like that.


ThingSpeak for ESP32 (free if you keep the message count to ~3meg per year.

OP FYI
ESP32 cost only $10 :open_mouth:

myDevices/cayenne allows the creation of dashboards for display of sensor information and control of remote devices using buttons, sliders, etc.
Various communications options are available, e.g. I have used LoraWAN (using a The Things UNO), an ESP8266 using MQTT over WiFi and a GSM modem using MQTT

My setup :
The main controller board has server running on a different PORT : WiFiServer server(80); (Change the 80).
I forwarded the PORT on my router to the IP of the controller.
I use a no-ip to give me an address for MY intranet.
The software on my phone connects to the no-ip address:PORT, allowing access from anywhere. The controller connects to multiple other controllers throughout the property giving control of devices around the property.

No. "192.168.x.x" is a non-routable address. You can't use that address outside your home WiFi.

What you CAN do is set up port mapping on your internet router. You can tell the router that when a connection comes in for "port 8080", for example, to forward that request to port 80 on "192.168.1.223". Then you can use your phone anywhere you have an internet connection to connect to "YourHomeInternetAddress:8080" and you will be connected to the WeMos.

Since your ISP may change your router's internet address from time to time you might want to set up DynamicDNS. A small program running on your home PC will watch out for changes in your Internet Address and update the DynamicDNS name server so your chosen name continues to point to your home router. That way you can use the name and not worry about number changes. I use NoIP.com for DynamicDNS. You can use it for free for up to three names.

Not the sort of rogue ISP you would really want to be using of course! :astonished:

Thank you ALL for your comments and suggestions.

Some Data for My Components:
Router Gateway (IP address): 192.168.1.1
Router IPv4 address: 192.168.1.239
WeMosD1WiFi IP address: 192.168.1.223
Router PORT number: 80
(Note: I think it’s 80, since when I run my sketch with port of 80 it compiles correctly; if I pick any other number, I get an error)
FYI……I tried cmd netstat –a and got lots of lines with 192.168.1.239 (but I don’t understand the readout); also it did not show 192.168.1.1

Based on your various post comments, it appears I need to do a Port Forwarding within my Router.

Three (3) questions please;

(1) On my iPhone, I downloaded the MySpectrum App, and logged-in and found and opened Port Forwarding Screen. That screen, asks for the following
(what should I put in these ?)

IP Address ?
Internal Port number ?
External Port number ?

After I fill-in the Port Forwarding info:

(2) Do I have to change the port number in my Sketch ?

(3) When I’m away from home, what “IP address” do I then type into my iPhone to access (display) the “Turn on/off” screen ?

If I mis-interpreted some of your posts, please let me know.

I know this is a simplistic project, to turn on/off a light, but it will open lots of Arduino application possibilities for me in the future (as I try to advance from Newbie both in the Forum and with actual projects).

"IP Address" is the address your sketch shows in "Use this URL : HTTP://address/" Right now it's "192.168.1.223" but it may change if your DHCP server (probably the router) assigns that address to some other device.

"Internal Port number" is 80, the port of your server.

"External Port Number" is a value you pick. You can pick 80. Most people pick "8080" or something similar to distinguish it from some other public web server at your IP address.

No. The port forwarding will translate between the external port and internal port.

Like I said: If you set External Port to 8080, use "HTTP://YourHomeInternetAddress:8080/". From home, ask Google 'What is my IP address?'.

if you wish to access your device using port forwarding it is wise to assign it a static IP address otherwise the IP assigned by DHCP may change and port forwarding fails
my router appears to use IP addresses 192.168.1.64 to 192.168.1.253 for DHCP
up to 192.168.1.63 are for static IP addresses

John, as you see, I had a lot of info from everyone's postings; again thank you all. I have to take this in steps so as to not get overwhelmed.

OK, I did do a PORT FORWARDING as follows:
IP Address 192.168.1.223
Internal Port number 80
External Port number 8080

I also, Googled on my Home PC, what is my IP address and got a long string of numbers and letters,

Starting with 2600:6c64: and Ending with :f:5deb stating that it was my Public IP Address

So, If I am understanding this correctly (??), when I am away from home, when I open a browser (for example Google Chrome) on my iPhone

do I type this in:

http:// the long string of letters and numbers and simply tack-on :8080 to the end of it ?

If so, when I do that, would that send me directly to the menu saying Click here to turn on LED or will it take me to a different screen?

Sorry for these basic questions, however this would be my very first attempt to communicate with my home and turn on an LED (ie, like doing an Internet of Things)

that sounds like an IPv6 address
to get your IPv4 address to https://www.iplocation.net/ or login to your router which will list the external IPv4 address
Your internet provider may allocate a dynamic IP address which if you loose your connection may change. You may be able to be allocated a static IP address.

As I said in #11. :roll_eyes:

Yes. That should work. Good luck.

wireshark is a useful tool for monitoring network traffic

A bit late getting back to you all.

Horace was correct, the “long string” that I got from google, (Starting with 2600:6c64: and Ending with :f:5deb) is a Temporary IPv6 Address

(I just verified that by running CMD>ipconfig on my computer) …. Hence not an address to be used in any Address Bar )

To recap: I did PORT FORWARDING as follows:
Local IP Address 192.168.1.223 (my WeMosD1 unit)
Internal Port number 80 (also the Port used in my Sketch)
External Port number 8080

My Home Spectrum Router Application shows it has a
Public IP Address 68.xxx.yyy.zzz and
I verified this by using NordVPN and it gave the same IP Address. (Not knowing any better, I assume it’s best not to show my home ip address on this forum or anywhere else, so I used x,y,z))

So I believe this is my Home Internet IP Address

Now, using the posted suggestion:
"HTTP://YourHomeInternetAddress:8080/

I typed: http://68.xxx.yyy.zzz:8080/ Result: "Safari cannot open the page because it could not connect to the server"

Can someone advise if I’m making a mistake or if there is something different I should type into the address bar on my computer to access my WeMosD1 via internet ?

If I understand it correctly, this WeMosD1 unit is now a “local server” - - since I could access its internal local IP address "from my iPhone" at home and was able to turn on/off LED (I’ll call this successful Step-One)

My Step-Two goal is to be able to access the WeMos local server “from the internet”.

Relative to the other posted items, I’m assuming (???) after having read about these, that Blynk, no-ip, Cayenne, and creating a “static” ip address…….appear to all be enhancements to make things easier in case my ip changes, AND to better illustrate things on my iPhone screen someday with buttons and sliders, etc.

If that assumption is correct, then my final assumption (?????) based on postings here, is that I should be able to access the WeMosD1 local server “without relying on” any of the enhancements, and just access via the computer's browser address bar

(I would certainly follow up with enhancements for sure, after I first access it via the Internet on my iPhone.)