Example sketches for the new Arduino IDE for ESP8266

Moderator: igrr

User avatar
By zammykoo
#56130 This worked great. Thanks!

oeyhaga wrote:Seem I'v solved the problem. As easy as adding a longer delay after GET command.
Changed delay(10); to delay(500)

Finaly I can continue with the project after many hours scratching my head :P

Code: Select all#include <ESP8266WiFi.h>
 
const char* ssid     = "NAME";
const char* password = "PASSWORD";
 
const char* host = "api.thingspeak.com";
 
int value = 1;
 
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);
 
  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());
 
}//end setup
 
void loop() {
  delay(5000);
   
  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;
  }
 
  // We now create a URI for the request
  String url = "/apps/thinghttp/send_request?api_key=PAW43HX9QN9SVOW1";
  Serial.print("Requesting URL: ");
  Serial.println(host + url);
  Serial.println(String("TRY: ") + value + ".");
 
  // This will send the request to the server
 Client.print(String("GET ") + url + "&headers=false" + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n");
  delay(500);
 
  // Read all the lines of the reply from server and print them to Serial
  while(Client.available()){
  String line = Client.readStringUntil('\r');
  Serial.println(line);
 }
  Serial.println("");
  Serial.println(String("Try nr. ") + value + " is finished.");
  Serial.println("Waiting for next try...");
  Serial.println("");
  value = value + 1;
  delay(20000);
}
User avatar
By shawnleo
#56217 Hi,

I used your code and it works great. Thanks for sharing! However, before using your code, I was trying to use the following http request:

https://thingspeak.com/channels/channel/fields/1/last

This needs HTTPS connection but it skipped the ThingHTTP app step.

I tried many many times but did not get it working. Sorry I am just a noob and could you please let me know how to do it?

Thanks a lot! :D
Last edited by shawnleo on Wed Oct 12, 2016 10:16 am, edited 1 time in total.