Remotely access to my esp8266 device via web-interface


#1

Hi there! I would like to ask, does anybody can access to your esp8266 device via web-interface after flashing firmware from “Door Sensor” or “Sprinkler System” guides?
I tried to get access to devices via web-interface using address from code example below and i also tried to use ip address of device instead “HOSTNAMEOFDEVICE.local”, but it didn’t work.
By the way i can ping my devices via CLI. Thank you!

After flashing the code once you can remotely access your device by going to http://HOSTNAMEOFDEVICE.local/firmware

  • obviously replace HOSTNAMEOFDEVICE with whatever you defined below. The user name and password are also defined below.

#2

Works on my LED controller. Haven’t tried the door sensor or sprinkler system yet, but same premise.

skip the .local at the end and maybe just do http://ipaddressofdevice instead. If that works, DHCP may not be handing off correct DNS information to your ESP8266, or if it’s manually assigned, you may have a different DNS issue.


#3

Hi, friend! I just wrote that i tried to use the ip address of my device instead HOSTNAMEOFDEVICE. I thought maybe problem is in my eps8266 device? I use a NodeMCU V3 with the last basic firmware. It’s code, which i used.

#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

/* ---------- DO NOT EDIT ANYTHING ABOVE THIS LINE ---------- */

//Only edit the settings in this section

/* WIFI Settings /
// Name of wifi network
const char
ssid = “IoT”;

// Password to wifi network
const char* password = “Qq111111”;

/* Web Updater Settings /
// Host Name of Device
const char
host = “MK-SprinklerSystem1”;

// Path to access firmware update page (Not Neccessary to change)
const char* update_path = “/firmware”;

// Username to access the web update page
const char* update_username = “admin”;

// Password to access the web update page
const char* update_password = “password”;

/* MQTT Settings /
// Topic which listens for commands
char
subscribeTopic = “MK-SmartHouse/utilities/MK-SprinklerSystem1”;

//MQTT Server IP Address
const char* server = “192.168.100.4”;

//Unique device ID
const char* mqttDeviceID = “MK-SmartHouseDevice1”;

/* ---------- DO NOT EDIT ANYTHING BELOW THIS LINE ---------- */

int channel1 = D4;

//webserver
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

//MQTT
WiFiClient net;
MQTTClient client;

unsigned long lastMillis = 0;

//Connect to WiFI and MQTT
void connect();

//Setup pins, wifi, webserver and MQTT
void setup()
{
// set pin modes
pinMode(channel1, OUTPUT);
digitalWrite(channel1, LOW);

WiFi.mode(WIFI_STA);

WiFi.begin(ssid, password);
client.begin(server, net);
client.onMessage(messageReceived);

connect();

MDNS.begin(host);

httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();

MDNS.addService(“http”, “tcp”, 80);
}

//Connect to wifi and MQTT
void connect()
{
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}

while (!client.connect(mqttDeviceID))
{
delay(1000);
}

client.subscribe(subscribeTopic);
}

void loop()
{
// MQTT Loop
client.loop();
delay(10);

// Make sure device is connected
if(!client.connected())
{
connect();
}

httpServer.handleClient();

}

// Change the state of a relay based on the MQTT Message
void messageReceived(String &topic, String &payload)
{
String msgString = payload;

if (msgString == “CH1ON”)
{
digitalWrite(channel1, HIGH);
delay(250);
}
else if (msgString == “CH1OFF”)
{
digitalWrite(channel1, LOW);
delay(250);
}
}


#4

I also tried to upload “WebUpdater” example from “ESP8266HTTPUpdateServer” library and it works properly.
I have no idea what can cause the problem.

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

const char* host = “esp8266-webupdate”;
const char* ssid = “…”;
const char* password = “…”;

ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

