This is the code to get weather information from openweathermap
#include "WiFiEsp.h"
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
//#include <ArduinoJson.h>
//#include <Arduino_JSON.h>
SoftwareSerial ETH_ESP_01(6, 7); // RX, TX
#endif
const char ssid[] = "SK_WiFiGIGA6E56"; // your network SSID (name)
const char pass[] = "********"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status
char server[] = "api.openweathermap.org";
String result = "";
// Initialize the Ethernet client object
WiFiEspClient Ethclient_ESP_01;
void setup()
{
// initialize serial for debugging
Serial.begin(9600);
// initialize serial for ESP module
ETH_ESP_01.begin(9600);
// pinMode(10, OUTPUT);
// digitalWrite(10, LOW);
// delay(100);
// digitalWrite(10, HIGH);
// delay(500);
WiFi.init(Ð_ESP_01);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// you're connected now, so print out the data
Serial.println("You're connected to the network");
printWifiStatus();
Serial.println();
Serial.println("Starting connection to server...");
// if you get a connection, report back via serial
if (Ethclient_ESP_01.connect(server, 80)) {
Serial.println("Connected to server");
// Make a HTTP request
Ethclient_ESP_01.println("GET /data/2.5/weather?id=1838519&appid=token HTTP/1.1");
Ethclient_ESP_01.println("Host: api.openweathermap.org");
Ethclient_ESP_01.println("Connection: close");
Ethclient_ESP_01.println();
}
}
void loop() {
result.reserve(1024);
bool i = false;
while (Ethclient_ESP_01.available()) {
char c = Ethclient_ESP_01.read();
result += c;
}
Serial.println(result);
int first = result.indexOf("{");
// result = result.substring(first+1);
result.remove(0, 318);
ETH_ESP_01.println(result);
ETH_ESP_01.println(first);
JSONVar myArray = JSON.parse(result);
if (JSON.typeof(myArray) == "undefined") {
Serial.println("Parsing input failed!");
}
Serial.println(myArray["name"]);
// StaticJsonDocument<512> doc;
// DeserializationError error = deserializeJson(doc, result);
//
// if (error) {
// Serial.print(F("deserializeJson() failed: "));
// Serial.println(error.f_str());
// }
// if the server's disconnected, stop the client
if (!Ethclient_ESP_01.connected()) {
Serial.println();
Serial.println("Disconnecting from server...");
Ethclient_ESP_01.stop();
// do nothing forevermore
while (true);
}
}
void printWifiStatus()
{
// print the SSID of the network you're attached to
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength
long rssi = WiFi.RSSI();
Serial.print("Signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Works well in this state, normal result:
{"coord":{"lon":129.05,"lat":35.1333},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":285.15,"feels_like":283.71,"temp_min":285.15,"temp_max":285.15,"pressure":1018,"humidity":50},"visibility":10000,"wind":{"speed":2.06,"deg":200},"clouds":{"all":0},"dt":1618486591,"sys":{"type":1,"id":8086,"country":"KR","sunrise":1618433511,"sunset":1618480530},"timezone":32400,"id":1838519,"name":"Busan","cod":200}
However, if you run Arduino Json.h or Arduino_JSON.h on this code, the result will be strange.
Abnormal result value :
{"coord":{"lon":129.05,"lat":35.1333},"weather":[{"id":800,"main":"Clear","description":"clear sky","icon":"01n"}],"base":"stations","main":{"temp":285.15,"feels_like":283.71,"temp_min":285.15,"temp_max":285.15,"pressure":1018,"humidity":50},"visibility":10000,"wind":{"speed":2.06,"deg":200},"clouds":{"all":0},"dt":1618486591,"sys":{"type":1,"id":8086,"country":"KR","sunrise":1618433511,"sunset":161848053
I use Arduino Uno and ESP-01 module
How can I solve this problem?