Connection only works once | ESP8266 Wi-Fi Module | Forum

Avatar

Please consider registering
Guest

Search

— Forum Scope —






— Match —





— Forum Options —





Minimum search word length is 3 characters - maximum search word length is 84 characters

Register Lost password?
sp_Feed sp_TopicIcon
Connection only works once
Avatar
Nic Roche
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
1
January 13, 2017 - 8:29 am
sp_Permalink sp_Print sp_EditHistory

Hi,

 

I'm doing an update using the GET verb in request.

This mechanism is important.

It works the first time and then all subsequent connections fail.

 

I have used the domain url and IP address with the same results.

I have used a 1minute and 2minute loop to no avail.

Code (-keys):

DERIVED FROM: https://github.com/squix78/esp8266-projects/blob/master/arduino-ide/thingspeak-data-logger/thingspeak-data-logger.ino

/*
*  This sketch sends data via HTTP GET requests to thingspeak service every 10 minutes
*  You have to set your wifi credentials and your thingspeak key.
*/

#include
extern "C" {
#include "user_interface.h"
}

const char* ssid = "Virgin Mobile 4G_38AB4B";
const char* password = "XXX";

const char* host = "api.thingspeak.com";

void setup() {
   Serial.begin(115200);
   Serial.println();
   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.println("WiFi connected");
   Serial.println("IP address: ");
   Serial.println(WiFi.localIP());
   delay(5000);
}

int value = 0;

void loop() {
   ++value;
   logThingSpeak(value);
   delay(60000);
}

void logThingSpeak(int value) {
   Serial.print("connecting to ");
   Serial.println(host);
   // Use WiFiClient class to create TCP connections
   WiFiClient client;
   const int httpPort = 80;
   if (!client.connect(host, httpPort)) {
      Serial.println("connection failed");
      return;
   }
   String url = "/update?api_key=XXX&field1=" + String(value);
   Serial.print("Requesting URL: ");
   Serial.println(url);
   // This will send the request to the server
   client.print(String("GET ") + url + " HTTP/1.1
" +
      "Host: " + host + "
" +
      "Connection: close

");
   delay(10);
   // Read all the lines of the reply from server and print them to Serial
   while (client.available()) {
      String line = client.readStringUntil('
');
      Serial.print(line);
   }
   Serial.println();
   Serial.println("closing connection. ");
}

The serial output:

Opening port
Port open

Connecting to Virgin Mobile 4G_38AB4B
.
WiFi connected
IP address:
192.168.0.134
connecting to api.thingspeak.com
Requesting URL: /update?api_key=XXX&field1=1

closing connection.
connecting to api.thingspeak.com
connection failed
connecting to api.thingspeak.com
connection failed
connecting to api.thingspeak.com
connection failed
connecting to api.thingspeak.com
connection failed 

Is there something fundamental I am missing?

 

Thanks

Avatar
Hans
Natick, MA

Forum Posts: 390
sp_UserOfflineSmall Offline
2
January 13, 2017 - 9:35 am
sp_Permalink sp_Print

Hi. I would try commenting out the return line to see if the code continues. If that does not work, then I would try restarting the client. Let us know what you discover.

Avatar
Rob Purser
Natick, MA

Forum Posts: 46
sp_UserOfflineSmall Offline
3
January 13, 2017 - 9:47 am
sp_Permalink sp_Print

Are you actually closing the connection that you opened?  I don't see a call to stop()...

Consider using the ThingSpeak Communication Library, which supports more ESP-8266 devices.   The code is public, so you can use it as a reference.

https://github.com/mathworks/thingspeak-arduino

-Rob

Senior Development Manager for IoT and Hardware Interfacing for MATLAB at MathWorks.  Visit ThingSpeak.com to explore the IoT Analytic platform that speaks MATLAB made for engineers and scientists.  You can collect, analyze, and act in 5 minutes or less!

Avatar
Nic Roche
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
4
January 13, 2017 - 6:15 pm
sp_Permalink sp_Print

 > I don't see a call to stop()...

This is linked github code refactored.

In my many iterations I have tried stop(), global WiFiClient, local WiFiClient, timeouts etc.

> using the ThingSpeak Communication Library

It needs to be a plain get using a querystrings with no specific headers.

> so you can use it as a reference.

Will have a look.

 

Thanks Rob.

Avatar
Nic Roche
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
5
January 13, 2017 - 6:25 pm
sp_Permalink sp_Print sp_EditHistory

Hans said
Hi. I would try commenting out the return line to see if the code continues. If that does not work, then I would try restarting the client. Let us know what you discover.  

as there is no response from // Read all the lines of the reply I could not verify an actual success (serial side) doing this, but will try and check the channel feed.

 

EDIT: removed return and no requests received in my channel (other than the first).

EDIT2: tried http://www.google.com and had the same issue.

Avatar
Nic Roche
New Member
Forum Posts: 4
sp_UserOfflineSmall Offline
6
January 13, 2017 - 7:55 pm
sp_Permalink sp_Print

I _seem_ to have resolved it.

I have modified the order before for 

WiFi.mode(WIFI_STA);

Now it sits just before

WiFi.begin (ssid, pw);

Also I modified the number of connection my access point can handle.

It was repoerting 4  connections on a limit of 5; now the limit is 10 - mainly mentioned this for transparency as it does not explain the initial successful connection...

 

Thanks all

Forum Timezone: America/New_York

Most Users Ever Online: 114

Currently Online: cstapels
21 Guest(s)

Currently Browsing this Page:
1 Guest(s)

Top Posters:

rw950431: 252

Vinod: 164

cstapels: 96

piajola: 75

vespapierre: 63

Adarsh_Murthy: 59

Newest Members:

yeseniara11

RomanFug

JamesPes

RobertBak

ScottSmiva

Amourhotte

Forum Stats:

Groups: 4

Forums: 17

Topics: 1313

Posts: 4565

 

Member Stats:

Guest Posters: 1

Members: 5703

Moderators: 0

Admins: 2

Administrators: Hans, lee