- Tue Dec 29, 2015 5:52 am
#37368
2 ways to remotely access your network device over the internet is 1, thru Forwarding rules in your router, and 2, via an online server or a cloud service as others call it nowadays.
Forwarding Rules:
GengusKhan have used Forwarding rules in his router that is why his esp wifi is publicly accessible at: 82.5.78.180, on ports 80, 81, 82, 83, and others.
the code he posted is an arduino code running on his ESP8266 which will be executed when anyone access it at:
http://82.5.78.180:94/jsread.
Forwarding rules differ from one router brand to another as mentioned here. but their difference is not really that much.
Online servers / Cloud service:
In this approach, your wifi module will need to connect to an online server, no Forwarding required, send HTTP POST to upload data and HTTP GET, to request data.
HTTP POST is commonly used when you want to "remotely monitor" your wifi module. data is uploaded to a server that offers front end application like the normally usage for Thingspeak IOT, Freeboard.io, data.sparkfun, etc.
Front end apps are those graphs, gauges, and maps from these servers. These are used for monitoring.
HTTP GET on the other hand, is used when you to get data from a server. this data can be used to trigger an io of an ESP8266.
ie. when you get a data of "1", you turn you servo motor 90deg left. when you get a data of "0", you turn your servo motor 90deg right. thus, providing a way of "remote control".
Note: GET and POST on some servers works the same.
Remote monitoring apps like thingspeak iot and freeboard.io are very common now.
The front end apps for remote control is the one we dont normally get from these server.
So let us make a working example.
Tools needed:
Hosting site. a free one:
https://www.000webhost.com so anyone who wish to make wiil need not to spend an amount.
HTML, PHP, maybe Ajax, programming skill.
very basic will do.
helpful links:
http://www.w3schools.com/tags/att_button_formaction.asp http://html.net/tutorials/php/lesson16.php http://www.w3schools.com/ajax/tryit.asp ... ajax_first1. Create a hosting account:
https://members.000webhost.com once you have an account, create a domain account, and go thru the Control Panel (CPanel) so you can make files for the
server.
domain account created: villtech.comlu.com
CPanel opened. scroll down to File Manager
Inside File Manager. go to public_html folder. that is where we need to create files.
Files to be created:
index.html - HTML page
0.php - script to write "0" to data.json file
1.php - script to write "1" to data.json file
data.json - holds our data. will be accessed by ESP8266
HTML: index.html
Code: Select all<!DOCTYPE html>
<html>
<body>
<center>
<h3>Local Server<h3>
<button type="button" onclick="On()" >On</button>
<button type="button" onclick="Off()">Off</button>
</center>
<script>
function On() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/1.php", true);
xhttp.send();
}
function Off() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "/0.php", true);
xhttp.send();
}
</script>
</body>
</html>
it contains a little Ajax script. Ajax is used so that the whole page need not to refresh when a button is clicked
PHP script: 0.php - will write "0" to data.json
Code: Select all<?php
$myfile = fopen("data.json", "w") or die("Unable to open file!");
fwrite($myfile, "{0}");
fclose($myfile);
?>
PHP script: 1.php - will write "1" to data.json
Code: Select all<?php
$myfile = fopen("data.json", "w") or die("Unable to open file!");
fwrite($myfile, "{1}");
fclose($myfile);
?>
json: data.json
will be automatically created once we run our html.
Note: All files must be located in one location. For this example, in public_html
Once all is set, you can access the webpage now using the domain you created.
you can access mine at:
http://villtech.comlu.comWhen button "On" is clicked, "1" is written to data.json
When button "Off" is clicked, "0" is written to data.json
On ESP8266, you can read the content of data.json file using the sample Arduin Sketch "WiFiClient"
with this GET command:
Code: Select all client.print(String("GET ") + "/data.json" + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
where host = "your domain"
Once your esp get a "1" or "0", you should be able to control its io.
Using dweet.io for remote control application.
dweet.io provided url to:
write -
http://dweet.io/dweet/for/my-thing-name?hello=worldread -
http://dweet.io/get/latest/dweet/for/my-thing-namefiles on its server.
where: "my-thing-name" = file name
"hello=world" = data.
anything that follows "?" is your data to be written
here is an link to write "0" and "1" to file "esp8266" on dweet.io server:
http://villtech.comlu.com/dweet.htmldweet.html code
Code: Select all<!DOCTYPE html>
<html>
<body>
<center>
<h3>dweet.io Server<h3>
<button type="button" onclick="On()" >On</button>
<button type="button" onclick="Off()">Off</button>
</center>
<script>
function On() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://dweet.io/dweet/for/esp8266?1", true);
xhttp.send();
}
function Off() {
var xhttp = new XMLHttpRequest();
xhttp.open("POST", "http://dweet.io/dweet/for/esp8266?0", true);
xhttp.send();
}
</script>
</body>
</html>
on your esp, here is the GET command to read the content of "esp8266" from dweet.io:
Code: Select all client.print(String("GET ") + "/get/latest/dweet/for/esp8266" + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
where host = 52.0.196.251. dweet.io IP.
Reply of dweet to esp8266's request when "On" button from the sample is pressed
Reply of dweet to esp8266's request when "Off" button from the sample is pressed