0 Members and 1 Guest are viewing this topic.
#include <ESP8266WiFi.h>const char* ssid = "xxxx";const char* password = "yyyy";const char* host = "api.thingspeak.com";// Use WiFiClient class to create TCP connectionsWiFiClient client;void setup() { Serial.begin(115200); delay(100); // 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());}void loop() { Serial.print("connecting to "); Serial.println(host);if (client.connect(host,80)) { // "184.106.153.149" or api.thingspeak.comString postStr = "GET /talkbacks/7441/commands/1145081?api_key=WSMCMVFUJBI6Z115 HTTP/1.1\n";client.print(postStr);client.print("Host: api.thingspeak.com\n");client.print("Connection: close\n");} // Read all the lines of the reply from server and print them to Serial while(client.available()){ char c = client.read(); Serial.print(c); } client.stop(); Serial.println(); Serial.println("closing connection"); delay(15000);}
client.print(postStr);client.print("Host: api.thingspeak.com\n");client.print("Connection: close\n");
#include <ESP8266WiFi.h>const char* ssid = "xxxxx";const char* password = "yyyyy";const char* host = "api.thingspeak.com";String TalkBackID ="7441";String TalkBackAPIKey ="WSMCMVFUJBI6Z115";String Command_ID ="1145081";String url = "/talkbacks/"+TalkBackID+"/commands/"+Command_ID+"?api_key="+TalkBackAPIKey;// Use WiFiClient class to create TCP connectionsWiFiClient client;void setup() { Serial.begin(115200); delay(100); // 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.print("IP address: "); Serial.println(WiFi.localIP());}void loop() { Serial.print("connecting to "); Serial.println(host); if (client.connect(host, 80)) { Serial.println("Tcp Connection eastablished with thingSpeak API"); }else { Serial.println("connection failed"); return; } client.print(String("GET ") + url + "&headers=false" + " HTTP/1.1\r\n" + "Host: " + host + "\r\n" + "Connection: close\r\n\r\n"); delay(500); // apparently some timeout is needed here String messageBody = ""; while (client.available()) { String line = client.readStringUntil('\n'); if (line.length() == 1) { //actual content starts after empty line (that has length 1) messageBody = client.readStringUntil('\n'); break; } } Serial.print("Found a message, the message is: "); Serial.print(messageBody); client.stop(); Serial.println(); Serial.println("closing connection"); Serial.println(); delay(15000);}