Sei sulla pagina 1di 3

Pubblicare il proprio IP pubblico utilizzando un modulo ESP8266 su una pagina internet in modo da

poter raggiungere la nostra LAN dall’esterno senza conoscere l’ip. Io l’ho utilizzato per raggiungere
una webcam dall’esterno senza pagare un servizio di dns dinamico

HOST= dove andate a salvare i vostri file php (esempio altervista offre spazio web gratuito e un db
gratis)

Rete=il nome della tua rete wifi

Pass= la tua password dela tua rete wifi (questa rimane memorizzata sul tuo arduino e non va a
finire in giro per la rete….)

Arduino choiamerà:

http://HOST/savemypubip.php //scrive l’ip address nel db

per memorizzare l’ip

Tu andrai a recuperarlo con questo comando:

http://HOST/readmypubip.php //recupera l’ip address

il database da creare ha una unica tabella:

ip
xxx.yyy.zzz.ddd

Questi i file da mettere sul sito HOST.

savemypubip.php :

<?php

include "dbConn.php"; // Using database connection file here

$MyIp=$_SERVER['REMOTE_ADDR'];

$update = mysqli_query($db,"UPDATE ipaddress SET ip = '$MyIp'");

if(!$update)

echo mysqli_error();

else
{

echo "Records updated successfully.";

mysqli_close($db); // Close connection

?>

readmypubip.php :

<?php

echo $_SERVER['REMOTE_ADDR'];

?>

Questo lo sketch da caricare su ESP8266:

#include <Arduino.h>

#include <ESP8266WiFi.h>

#include <ESP8266WiFiMulti.h>

#include <ESP8266HTTPClient.h>

#include <stdlib.h>

#include <WiFiClient.h>

ESP8266WiFiMulti WiFiMulti;

String myIP = String("");

void setup() {

Serial.begin(115200);

// Serial.setDebugOutput(true);

Serial.println();

Serial.println();

Serial.println();

for (uint8_t t = 4; t > 0; t--) {

Serial.printf("[SETUP] WAIT %d...\n", t);

Serial.flush();

delay(1000);

WiFi.mode(WIFI_STA);

WiFiMulti.addAP("rete", "password");

}
void loop() {

// wait for WiFi connection

if ((WiFiMulti.run() == WL_CONNECTED)) {

WiFiClient client;

HTTPClient http;

/* UPDATE MY IP ADDRESS IN TO THE DB*/

Serial.print("[HTTP] begin...\n");

if (http.begin(client, "http://HOST/savemypubip.php")) { // HTTP

Serial.print("[HTTP] GET...\n");

// start connection and send HTTP header

int httpCode = http.GET();

// httpCode will be negative on error

if (httpCode > 0) {

// HTTP header has been send and Server response header has been handled

Serial.printf("[HTTP] GET... code: %d\n", httpCode);

// file found at server

if (httpCode == HTTP_CODE_OK || httpCode ==


HTTP_CODE_MOVED_PERMANENTLY) {

String payload = http.getString();

Serial.println(payload);

} else {

Serial.printf("[HTTP] GET... failed, error: %s\n", http.errorToString(httpCode).c_str());

http.end();

} else {

Serial.printf("[HTTP} Unable to connect\n");

/* delay*/

delay(120000);

Potrebbero piacerti anche