Hi
I'm working on a small project to display actual time with date and two departure times from a online timetable.
Steps:
get time
get timetable information via curl
display time/date and two departures times
Also I have not so much program storage space left.
Sketch uses 28,080 bytes (97%) of program storage space. Maximum is 28,672 bytes.
Global variables use 863 bytes (33%) of dynamic memory, leaving 1,697 bytes for local variables. Maximum is 2,560 bytes.
here the code what I have right now:
#include <Process.h>
#include <MD_Parola.h>
#include <MD_MAX72xx.h>
#define MAX_ZONES 2
#define ZONE_SIZE 5
#define MAX_DEVICES (MAX_ZONES * ZONE_SIZE)
#define ZONE_UPPER 1
#define ZONE_LOWER 0
#define CLK_PIN 11
#define DATA_PIN 12
#define CS_PIN 10
MD_Parola P = MD_Parola(DATA_PIN, CLK_PIN, CS_PIN, MAX_DEVICES);
char dep1[8];
char dep2[8];
int seconds;
int lastSecond = -1;
String timeStamp;
String dateStamp;
String ts;
Process pc; // process for curl, used to get timetable information
Process date; // process used to get time
void setup() {
Bridge.begin(); // Initialize Bridge
Serial.begin(57600);
while (!Serial);
Serial.println("Start Setup");
P.begin(MAX_ZONES);
P.setZone(ZONE_LOWER, 0, ZONE_SIZE - 1);
P.setZone(ZONE_UPPER, ZONE_SIZE, MAX_DEVICES - 1);
Serial.println("End Setup");
}
void loop() {
getTime();
getDepartures();
toMatrix();
}
String urlDecode(String urlChars) {
urlChars.replace("[", "%5B");
urlChars.replace("]", "%5D");
urlChars.replace("ä", "%C3%A4");
return urlChars;
}
void getDepartures() {
char jsonResponse[200];
String url = "http://transport.opendata.ch/v1/connections?from=Kräyigen&to=Bern&limit=2&fields[]=connections/from/departure&time=" + ts;
Serial.println(url);
pc.begin("curl");
pc.addParameter(" - H \"Cache-Control: no-cache\"");
pc.addParameter(urlDecode(url));
pc.run();
// response
int i = 0;
while (pc.available() > 0) {
char c = pc.read();
jsonResponse[i] = c;
i++;
}
char *ptr; // temp pointers
char *fieldPtr; // temp pointers
// get departure time 1
ptr = strstr(jsonResponse, "T");
if (ptr != NULL) {
ptr++;
fieldPtr = ptr;
ptr = strstr(ptr, "+");
ptr = '\0';
strcpy(dep1, "1. ");
strncat(dep1, fieldPtr, 5);
}
// get departure time 2
ptr = strstr(fieldPtr, "T");
if (ptr != NULL) {
ptr++;
fieldPtr = ptr;
ptr = strstr(ptr, "+");
ptr = '\0';
strcpy(dep2, "2. ");
strncat(dep2, fieldPtr, 5);
}
}
void getTime() {
Serial.println("get time ...");
if (lastSecond != seconds) {
if (!date.running()) {
date.begin("date");
date.addParameter("+%d.%m.%y %T");
date.run();
}
}
while (date.available() > 0) {
timeStamp = date.readString();
timeStamp.trim();
dateStamp = timeStamp.substring(0, 8);
timeStamp = timeStamp.substring(9, 17);
int secondColon = timeStamp.lastIndexOf(":");
String secString = timeStamp.substring(secondColon + 1);
lastSecond = seconds;
seconds = secString.toInt();
ts = timeStamp.substring(0, 5); // timestamp needed for curl query
}
}
void toMatrix() {
char charBuf[6];
ts.toCharArray(charBuf, 6);
P.displayZoneText(ZONE_LOWER, charBuf, CENTER, 40, 5000, SCROLL_DOWN, SCROLL_UP);
P.displayZoneText(ZONE_UPPER, dep1, CENTER, 40, 5000, SCROLL_DOWN, SCROLL_UP);
// synchronise the start and run the display to completion
P.synchZoneStart();
while (!P.getZoneStatus(ZONE_LOWER) || !P.getZoneStatus(ZONE_UPPER))
P.displayAnimate();
char charBuf2[9];
dateStamp.toCharArray(charBuf2, 9);
P.displayZoneText(ZONE_LOWER, charBuf2, CENTER, 40, 5000, SCROLL_UP, SCROLL_DOWN);
P.displayZoneText(ZONE_UPPER, dep2, CENTER, 40, 5000, SCROLL_UP, SCROLL_DOWN);
// synchronise the start and run the display to completion
P.synchZoneStart();
while (!P.getZoneStatus(ZONE_LOWER) || !P.getZoneStatus(ZONE_UPPER))
P.displayAnimate();
}
The time/data part is working. The curl part itself is also working.
I have some problem with read out of the departure times.
Curl is running the url and the response is as follows:
{
"connections": [{
"from": {
"departure": "2016-01-05T15:53:00+0100"
}
}, {
"from": {
"departure": "2016-01-05T15:54:00+0100"
}
}]
}
from this json I need just the timestamp ->15:53 and 15:54
When I upload the whole sketch noting will display on the LED Matrix. When I uncomment the block
// get departure time 1
...
and
// get departure time 2
...
then the time and date on the LED matrix is shown.
Some one has an idea how to optimize the part to read out the time?
THX
Pascal