Hello, I am going fo a very common project of weather station to learn few things, (especially ESP01 and solar panel usage...) but guess what, I have some trouble.
I just try things step by steps before integrating all, so currently I am just trying to send some data to a web server (intranet) from the ESP. At the moment I don't even try to send the data from the sensor, just a value, to check the HTTP GET and wifi part.
I have first tried to use the ESP as a webserver and it was working find, accessible by any browser on the local network.
Now, I want to work the other way, having the ESP contacting a local server.
I first have the WIFI management, (similar to the previous code), then the client part, fully different. The wifi connection is handled with static IP. It helps me for other things. There is no address conflict.
We I start the ESP, It connects to the Wifi, but fail to connect to the local server. When I try to connect to the server from a browser on the local network, it works fine.
Here is my code
/* This sketch sends data via HTTP GET requests to local server from the ESP */
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
const char* ssid = "SSID"; //replace with your own wifi ssid
const char* password = "PASSWORD"; //replace with your own wifi ssid password
const int host = (192,168,1,10); // could be a char with the url, or array with the ip address
const char* host_s = "192.168.1.10";
const int sensor_id=2; // sensor id in the database for records
// Static IP address configuration
IPAddress staticIP(192, 168, 1, 105); //ESP static IP address
IPAddress gateway(192, 168, 1, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
IPAddress dns(116,228,111,118); //DNS (could be 8,8,8,8 for google dns or any other DNS
const char* deviceName = "TEST_ESP";
void setup() {
Serial.begin(115200); delay(10); // We start by connecting to a WiFi network Serial.println();
Serial.println(); Serial.print("Connecting to ");
Serial.println(ssid);
/* Explicitly set the ESP8266 to be a WiFi-client, and with correct mask */
WiFi.begin(ssid, password);
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
WiFi.hostname(deviceName); // DHCP Hostname
WiFi.config(staticIP, subnet, gateway, dns);
WiFi.begin(ssid, password);
WiFi.mode(WIFI_STA); //WiFi mode station (connect to wifi router only) **** Is it here I am making a mistake? ****
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
int value = 0;
void loop() {
delay(5000); ++value; Serial.print("connecting to ");
Serial.println(host_s);
WiFiClient client;// Use WiFiClient class to create TCP connections
const int httpPort = 80;
if (!client.connect(host, httpPort)) { // **** This is where It fails ****
Serial.println("connection failed");
return;
}
// We now create a URI for the request
//this url contains the informtation we want to send to the server
//if esp8266 only requests the website, the url is empty
String url = "/meteo/create_line.php";
url += "?sensor="; // ?sensor = name of the GET variable
url += sensor_id; // here the value, in this case the
/* url += "?param2="; //other variable name (normally temperature, or pressure ...
url += param2; //other variable value (normally temperature value
*/
Serial.print("Requesting URL: ");
Serial.println(url); // This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
unsigned long timeout = millis();
while (client.available() == 0) {
if (millis() - timeout > 5000)
{
Serial.println(">>> Client Timeout !");
client.stop(); return;
}
} // Read all the lines of the reply from server and print them to Serial
while (client.available())
{ String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
Serial.println("closing connection"); }
I have added comments with **** which are specific to my problems, while other comments are more global to the code.
I don't see how to investigate the issue. I can't get access to logs, or things like that to know what is actually sent and received. It would be very nice if you could tell me or give me some clues on where to look.
Thanks