Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Nic Roche
#60937 If I do the GET to thingspeak in setup (were the GET to timeapi is made) it is successful. Just commenting out httpServer.handleClient() in loop does not help "the GET to thingspeak" (failing still) which is called from loop with a minute interval..

EDIT:

I just ran the code at https://github.com/squix78/esp8266-proj ... logger.ino

Putting in my key and hard-coding some values, GETing every 60 seconds, and I can only connect once successfully.

The modified code (- keys):

Code: Select all/*
*  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 <ESP8266WiFi.h>
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\r\n" +
      "Host: " + host + "\r\n" +
      "Connection: close\r\n\r\n");
   delay(10);
   // 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. ");
}



Serial output:

Code: Select allOpening 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


The same result if I GET every 2 minutes.
User avatar
By Nic Roche
#60981 OK so this is not thingspeak specific: (once a minute)

Code: Select all

#include <ESP8266WiFi.h>
extern "C" {
#include "user_interface.h"
}

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


const char* host = "www.google.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 = "/search?q=arduino";
   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");
   delay(10);
   // 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);
   }
   client.stop();
   Serial.println();
   Serial.println("closing connection. ");
}



Outputs:

Code: Select allOpening port
Port open


Connecting to Virgin Mobile 4G_38AB4B
.
WiFi connected
IP address:
192.168.0.134
connecting to www.google.com
Requesting URL: /search?q=arduino

closing connection.
connecting to www.google.com
connection failed
connecting to www.google.com
connection failed
connecting to www.google.com
connection failed


Any ideas?
User avatar
By Nic Roche
#60983 I _seem_ to have resolved it.
I have modified the order before for
Code: Select allWiFi.mode(WIFI_STA);

Now it sits just before
Code: Select allWiFi.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...