NodeMCU + I2C LCD1602 Погодная станция
- Войдите на сайт для отправки комментариев
Чт, 18/04/2019 - 11:49
Всем привет. Сделал погодную станцию, самую простейшую (парсер json с выводом на экран).
Суть проблемы.
При обновлении данных спустя некоторое время на экран выводятся старые значения температуры и влажности хотя в мониторе порта видно что приходят новые с другими значениями. + спустя 8 циклов обновления данных модуль уходит в ребут. Как это можно решить?
Скетч:
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#include <Wire.h>
const char* ssid = "";
const char* password = "";
String APIKEY = "";
String CityID = ""; //Your City ID
WiFiClient client;
const char *weatherHost = "api.openweathermap.org";
int counter = 15;
String weatherDescription ="";
String weatherLocation = "";
String Country;
float Temperature;
float Humidity;
String result;
LiquidCrystal_I2C lcd(0x27, 16, 2); // Address of your i2c LCD back pack should be updated.
void setup() {
Wire.begin(2,0);
Serial.begin(115200);
int cursorPosition=0;
lcd.begin(16, 2);
lcd.init();
lcd.backlight();
lcd.print(" Connecting");
Serial.println("Connecting to " + String (ssid));
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
lcd.setCursor(cursorPosition,2);
lcd.print(".");
cursorPosition++;
}
lcd.clear();
lcd.print(" Connected!");
Serial.println("Connected to " + String (ssid));
Serial.println(WiFi.localIP());
delay(1000);
}
void loop() {
if(counter == 15) //Get new data every 10 minutes
{
counter = 0;
displayGettingData();
delay(1000);
getWeatherData();
}else
{
counter++;
displayWeather(weatherLocation,weatherDescription);
delay(5000);
displayConditions(Temperature,Humidity);
delay(5000);
}
}
void getWeatherData() //client function to send/receive GET request data.
{
if (client.connect(weatherHost, 80))
{ //starts client connection, checks for connection
client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY);
client.println("Host: api.openweathermap.org");
client.println("User-Agent: ArduinoWiFi/1.1");
client.println("Connection: close");
client.println();
}
else {
Serial.println("connection failed"); //error message if no client connect
Serial.println();
}
while(client.connected() && !client.available())
delay(1); //waits for data
while (client.connected() || client.available())
{ //connected or data available
char c = client.read(); //gets byte from ethernet buffer
result = result+c;
}
client.stop(); //stop client
result.replace('[', ' ');
result.replace(']', ' ');
Serial.println(result);
char jsonArray [result.length()+1];
result.toCharArray(jsonArray,sizeof(jsonArray));
jsonArray[result.length() + 1] = '\0';
DynamicJsonBuffer json_buf;
JsonObject &root = json_buf.parseObject(jsonArray);
if (!root.success())
{
Serial.println("parseObject() failed");
}
String location = root["name"];
String country = root["sys"]["country"];
float temperature = root["main"]["temp"];
float humidity = root["main"]["humidity"];
String weather = root["weather"]["main"];
String description = root["weather"]["description"];
weatherDescription = description;
weatherLocation = location;
Country = country;
Temperature = temperature;
Humidity = humidity;
}
void displayWeather(String location,String description)
{
lcd.clear();
lcd.setCursor(0,0);
lcd.print(location);
lcd.print(", ");
lcd.print(Country);
lcd.setCursor(0,1);
lcd.print(description);
}
void displayConditions(float Temperature,float Humidity)
{
lcd.clear(); //Printing Temperature
lcd.print("T:");
lcd.print(Temperature,1);
lcd.print((char)223);
lcd.print("C ");
lcd.print(" H:"); //Printing Humidity
lcd.print(Humidity,0);
lcd.print(" %");
}
void displayGettingData()
{
lcd.clear();
lcd.print("Getting data");
}
Вывод в мониторе порта:
09:43:33.441 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮
09:46:05.622 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
09:48:37.887 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
09:51:10.087 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
09:53:42.416 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
09:56:14.739 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
09:58:47.179 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
10:01:19.570 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
10:03:52.073 -> {"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":9.62,"pressure":1029,"humidity":61,"temp_min":8,"temp_max":10.56},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569110,"sys":{"type":1,"id":8903,"message":0.0047,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0052,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.006,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮{"coord":{"lon":30.52,"lat":50.43},"weather": {"id":800,"main":"Clear","description":"clear sky","icon":"01d"} ,"base":"stations","main":{"temp":10,"pressure":1029,"humidity":61,"temp_min":10,"temp_max":10},"visibility":10000,"wind":{"speed":1},"clouds":{"all":0},"dt":1555569000,"sys":{"type":1,"id":8903,"message":0.0054,"country":"UA","sunrise":1555556329,"sunset":1555606528},"id":703448,"name":"Kiev","cod":200}⸮
10:03:59.212 ->
10:03:59.212 -> ets Jan 8 2013,rst cause:4, boot mode:(1,6)
10:03:59.212 ->
10:03:59.212 -> wdt reset
10:10:41.988 -> sd
Вы никогда не чистите переменную result. Вы всё накапливаете и накапливаете данные в ней, а выбрасывать их дяд будет.
Вот и выходит, что сначала она у Вас покащывает первые разобранные данные, а потом (по мере того, как переменная result) перестаёт помещаться в память) сдыхает совсем.
Вы никогда не чистите переменную result. Вы всё накапливаете и накапливаете данные в ней, а выбрасывать их дяд будет.
Вот и выходит, что сначала она у Вас покащывает первые разобранные данные, а потом (по мере того, как переменная result) перестаёт помещаться в память) сдыхает совсем.
добавил обнуление строки result = ""; в loop, вроде бы сработало.. или есть правильный способ?
Ну, если работает, то и ладно.
А правильный, ну, как говорил известный сантехник, там систему менять надо.
Ну, для начала, само описание Вашего result можно перенести в loop (чего она делает в глобальном пространстве?). Тогда и обнулять не нужно. В строке 114, как минимум, надо добавить & после слова Sting (после обоих). Ну, и там много чего. Если "по уму", то там надо всё переписывать.
Почитайте мои этюды про работу с памятью. Они в разделе "программирование" в начале "прибиты".
этот писал какой-то индус, я просто немного под себя переделал..
что-то не могу этой строки найти у себя
этот писал какой-то индус
Это заметно.
что-то не могу этой строки найти у себя
Чего???? Откройте код в стартовом посте этого топика. Там слева стоят номера строк. Вы не можете найти строку №114? Ну, если не можете, то, боюсь, "медицина бессильна".
этот писал какой-то индус
Это заметно.
что-то не могу этой строки найти у себя
Чего???? Откройте код в стартовом посте этого топика. Там слева стоят номера строк. Вы не можете найти строку №114? Ну, если не можете, то, боюсь, "медицина бессильна".
я смотрел сначала в IDE, там 114 строка String location = root["name"];, а потом понял что что-то не то. Приношу извинения :)
Добавил ещё чтобы на oled экран выводилось время, но только показания секунд обновляются только через 15 сек.
Подскажите, пожалуйста, где я допустил ошибку
// Needed hardware: NodeMCU, LCD 1602 // Needed libs: ArduinoJson 5.x, LiquidCrystal_I2C, ESP8266WiFi, Wire // Weather Json-parser for Kyiv // // To order other city - you should: // Get your API from openweathermap // Get your city code // Change name and password of your Wi-Fi // const char* ssid = "your Wi-Fi"; // const char* password = "password from your Wi-Fi"; // String APIKEY = "your API from openweathermap"; #include <ESP8266WiFi.h> #include <LiquidCrystal_I2C.h> #include <ArduinoJson.h> #include <Wire.h> #include <SSD1306.h> #include <SSD1306Wire.h> #include <NTPClient.h> #include <Time.h> #include <TimeLib.h> #include <Timezone.h> #include <WifiUDP.h> #include <String.h> #define NTP_OFFSET 60 * 60 #define NTP_INTERVAL 24 * 60 * 60 * 1000 #define NTP_ADDRESS "ca.pool.ntp.org" const char* ssid = ""; const char* password = ""; String APIKEY = ""; String CityID = "";//Your City ID WiFiClient client; WiFiUDP ntpUDP; NTPClient timeClient(ntpUDP, NTP_ADDRESS, NTP_OFFSET, NTP_INTERVAL); String date; String t; const char * days[] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"} ; const char * months[] = {"Jan", "Feb", "Mar", "Apr", "May", "June", "July", "Aug", "Sep", "Oct", "Nov", "Dec"} ; const bool show24hr = true; const char *weatherHost = "api.openweathermap.org"; int counter = 15; String weatherDescription =""; String weatherLocation = ""; String Country; float Temperature; float Temperature_max; float Temperature_min; float Humidity; float Pressure; String result; LiquidCrystal_I2C lcd(0x27, 16, 2); // Address of your i2c LCD back pack should be updated. SSD1306 display(0x3c, 0, 2); //0x3d for the Adafruit 1.3" OLED, 0x3C being the usual address of the OLED void setup() { timeClient.begin(); // Start the NTP UDP client display.init(); display.flipScreenVertically(); Wire.begin(2,0); Serial.begin(115200); int cursorPosition=0; lcd.begin(16, 2); lcd.init(); lcd.backlight(); lcd.print(" Connecting"); Serial.println("Connecting to " + String (ssid)); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); lcd.setCursor(cursorPosition,2); lcd.print("."); cursorPosition++; } lcd.clear(); lcd.print(" Connected!"); Serial.println("Connected to " + String (ssid)); Serial.println(WiFi.localIP()); delay(1000); display.clear(); display.drawString(0, 10, "Connecting to Wifi..."); display.display(); display.drawString(0, 24, "Connected."); display.display(); } void loop() { if (WiFi.status() == WL_CONNECTED) //Check WiFi connection status { date = ""; // clear the variables t = ""; // update the NTP client and get the UNIX UTC timestamp timeClient.update(); unsigned long epochTime = timeClient.getEpochTime(); // convert received time stamp to time_t object time_t local, utc; utc = epochTime; // Then convert the UTC UNIX timestamp to local time TimeChangeRule usEDT = {"EDT", Second, Sun, Mar, 2, +120}; //UTC - 5 hours - change this as needed TimeChangeRule usEST = {"EST", First, Sun, Nov, 2, +180}; //UTC - 6 hours - change this as needed Timezone usEastern(usEDT, usEST); local = usEastern.toLocal(utc); // now format the Time variables into strings with proper names for month, day etc date += days[weekday(local)-1]; date += ", "; date += months[month(local)-1]; date += " "; date += day(local); date += ", "; date += year(local); // format the time to 12-hour format with AM/PM and no seconds if (show24hr) { t += hour(local); } else { t += hourFormat12(local); } t += ":"; if(minute(local) < 10) // add a zero if minute is under 10 t += "0"; t += minute(local); t += ":"; if(second(local) < 10) // add a zero if minute is under 10 t += "0"; t += second(local); // Display the date and time Serial.println(""); Serial.print("Local date: "); Serial.print(date); Serial.println(""); Serial.print("Local time: "); Serial.print(t); Serial.println(""); // print the date and time on the OLED display.clear(); display.setTextAlignment(TEXT_ALIGN_CENTER); display.setFont(Unkempt_Bold_24); display.drawStringMaxWidth(64, 10, 128, t); display.setFont(ArialMT_Plain_10); display.drawStringMaxWidth(64, 38, 128, date); display.display(); } else // attempt to connect to wifi again if disconnected { display.clear(); display.drawString(0, 10, "Connecting to Wifi..."); display.display(); WiFi.begin(ssid, password); display.drawString(0, 24, "Connected."); display.display(); } if(counter == 15) //Get new data every 10 minutes { result = ""; counter = 0; displayGettingData(); delay(1000); getWeatherData(); }else { counter++; displayWeather(weatherLocation, weatherDescription); delay(5000); displayConditions(Temperature, Humidity, Pressure); delay(5000); displayConditions_minmax(Temperature_max, Temperature_min); delay(5000); } } void getWeatherData() //client function to send/receive GET request data. { if (client.connect(weatherHost, 80)) { //starts client connection, checks for connection client.println("GET /data/2.5/weather?id="+CityID+"&units=metric&APPID="+APIKEY); client.println("Host: api.openweathermap.org"); client.println("User-Agent: ArduinoWiFi/1.1"); client.println("Connection: close"); client.println(); } else { Serial.println("connection failed"); //error message if no client connect Serial.println(); } while(client.connected() && !client.available()) delay(1); //waits for data while (client.connected() || client.available()) { //connected or data available char c = client.read(); //gets byte from ethernet buffer result = result+c; } client.stop(); //stop client result.replace('[', ' '); result.replace(']', ' '); Serial.println(result); char jsonArray [result.length()+1]; result.toCharArray(jsonArray,sizeof(jsonArray)); jsonArray[result.length() + 1] = '\0'; DynamicJsonBuffer json_buf; JsonObject &root = json_buf.parseObject(jsonArray); if (!root.success()) { Serial.println("parseObject() failed"); } String location = root["name"]; String country = root["sys"]["country"]; float temperature = root["main"]["temp"]; float temperature_max = root["main"]["temp_max"]; float temperature_min = root["main"]["temp_min"]; float humidity = root["main"]["humidity"]; String description = root["weather"]["main"]; float pressure = root["main"]["pressure"]; Pressure = pressure; weatherDescription = description; weatherLocation = location; Country = country; Temperature = temperature; Temperature_max = temperature_max; Temperature_min = temperature_min; Humidity = humidity; } void displayWeather(String& location, String& description) { lcd.clear(); lcd.setCursor(0,0); lcd.print(location); lcd.print(", "); lcd.print(Country); lcd.setCursor(0,1); lcd.print(description); } void displayConditions(float Temperature, float Humidity, float Pressure) { lcd.clear(); //Printing Temperature lcd.print("T:"); lcd.print(Temperature,1); lcd.print((char)223); lcd.print("C "); lcd.print(" H:"); //Printing Humidity lcd.print(Humidity,0); lcd.print(" %"); lcd.setCursor(0,1); //Printing Pressure lcd.print("P: "); lcd.print(Pressure,1); lcd.print(" hPa"); } void displayConditions_minmax(float Temperature_max, float Temperature_min) { lcd.clear(); //Printing Temperature lcd.print("T_min:"); lcd.print(Temperature_min,1); lcd.print((char)223); lcd.print("C "); lcd.setCursor(0,1); //Printing min temp lcd.print("T_max:"); lcd.print(Temperature_max,1); lcd.print((char)223); lcd.print("C "); } void displayGettingData() { lcd.clear(); lcd.print("Getting data.."); }Строки №№ 171, 173 и 175 - как раз задержка на 15 секунд.
в строках 165, 171, 173, 175
Строки №№ 171, 173 и 175 - как раз задержка на 15 секунд.
в строках 165, 171, 173, 175
с погодой как раз таки всё нормально.. проблема с "опозданием" отображения времени на oled экране
Так вот из-за этого и проблема. Вы там сидите 15 секунд в задержке, а потом удивляете, почему 15 секунд в программе ничего не происходит
Так вот из-за этого и проблема. Вы там сидите 15 секунд в задержке, а потом удивляете, почему 15 секунд в программе ничего не происходит
теперь дошло. а как же тогда сделать плавный переход между показаниями погоды спустя пару секунд, но без ущерба для показаний времени?
а как же тогда сделать плавный переход между показаниями погоды спустя пару секунд, но без ущерба для показаний времени?
Дак это программировать нада.
а как же тогда сделать плавный переход между показаниями погоды спустя пару секунд, но без ущерба для показаний времени?
Дак это программировать нада.
можно пример?
можно пример?
От меня - нет, это ж нада трудица, а у мня тут трэш и угар.
Так вот из-за этого и проблема. Вы там сидите 15 секунд в задержке, а потом удивляетесь, почему 15 секунд в программе ничего не происходит
теперь дошло. а как же тогда сделать плавный переход между показаниями погоды спустя пару секунд, но без ущерба для показаний времени?
Научиться делать неблокирущие задержки. Т.е. писать так, чтобы действие задержалось, но программа при этом продолжала исполняться. В частности, экран обновлялся и т.п.
Пример такого программирования подробно разобран, например, вот здесь. Там во время задержки между включением и выключением светодиода программа продолжает исполняться и делать то, что ей нужно.