Accessing NodeMCU HTTP server from a different network

All ESP8266 boards running MicroPython.
Official boards are the Adafruit Huzzah and Feather boards.
Target audience: MicroPython users with an ESP8266 board.
Post Reply
laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Accessing NodeMCU HTTP server from a different network

Post by laukejas » Thu May 02, 2019 5:32 pm

Hi,

I just started with NodeMCU and MicroPython. I have followed this tutorial (https://docs.micropython.org/en/latest/ ... k_tcp.html) to set up a HTTP server for simple LED switching.

My boot.py looks like this:

Code: Select all

import os

try:
  import usocket as socket
except:
  import socket

from machine import Pin
import network

import esp
esp.osdebug(None)

import gc
gc.collect()

ssid = 'my wifi SSID'
password = 'my wifi password'

station = network.WLAN(network.STA_IF)

station.active(True)
station.ifconfig
station.connect(ssid, password)

while station.isconnected() == False:
  pass

print('Connection successful')
print(station.ifconfig())
And my main.py looks like this:

Code: Select all

def web_page():
  if led.value() == 1:
    gpio_state="ON"
  else:
    gpio_state="OFF"
  
  html = """<html><head> <title>ESP Web Server</title> <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="icon" href="data:,"> <style>html{font-family: Helvetica; display:inline-block; margin: 0px auto; text-align: center;}
  h1{color: #0F3376; padding: 2vh;}p{font-size: 1.5rem;}.button{display: inline-block; background-color: #e7bd3b; border: none; 
  border-radius: 4px; color: white; padding: 16px 40px; text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}
  .button2{background-color: #4286f4;}</style></head><body> <h1>ESP Web Server</h1> 
  <p>GPIO state: <strong>""" + gpio_state + """</strong></p><p><a href="/?led=on"><button class="button">ON</button></a></p>
  <p><a href="/?led=off"><button class="button button2">OFF</button></a></p></body></html>"""
  return html

s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.bind(('', 80))
s.listen(5)

while True:
  conn, addr = s.accept()
  print('Got a connection from %s' % str(addr))
  request = conn.recv(1024)
  request = str(request)
  print('Content = %s' % request)
  led_on = request.find('/?led=on')
  led_off = request.find('/?led=off')
  if led_on == 6:
    print('LED ON')
    led.value(1)
  if led_off == 6:
    print('LED OFF')
    led.value(0)
  response = web_page()
  conn.send('HTTP/1.1 200 OK\n')
  conn.send('Content-Type: text/html\n')
  conn.send('Connection: close\n\n')
  conn.sendall(response)
  conn.close()
Now, upon boot, NodeMCU prints the local IP address, which is 192.168.0.133, which I can then access on the same WiFi network from my phone/computer, and switch the LED through the browser. But if I try to connect from another network (outside my home WiFi), then of course, I cannot access my NodeMCU.

My question is, how do I need to modify my code to be able to access my NodeMCU from anywhere in the world? Do I need a static IP address? I have read that there are several ways to achieve this, first being some port forwarding through the router (unfortunately, I do not have access to the router, and I have several mobile projects in mind where I will need to create NodeMCU server from other WiFi networks where I won't have access to router either). Second, I found that there are some services like aRest and ThingSpeak that somehow allow to do this too, but the libraries required to use them don't seem to be available for MicroPython.

So, what options are there? What's the simplest way to make NodeMCU accessible from anywhere without having to tinker with the router?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Accessing NodeMCU HTTP server from a different network

Post by jimmo » Sun May 05, 2019 6:37 am

Yeah, unfortunately 192.168.x.x is what's called a private address. Your router likely uses NAT to share your public address to the private addresses on your network.

You're right, port forwarding is the simplest solution, but would require router config. (And may not even work if your router's address isn't public either, e.g. because your ISP uses CG-NAT).

There are workarounds, but it's going to be much simpler to have some sort of central server running on the internet (e.g. any of the cloud hosting providers) that all your devices query, and you can deliver commands via that.

It also means that you can keep the code simpler on the ESP because it doesn't have to host the user interface, just the code to send/receive basic commands.

MQTT is one of many examples of this sort of thing. And there are many broker services already available (e.g. Adafruit Io, Amazon IoT, etc)

laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Re: Accessing NodeMCU HTTP server from a different network

Post by laukejas » Sun May 05, 2019 9:00 pm

[quote=jimmo post_id=36279 time=1557038268 user_id=3071]
Yeah, unfortunately 192.168.x.x is what's called a private address. Your router likely uses NAT to share your public address to the private addresses on your network.

You're right, port forwarding is the simplest solution, but would require router config. (And may not even work if your router's address isn't public either, e.g. because your ISP uses CG-NAT).

There are workarounds, but it's going to be much simpler to have some sort of central server running on the internet (e.g. any of the cloud hosting providers) that all your devices query, and you can deliver commands via that.

It also means that you can keep the code simpler on the ESP because it doesn't have to host the user interface, just the code to send/receive basic commands.

MQTT is one of many examples of this sort of thing. And there are many broker services already available (e.g. Adafruit Io, Amazon IoT, etc)
[/quote]

Thank you very much for replying, Jimmo! While I was waiting for an answer, I pretty much arrived at the same conclusion. I guess having an external server and ESP dumping data to it would be an easier solution.

I would like to ask, are there any limitations to this setup you proposed? I plan to try the same thing with ESP32, with a video camera attached to it. Is it viable to feed video data to a server on some cloud like you said, and have decent framerate and minimal delay when viewing that data on that server from another network? I mean, compared to connecting to ESP directly via same WiFi network.

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Accessing NodeMCU HTTP server from a different network

Post by jimmo » Tue May 07, 2019 3:20 am

I think that's much more a question of how fast your home internet is. Can do do video conferencing on your internet connection (e.g. skype, google hangouts, etc) -- that's basically the same thing.

I think the bigger question for video streaming (regardless if you're connected directly via wifi or over some server on the internet) is whether the ESP32 is powerful enough to do real time video encoding? (Don't know enough about the ESP32 to answer that).

laukejas
Posts: 29
Joined: Thu May 02, 2019 5:17 pm

Re: Accessing NodeMCU HTTP server from a different network

Post by laukejas » Tue May 07, 2019 9:44 am

jimmo wrote:
Tue May 07, 2019 3:20 am
I think that's much more a question of how fast your home internet is. Can do do video conferencing on your internet connection (e.g. skype, google hangouts, etc) -- that's basically the same thing.

I think the bigger question for video streaming (regardless if you're connected directly via wifi or over some server on the internet) is whether the ESP32 is powerful enough to do real time video encoding? (Don't know enough about the ESP32 to answer that).
Yes, my home internet should be fast enough for it. As for video streaming, ESP32 is definitely powerful enough for it, even though the quality and framerate are somewhat limited. Here's a video on one of ESP32 variants with integrated camera, streaming via STA connection: https://www.youtube.com/watch?v=MicAM_A0_lU There are other videoes showing how regular ESP32 works in this way with external cameras too.

Post Reply