Hey everyone!
I have recently been working on a project where I receive live weather data using an API. I want to create a home weather 'clock'. Right now I am focusing on temperature data. I get the data from a website, using the esp8266. So far I have got the data, and I can 'write' it to the serial monitor. I want to be able to save the temperature data as a variable in my Arduino code, which is continuously updating as the esp8266 receives more data.
One problem I have is that the API separates the temperature data into a separate webpage, with just the temperature visible in the top left-hand corner. However, when I 'get' the data into the arduino, the result is a jumbled mess full of random sentences, then at the bottom of the package
is the data that I need.
Here is an example of the data package that gets printed to the serial monitor.
HTTP/1.1 200 OK
Date: Thu, 31 Mar 2022 22:49:13 GMT
Content-Type: text/plain; charset=utf-8
Transfer-Encoding: chunked
Connection: close
Status: 200 OK
X-Frame-Options: SAMEORIGIN
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: GET, POST, PUT, OPTIONS, DELETE, PATCH
Access-Control-Allow-Headers: origin, content-type, X-Requested-With
Access-Control-Max-Age: 1800
Cache-Control: max-age=0, private, must-revalidate
X-Runtime: 0.117807
X-Powered-By: Phusion Passenger
Server: nginx + Phusion Passenger
2
37
0
The '37' is the only data that I need. I have no idea what anything else means, or even where it came from, because the API only shows the 37.
So, please could someone help me assign just the temperature data to a variable that can be used in my code? Thank you so much in advance. My code is below.
#include <ESP8266WiFi.h>
#include <Wire.h> // This library is already built in to the Arduino IDE
const char* ssid = "***********"; // put router name
const char* password = "*************";// put password
const char* host = "api.thingspeak.com";
void setup() {
Serial.begin(115200);
// 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());
}
int value = 0;
void loop() {
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");
}
//now create a URL for the request
String url = "/apps/thinghttp/send_request?api_key=************";
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(500);
// Read all the lines of the reply from server and print them to Serial
while(client.available()){
char c = client.read();
Serial.write(c);
}
Serial.println();
Serial.println("closing connection");
delay(2000);
}