Hello there!
So basically I have 2 WeMos D1 r3 boards, which are connected through 2 rooters with server-client connection with a local static IP. Both nodes are far apart(around 2km), but each one has router and cable connects them.
So in the Server, I do the normal connection:
-----------------Server code-----------------------
#include <SPI.h>
#include <ESP8266WiFi.h>
byte ledPin = 2;
char ssid[] = "ssid"; // SSID of your home WiFi
char pass[] = "password"; // password of your home WiFi
unsigned long IntervalIntranet = 120000;
unsigned long PreviousMillis = 0;
String request;
WiFiServer server(80);
IPAddress ip(192, 168, 0, 80); // IP address of the server
IPAddress gateway(192,168,0,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
void setup() {
Serial.begin(115200); // only for debug
Serial.println("Connecting...");
WiFi.config(ip, gateway, subnet);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(500);
};
Serial.println("---Connected!---");
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // some parameters from the network
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
Serial.print("Networks: "); Serial.println(WiFi.scanNetworks());
server.begin();
pinMode(ledPin, OUTPUT);
}
void loop () {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
client.print("Hi client! No, I am listening. ");
client.print(analogRead(A0));
client.println("r"); // sends the answer to the client
request = client.readStringUntil('r'); // receives the message from the client
Serial.print("Distance: "); Serial.println(request);
client.flush();
digitalWrite(ledPin, HIGH);
}
client.stop(); // tarminates the connection with the client
};
}
----------END of Server code------------------
On the client-side I have the following code:
--------------Client code begin------------------
#include <SPI.h>
#include <ESP8266WiFi.h>
#define echoPin 12 // Echo Pin D6
#define triggerPin 13 // Trigger Pin D7
long duration, distance;
byte ledPin = 2;
char ssid[] = "ssid"; // SSID of your home WiFi
char pass[] = "password"; // password of your home WiFi
unsigned long askTimer = 0;
IPAddress server(192, 168, 0, 80); // IP address of the server
IPAddress gateway(192,168,0,1); // gateway of your network
IPAddress subnet(255,255,255,0); // subnet mask of your network
WiFiClient client;
void setup() {
Serial.begin(115200); // only for debug
//init the Ultrasonic sensor pins
pinMode(triggerPin, OUTPUT);
pinMode(echoPin, INPUT);
WiFi.mode(WIFI_STA);
WiFi.config(server,gateway,subnet);
WiFi.begin(ssid, pass); // connects to the WiFi router
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
};
//end init Ultrasonic sensor Pins
Serial.println("Connected to wifi");
Serial.print("Status: "); Serial.println(WiFi.status()); // Network parameters
Serial.print("IP: "); Serial.println(WiFi.localIP());
Serial.print("Subnet: "); Serial.println(WiFi.subnetMask());
Serial.print("Gateway: "); Serial.println(WiFi.gatewayIP());
Serial.print("SSID: "); Serial.println(WiFi.SSID());
Serial.print("Signal: "); Serial.println(WiFi.RSSI());
pinMode(ledPin, OUTPUT);
}
void loop () {
client.connect(server, 80); // Connection to the server
digitalWrite(ledPin, LOW); // to show the communication only (inverted logic)
Serial.println(".");
//Section detect the ultrasonic wave
digitalWrite(triggerPin, LOW);
delayMicroseconds(2);
digitalWrite(triggerPin, HIGH);
delayMicroseconds(10);
digitalWrite(triggerPin, LOW);
duration = pulseIn(echoPin, HIGH);
//Calculate the distance (in cm) based on the speed of sound.
distance = duration/58.2;
Serial.println(distance);
//Delay 50ms before next reading.
delay(50);
//End Section wave detection from the Ultrasonic sensor
//client.print("Hello server! Are you sleeping? ");
client.print(distance); // sends the message to the server
client.println("\r");
String answer = client.readStringUntil('\r'); // receives the answer from the sever
Serial.println("from server: " + answer);
client.flush();
digitalWrite(ledPin, HIGH);
delay(1000); // client will trigger the communication after two seconds
}----------------END client code---------------------
So, I am trying to send data through the Internet(and specifically using Thingspeak) after 10min. It appears so far I was not able to avoid the assigned IP.
The above code works excellent when I do communicate through the predefined routers IP.
Any suggestions/recommendations or solutions are much appreciated.
Thank you in advance!