Lose MQTT Connection after 15-30 minutes - wifi

I am currently having a problem with my ESP8266 establishing a stable connection to my mosquitto MQTT broker.
I have moved house and am therefore using a different network.
In the previous network, my ESP ran stably and I had no problems at all. The MQTTserver run on a Raspberry PI 4.
In the new network, as already mentioned, it breaks off every 15-30 minutes (no fixed length of time).
The code is of course adapted to the new network and the IP of the broker.
The code I use is probably the generally known code for connecting to the MQTT server.
I only added some I/O Ports.
Since the code worked without problems in the old network, I first thought that the WLAN connection was failing.
So in the loop I inserted the reestablishment with the WLAN.
But this is running stably:
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Connecting to Vodafone-947E
.....
WiFi connected
IP address:
192.168.0.52
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
Attempting MQTT connection...failed, rc=-2 try again in 5 seconds
..............
Can it be that my new router simply interrupts the connection after some time? Otherwise, I don't understand what the problem could be.
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#include <Wire.h>
//----------------------------------------------------------------------------------------------------------------------
#define wifi_ssid "xxxxxxx"
#define wifi_password "xxxxxxxxx"
#define mqtt_server "192.168.0.xxx"
//----------------------------------------------------------------------------------------------------------------------
WiFiClient espClient;
PubSubClient client(espClient);
bool status;
const int OutputPin2 = 12;
const int ResetPin = 4;
int ResetCounter =0;
//----------------------------------------------------------------------------------------------------------------------
void setup() {
Serial.begin(9600);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
pinMode(OutputPin2, OUTPUT);
pinMode(ResetPin, OUTPUT);
digitalWrite(OutputPin2, HIGH);
digitalWrite(ResetPin, HIGH);
}
//----------------------------------------------------------------------------------------------------------------------
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
WiFi.begin(wifi_ssid, wifi_password);
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId= "ESP8266-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
delay(100);
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
// ... and resubscribe
client.subscribe("esp3/LED");
} else {
ResetCounter++;
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
Serial.println(ResetCounter);
if (ResetCounter >=5)
{
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
//digitalWrite(ResetPin, LOW);
ResetCounter =0;
}
// Wait 5 seconds before retrying
delay(5000);
}
}
}
//----------------------------------------------------------------------------------------------------------------------
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
//----------------------------------------------------------------------------------------------------------------------
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(wifi_ssid);
WiFi.begin(wifi_ssid, wifi_password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
//----------------------------------------------------------------------------------------------------------------------
void callback(char* topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
client.subscribe ("esp3/LED");
if (strcmp(topic,"esp3/LED")) {
Serial.print("Changing output to ");
if(messageTemp == "on LED"){
Serial.println("on LED");
digitalWrite(OutputPin2, LOW); //Invertiertes Signal
delay(200);
}
else if(messageTemp == "off LED"){
Serial.println("off LED");
digitalWrite(OutputPin2, HIGH);
delay(200);
}
}
}
```

Fortunately, I was able to solve the problem. The reason was that the WLAN signal on the ESP8266 was simply too low. As a result, the ESP was not able to stay on the network permanently. I actually ruled this out because the router is only one room away. The problem is that many routers in the surrounding flats also transmit on the same frequency band and thus influence the signal. I bought a WLAN amplifier (of course this does not improve the situation with the signal overlapping). Now I have no more problems with disconnects from the ESP and this has been the case for 2 days.

Related

ESP8266 unable to read data from Google Firebase

I'm trying to make an IOT app. My android side is working good so far, however my ESP8266 is unable to read data from Firebase Database. There is no error while connecting to the internet using onboard ESP8266 though. Can anyone point to a possible error? Thank You
#include <ESP8266WiFi.h>
#include <FirebaseArduino.h>
#define WIFI_SSID "WiFi"
#define WIFI_PASSWORD "xxxxxxxx"
#define FIREBASE_HOST "https://iot.firebaseio.com"
#define FIREBASE_AUTH "xxxxxxxx"
int LED1 = D5;
void setup()
{
Serial.begin(115200);
pinMode(LED1, OUTPUT);
Serial.println('\n');
wifiConnect();
Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);
delay(10);
}
void loop()
{
if(WiFi.status() != WL_CONNECTED)
{
wifiConnect();
}
delay(10);
Serial.print("Data from firebase:" + Firebase.getString("LED1") + "\n");
digitalWrite(LED1, Firebase.getString("LED1").toInt());
//digitalWrite(LED1, HIGH);
delay(10);
}
void wifiConnect()
{
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); // Connect to the network
Serial.print("Connecting to ");
Serial.print(WIFI_SSID); Serial.println(" ...");
int teller = 0;
while (WiFi.status() != WL_CONNECTED)
{ // Wait for the Wi-Fi to connect
delay(1000);
Serial.print(++teller); Serial.print(' ');
}
Serial.println('\n');
Serial.println("Connection established!");
Serial.print("IP address:\t");
Serial.println(WiFi.localIP()); // Send the IP address of the ESP8266 to the computer
}
For anyone encountering this issue. I got it working by removing the forward slash '/' at the end and the https:// from the start of Host Address.
At the line:
#define FIREBASE_HOST "https://iot.firebaseio.com"
you should fix to:
#define FIREBASE_HOST "iot.firebaseio.com"

TX/RX Communication with NodeMCU

I am trying to set up a transmitter-receiver-style relationship between two NodeMCUs (ESP-12e) using WiFi. I have one NodeMCU (the "transmitter") configured as a server and the other ("receiver") as a client. The transmitter reads in an analog value and sends it to the receiver, which outputs it in digital format via 8 LEDs. The transmitter says that the network is active, but the client cannot connect, and when I look in my laptop WiFi manager the network isn't there either. Can someone tell me what I'm doing wrong? Is it something in my code or is it how the board is configured?
Transmitter/Server Code:
#include <ESP8266WiFi.h>
#define PORT 8555
#define LED0 13
#define LED1 14
#define ADC 0
const char* NETWORK = "NodeMCU";
const char* PASSKEY = "asdf1234";
WiFiServer host(PORT);
WiFiClient node;
IPAddress IP(192,168,4,1);
IPAddress gateway(192,168,4,1);
IPAddress subnet(255,255,255,0);
void setup() {
pinMode(LED0, OUTPUT);
pinMode(LED1, OUTPUT);
pinMode(ADC, INPUT);
digitalWrite(LED0, LOW);
digitalWrite(LED1, LOW);
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAP(NETWORK, PASSKEY);
WiFi.softAPConfig(IP, gateway, subnet);
delay(100);
WiFi.begin(NETWORK, PASSKEY);
delay(100);
host.begin();
delay(100);
digitalWrite(LED1, HIGH);
}
void loop() {
if (node.connected()) digitalWrite(LED0, HIGH);
else digitalWrite(LED0, LOW);
host.write(analogRead(ADC) / 4);
}
Receiver/Client Code:
#include <ESP8266WiFi.h>
#define PORT 8555
#define DAC0 16
#define DAC1 5
#define DAC2 4
#define DAC3 2
#define DAC4 14
#define DAC5 12
#define DAC6 13
#define DAC7 15
const char* NETWORK = "NodeMCU";
const char* PASSKEY = "asdf1234";
IPAddress hostIP(192,168,4,1);
WiFiClient node;
void setup() {
pinMode(DAC0, OUTPUT);
pinMode(DAC1, OUTPUT);
pinMode(DAC2, OUTPUT);
pinMode(DAC3, OUTPUT);
pinMode(DAC4, OUTPUT);
pinMode(DAC5, OUTPUT);
pinMode(DAC6, OUTPUT);
pinMode(DAC7, OUTPUT);
digitalWrite(DAC7, HIGH);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
WiFi.begin(NETWORK, PASSKEY);
while (WiFi.status() != WL_CONNECTED) { // <-- gets stuck here
digitalWrite(DAC7, LOW);
delay(200);
digitalWrite(DAC7, HIGH);
delay(200);
}
if(node.connect(hostIP, PORT)) digitalWrite(DAC7, LOW);
}
void loop() {
if (node.available()) adcWrite(node.read());
}
void adcWrite(byte level) {
bool vals[8];
for (int i = 0; i < 8; i++) {
vals[i] = level % 2;
level /= 2;
}
digitalWrite(DAC0, vals[0]);
digitalWrite(DAC1, vals[1]);
digitalWrite(DAC2, vals[2]);
digitalWrite(DAC3, vals[3]);
digitalWrite(DAC4, vals[4]);
digitalWrite(DAC5, vals[5]);
digitalWrite(DAC6, vals[6]);
digitalWrite(DAC7, vals[7]);
}
Ports LED0 and LED1 on the TX and DAC0-7 on the RX are connected to LEDs, the ADC on the TX is connected to a 10k potentiometer. When I run the code, LED0 is off, LED1 is on, and DAC7 blinks indefinitely.

NodeMCU esp8266 using PubSubClient.h callback function gets ignored

EDIT: The topics to publish and subscribe are correct even if they look different. with my publish, I writing on the field 4. Moreover I run also MQTT.fx in sub/pub mode with the same topics and it works perfectly.
I want to use mqtt with my NodeMCU with esp8266. I adapted simple MQTT example to my minimal project. In my code (attached below) I'm trying to publish and subscribe to a ThingSpeak channel. The publish function does not have any problem, it publishes every time it is called. But the callback function is simply ignored, my code never enters in it.
To give more infor here you have My settings:
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "*****";
const char* password = "*****";
const char* mqtt_server = "mqtt.thingspeak.com";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Ciao sono la callback1\n");
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
Serial.print("Ciao sono la callback2\n");
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
Serial.print("Ciao sono la callback3\n");
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("channels/517055/publish/V4ZW4Y01ZLFD5XYR", "field5=7&status=MQTTPUBLISH");
// ... and resubscribe
client.subscribe("channels/517055/subscribe/fields/field4");
Serial.print("Ciao sono subscribe 01\n");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.print("Ciao sono la callback 10\n");
client.subscribe("channels/517055/subscribe/fields/field4");
Serial.print("Ciao subscribe 10\n");
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "field4= %ld&status=MQTTPUBLISH", value);
Serial.print("Publish message: ");
Serial.println(msg);
client.publish("channels/517055/publish/V4ZW4Y01ZLFD5XYR", msg);
}
}
Moreover here you have my serial monitor output:
Connecting to ZICHICHI
scandone
scandone
state: 0 -> 2 (b0)
state: 2 -> 3 (0)
state: 3 -> 5 (10)
add 0
aid 1
cnt
connected with ZICHICHI, channel 1
dhcp client start...
............ip:192.168.1.68,mask:255.255.255.0,gw:192.168.1.254
.
WiFi connected
IP address:
192.168.1.68
Ciao sono la callback 10
Ciao subscribe 10
Attempting MQTT connection...connected
Ciao sono subscribe 01
Publish message: field4= 1&status=MQTTPUBLISH
Publish message: field4= 2&status=MQTTPUBLISH
pm open,type:2 0
Publish message: field4= 3&status=MQTTPUBLISH
Publish message: field4= 4&status=MQTTPUBLISH
Publish message: field4= 5&status=MQTTPUBLISH
Publish message: field4= 6&status=MQTTPUBLISH
Publish message: field4= 7&status=MQTTPUBLISH
Publish message: field4= 8&status=MQTTPUBLISH
Publish message: field4= 9&status=MQTTPUBLISH
Publish message: field4= 10&status=MQTTPUBLISH
Publish message: field4= 11&status=MQTTPUBLISH
Publish message: field4= 12&status=MQTTPUBLISH
Publish message: field4= 13&status=MQTTPUBLISH
Publish message: field4= 14&status=MQTTPUBLISH
Publish message: field4= 15&status=MQTTPUBLISH
Publish message: field4= 16&status=MQTTPUBLISH
Publish message: field4= 17&status=MQTTPUBLISH
I hope someone know what I made wrong.
Actually, your Code is Almost correct but the loop() function is executing to Infinity.
basically, the code got stuck in the loop function, so that's why its not able to subscribe. Even if it is subscribed also, loop function will dominate on callback function and callback function will not be called, it will act as a NULL
Here is the Code, I have made some changes. Just try out
**This code will not keep on Publish the payload every time, If you want you can add the client.plublish in the loop() before client.loop()
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Update these with values suitable for your network.
const char* ssid = "*****";
const char* password = "*****";
const char* mqtt_server = "mqtt.thingspeak.com";
WiFiClient espClient;
PubSubClient client(espClient);
long lastMsg = 0;
char msg[50];
int value = 0;
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Ciao sono la callback1\n");
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
for (int i = 0; i < length; i++) {
Serial.print((char)payload[i]);
}
Serial.println();
// Switch on the LED if an 1 was received as first character
if ((char)payload[0] == '1') {
Serial.print("Ciao sono la callback2\n");
digitalWrite(BUILTIN_LED, LOW); // Turn the LED on (Note that LOW is the voltage level
// but actually the LED is on; this is because
// it is acive low on the ESP-01)
} else {
Serial.print("Ciao sono la callback3\n");
digitalWrite(BUILTIN_LED, HIGH); // Turn the LED off by making the voltage HIGH
}
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP8266Client-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
// Once connected, publish an announcement...
client.publish("channels/517055/publish/V4ZW4Y01ZLFD5XYR", "field5=7&status=MQTTPUBLISH");
// ... and resubscribe
client.subscribe("channels/517055/subscribe/fields/field4");
Serial.print("Ciao sono subscribe 01\n");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
pinMode(BUILTIN_LED, OUTPUT); // Initialize the BUILTIN_LED pin as an output
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
Serial.print("Ciao sono la callback 10\n");
connectmqtt();
}
void loop() {
if (!client.connected()) {
reconnect();
}
long now = millis();
if (now - lastMsg > 2000) {
lastMsg = now;
++value;
snprintf (msg, 75, "field4= %ld&status=MQTTPUBLISH", value);
Serial.print("Publish message: ");
Serial.println(msg);
}
client.loop();
}
void connectmqtt()
{
client.connect("ESP8266Client-");
{
Serial.println("connected");
// Once connected, publish an announcement...
// ... and resubscribe
client.subscribe("channels/517055/subscribe/fields/field4");
client.publish("channels/517055/publish/V4ZW4Y01ZLFD5XYR", msg);
if (!client.connected())
{
reconnect();
}
}
}
You are subscribing to a different topic than a topic you are publishing to. Your publishing topic is "channels/517055/publish/V4ZW4Y01ZLFD5XYR" while you are subscribing to "channels/517055/subscribe/fields/field4".
SOLVED
The code posted by me and the code posted by #karan was correct. The problem I had was realeted with thingspeak Broker connection and the Thingspeak documentation, that wasn't updated at the time. In particular is necessary to pass MQTT_APIKEY and USERNAME to establish connection with mqtt broker, even with PUBLIC channel.
The problem is solved passing these 2 parameters in connection step, like this:
client.connect("ESP8266Client-", "Your_username", "MQTT_APIKEY");
Many thanks to #karan that help me, and improved my code.

Arduino ESP8266 01 MQTT Raspberry pi

I want to use ESP 8266 01 and arduino uno as client in MQTT. The code For ESP8266 01 is successfully compiled and I'M getting errors in Arduino uno
The error is 'messageTemp' was not declared in this scope
Amd also I have a thought of using stm32xx in client.
If I compile for that
exit status 1
ISO C++ forbids comparison between pointer and integer [-fpermissive]
above is the error I'm getting
1)ESP code
// Loading the ESP8266WiFi library and the PubSubClient library
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// Change the credentials below, so your ESP8266 connects to your router
const char* ssid = "YOUR_SSID";
const char* password = "YOUR_PASSWORD";
const char* mqtt_server = "YOUR_RPi_IP_Address";
// Initializes the espClient
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("WiFi connected - ESP IP address: ");
Serial.println(WiFi.localIP());
}
if(topic=="home/office/esp1/gpio2"){
Serial.print("Changing GPIO 2 to ");
if(messageTemp == "1"){
// digitalWrite(ledGPIO2, HIGH);
Serial.print("On");
}
else if(messageTemp == "0"){
// digitalWrite(ledGPIO2, LOW);
Serial.print("Off");
}
}
Serial.println();
}
void reconnect() {
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect("ESP8266Client")) {
Serial.println("connected");
client.subscribe("home/office/esp1/gpio2");
}
else
{
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
void setup() {
Serial.begin(115200);
setup_wifi();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
}
Code for Arduino UNO
void setup(){
Serial.begin(9600);
pinMode(12, OUTPUT);
digitalWrite(12, HIGH);
String messageTemp;
}
void callback(String topic, byte* message, unsigned int length) {
Serial.print("Message arrived on topic: ");
Serial.print(topic);
Serial.print(". Message: ");
String messageTemp;
for (int i = 0; i < length; i++) {
Serial.print((char)message[i]);
messageTemp += (char)message[i];
}
Serial.println();
}
void loop ()
{
if (Serial.available()){
char topic = Serial.read();
{
if (topic=="home/office/esp1/gpio2")
{
Serial.print("Changing GPIO 2 to ");
if(messageTemp == "1"){
digitalWrite(12, HIGH);
Serial.print("On");
}
else if(messageTemp == "0")
{
digitalWrite(12, LOW);
Serial.print("Off");
}
}}}}
ESP code doesn't look the whole source code you provided. In case of Arduino UNO, if you're intending to use messageTemp as a global variable, then you need to declare it only one time outside of functions. In the above code, you declared it twice in setup() and callback(), then those are considered as local variables. Even in loop(), there's no declaration, this should create the compile error.

GET request failed in Arduino-Uno

I am trying to access a simple web page running in my Rpi-server using ESP8266 and Arduino.
I have refereed this similar SO question , but it's not the solution for my problem.
Here is my current Arduino Code :
#include "WiFiEsp.h"
// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(2,3); // RX, TX
#endif
char ssid[] = "RPi"; // your network SSID (name)
char pass[] = "raspberry"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "192.168.50.1";
// Initialize the Ethernet client object
WiFiEspClient client;
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
printWifiStatus();
Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (client.connect(server, 80)) {
Serial.println("Connected to server");
// Make a HTTP request
client.println("GET /simple.html HTTP/1.1");
client.println("Host: 192.168.50.1");
client.println("Connection: close");
client.println();
}
}
void loop()
{
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client
if (!client.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
client.stop();
// do nothing forevermore
while (true);
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Output:
Starting connection to server...
[WiFiEsp] Connecting to 192.168.50.1
Connected to server
[WiFiEsp] Data packet send error (2)
[WiFiEsp] Failed to write to socket 3
[WiFiEsp] Disconnecting 3
My simple.html looks like this.
<html>
<body>
<p>1</p>
</body>
</html>
}
I accessed to this page from web browser and it shows the content properly.
What is missing here?
Thanks in advance.
Try this line in your code
client.print("GET /simple.html HTTP/1.0\r\n\r\n");

Resources