void setup(void){

Serial.begin(115200);
Serial.println();
Serial.println(“Booting Sketch…”);
WiFi.mode(WIFI_AP_STA);
WiFi.begin(ssid, password);

while(WiFi.waitForConnectResult() != WL_CONNECTED){
WiFi.begin(ssid, password);
Serial.println(“WiFi failed, retrying.”);
}

MDNS.begin(host);

httpUpdater.setup(&httpServer);
httpServer.begin();

MDNS.addService(“http”, “tcp”, 80);
Serial.printf(“HTTPUpdateServer ready! Open http://%s.local/update in your browser\n”, host);
}

void loop(void){
httpServer.handleClient();
}


#5

Might want to hide your passwords in the sketches you pasted. Let me compare to the sketch Matt provided for the LED controller.


#6

Looked at the web server, wifi, and MDNS code, it’s basically identical as Matt’s. All of your includes are the same. I’m no pro with the code though, maybe someone else will notice something and comment.

If you can ping via CLI, they’re definitely on the network, so it must be webserver related. If you can ping via hostname, then it’s not related to DNS.

Hmm… wish I could help more. :frowning:


#7

Don’t worry it’s not my password. Just look at the dump of wireshark…

    • WebUpdater Example

#8

I tried to find differences between these examples of code and shrinked it as much as i can. It looks like Matt’s code and easy to understrand, but doesn’t want to work properly. Maybe the problem in connect() function?

#include <ESP8266WiFi.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>

const char* ssid = “IoT”;
const char* password = “Qq111111”;

const char* update_path = “/update”;
const char* update_username = “admin”;
const char* update_password = “password”;

ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

void setup(){

WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
httpUpdater.setup(&httpServer, update_path, update_username, update_password);
httpServer.begin();
}

void loop(){
httpServer.handleClient();
}


#9

Honestly, since the includes are the same, I’d wager you could copy+paste Matt’s right in there.

Wireshark is definitely showing webserver is not even acknowledging. You’re on the right track. I’m still not sure why though, that’s really odd.

EDIT:

I take that back, it is acknowledging. But it’s doing nothing and you’re having to retransmit.


#10

ESP8266 just transmeted RST(Reset) to my compurter and close tcp session every time when i tried to get a web-page.


#11

The reason is that 80 port doesn’t work and it means webserver is not working properly. I can’t understand how work this lines of code:


void connect();

connect();

so maybe it’s time to write my own code…


#12

Finally i solved this problem. As i said earlier port 80 didn’t respond, it mean that problem in http server configuration. The reason is void connect() function, which used many time and contains:

void connect()
{
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
}

while (!client.connect(mqttDeviceID))
{
delay(1000);
}

client.subscribe(subscribeTopic);
}

So if it’s impossible to connect to your MQTT-broker, your code will go in cycle here:

while (!client.connect(mqttDeviceID))
{
delay(1000);
}

  1. Port 80 won’t be open.
  2. TCP handshake won’t be established.
  3. Cycle;

P.S. I just forgot to switch on my MQTT-broker ;D


#13

Can you explain " turning on your MQTT broker"


#14

Sorry? What exactly do you misunderstand?


#15

I am not a coder so like many here call me a Copy and Paster. CAP… Ha!
Simply your last comment " P.S. I just forgot to switch on my MQTT-broker ;D" , may be my problem.
When I use my Widows laptop I can verify the open and closed states of my door switch with MQTT so that leads me to think my broker is working.
In Openhabian local or remote I see the door switch icons etc however the state does not change.
Where did you “switch on the MQTT broker” as I assumed this checked out with the Windows MQTT test.
Thanks for your help.


#16

I mean, in that case my mqtt server was down (Raspberry Pi was switch off).
About your problem. Did you connect your openhab using mqtt binding to MQTT broker mosquitto?


#17

I am in the process of redoing the whole setup with a Raspberry Pi 3 so when I get to this point I will pay special attention to your suggestion.
Initially I set up everything on a Pi 3 and established a static IP in my router.
I then transferred the SD card to a Pi Zero W and set the router associate the Pi Zero W MAC to the same IP.
(no ethernet socket on a Pi zero)
The ESP 8266 switch operation works using the Pi Zero and MQTT on my Windows laptop but Openhabian does not respond.
I will post my results as soon as I can.

June 10 2018
I spared up a Rasp Pi 3 and reprogrammed it with Matts instructions.
I would like to refer to text instructions in https://www.mksmarthouse.com/setupinstallationwindows
refer to
Connecting The MQTT Mosquito server to OpenHAB 2:
steps
2. Then click paper ui, paper ui is the web administration pannel for the automation server
3. Add ons
4. In the search box type in MQTT
5. Next to where it says MQTT Binding click install wait for it to install

Instead!!

  1. Then click paper ui, paper ui is the web administration pannel for the automation server
  2. Add ons
    3A Click on BINDINGS<<<<<<<<<<<<<<<<<<<<<<<<<<
  3. In the search box type in MQTT
  4. Next to where it says MQTT Binding click install wait for it to install

This may be an Openhabian change since Matt wrote the directions but if you do a search without first selecting BINDINGS it will display MQTT ACTION
image

This is what we want:

image

I am not absolutely certain that this was my problem nor am I saying the Pi Zero was a problem however everything works now.

I hope this helps someone.


Need help setting up raspberry pie home network got stuck on static ip address