Hello!
I started messing with Arduino because of curiosity and wanting to learn something about something new. I have no programming skills. Nothing. I just started reading Arduino sample codes and tried to figure out how they worked.
I tried to do modifications to see if I could adapt them to my needs and little by little I got to do various things.
I set out to do a meteorological station with a small Oled screen and I got it, but it did not seem like much to me, so I decided to do another one with a colour LCD with touch-screen. It is almost finished. I could say that it is. But I have a big free space in the second screen, and here is where I don´t have the knowledge to program what I want to show in it.
If someone wanted to help me to solve the code I would be very happy.
Without entering in unuseful details I’ll explain the assembly:
The station consists in one Arduino Mega (the sketch is so long that I could not fit it into a Uno), a temperature, humidity and pressure sensor BME280, a WiFi module ESP8266-01 and a logic converter to communicate the WiFi low-voltage module with the Mega.
At boot time 3 data requests are made through the Wunderground servers to a station located very close to my house. A current conditions request, a forecast request and a lunar phase request. All this is done with AT commands sent to the WiFi module.
The main screen shows the current data of indoor temperature and humidity, outdoor temperature and humidity, current weather (with icon), current wind speed (with icon), moon phase (with icon) and sunrise-sunset - moonrise-moonset.
Touching the screen it switches to screen 2, which shows the weather forecast for today, tomorrow and the day after tomorrow (with icons), minimum and maximum temperatures and probability of rain. There is also a touch button at the bottom to force an update of all the parameters, in case that the initial Parse had failed (sometimes it happens, the parse fails or stops, and I do not know why).
That is the basics of the station.
I could leave it like this, but on the second screen I have a free space of almost half screen, and I would like to add 3 more strings of information provided by Wunderground, but I do not know how to modify the Parse method for it. I have tried and tried to modify the code without knowing very well what I am doing, and I am already desperate not to get results. In the Parse process I always get the message “ParseObject () has failed”.
Attached picture is a complete capture of what I receive from Wunderground when doing request. Blue-marked are the chains that I currently read and use. Red-marked are the 3 strings that I want to read and I don´t know how to do it, of course, adding the reading of the blue ones too.
And here are the parts of code that I currently use (and that would need to be modified):
...
#include <ArduinoJson.h>
#define wifi Serial1
...
char* forecast[] = {"\"weekday\":", "\"celsius\":", "\"celsius\":", "\"icon\":", "\"pop\":", "\"weekday\":", "\"celsius\":", "\"celsius\":", "\"icon\":", "\"pop\":", "\"weekday\":", "\"celsius\":", "\"celsius\":", "\"icon\":", "\"pop\":"};
int num_elements_fr = 15; // number of elements for the forecast array
char* forecast_rename[] = {"\"day_0\":", "\"high_0\":", "\"low_0\":", "\"icon_0\":", "\"pop_0\":", "\"day_1\":", "\"high_1\":", "\"low_1\":", "\"icon_1\":", "\"pop_1\":", "\"day_2\":", "\"high_2\":", "\"low_2\":", "\"icon_2\":", "\"pop_2\":"}; //rename for each different day
const int buffer = 400; // length of json buffer
char close_brace = '}'; // neccessary for parse
char comma = ','; // neccessary for parse
const char* day0; // array prevision Wunderground
float high0; // array prevision Wunderground
float low0; // array prevision Wunderground
float pop_0; // array prevision Wunderground
const char* icono0; // array prevision Wunderground
const char* day1; // array prevision Wunderground
float high1; // array prevision Wunderground
float low1; // array prevision Wunderground
float pop_1; // array prevision Wunderground
const char* icono1; // array prevision Wunderground
const char* day2; // array prevision Wunderground
float high2; // array prevision Wunderground
float low2; // array prevision Wunderground
float pop_2; // array prevision Wunderground
const char* icono2; // array prevision Wunderground
...
void setup() {
wifi.begin(57600);
wifi.setTimeout(5000);
Serial.begin(57600);
...
// try to connect to wifi
boolean connected = false;
for (int i = 0; i < 5; i++) {
if (connectWiFi()) {
connected = true;
...
updatePrevision();
break;
}
}
}
void loop() {
...
}
void updatePrevision() {
retry:
...
wifi.flush();
wifi.println("AT+CIPSTART=\"TCP\",\"23.222.152.140\",80");
if (wifi.find("Error")) return;
// (My API) (direct station)
String cmd2 = "GET /api/XXXXXXXXXXXXXXXX/forecast/lang:SP/q/pws:ILANGREO3.json HTTP/1.1\r\nHost: api.wunderground.com\r\n\r\n";
wifi.print("AT+CIPSEND=");
wifi.println(cmd2.length());
delay(2000);
if (wifi.find(">")) {
Serial.print(F(">>"));
} else {
wifi.println(F("AT+CIPCLOSE"));
Serial.println(F("Fail"));
delay(2000);
goto retry;
}
wifi.print(cmd2);
unsigned int i = 0; //timeout counter
char json[buffer] = "{"; // array for Json parsing
int n = 1; // character counter for json
while (!wifi.find("\"simpleforecast\": {")) {}
for (int j = 0; j < num_elements_fr; j++) {
while (!wifi.find(forecast[j])) {} // find the part we are interested in.
String Str1 = forecast_rename[j];
for (int l = 0; l < (Str1.length()); l++) {
json[n] = Str1[l];
n++;
}
Serial.print(".");
while (i < 60000) {
if (wifi.available()) {
char c = wifi.read();
if (c == ',') {
break;
}
if (c == '}') {
n--;
n--;
n--;
break;
}
json[n] = c;
n++;
i = 0;
}
i++;
}
if (j == num_elements_fr - 1) {
json[n] = close_brace;
} else {
json[n] = comma;
}
n++;
}
parseForecastJSON(json);
Serial.print("/ ");
wifi.println("AT+CIPCLOSE");
delay(2000);
...
}
void parseForecastJSON(char json[400]) {
StaticJsonBuffer<buffer> jsonBuffer;
JsonObject& root = jsonBuffer.parseObject(json);
if (!root.success()) {
Serial.println(F("parseObject() failed"));
wifi.println(F("AT+CIPCLOSE"));
delay(2000);
updatePrevision();
}
day0 = root["day_0"];
high0 = root["high_0"];
low0 = root["low_0"];
icono0 = root["icon_0"];
pop_0 = root["pop_0"];
day1 = root["day_1"];
high1 = root["high_1"];
low1 = root["low_1"];
icono1 = root["icon_1"];
pop_1 = root["pop_1"];
day2 = root["day_2"];
high2 = root["high_2"];
low2 = root["low_2"];
icono2 = root["icon_2"];
pop_2 = root["pop_2"];
}
...
I have to beg for help because I know I won´t be able to solve this problem on my own. I read some documentation about that but I don´t understand it, this problem is too big for me and my little knowledge.
Thanks in advance to anyone who wants to waste his time helping me.