putting lots of stuff into a string

For my first effort I’m trying to write a program for a ESP8266-E12 in the arduino IDE.
The overall objective of the program is retrieve the half hourly price data from Octopus Energy’s API and then either turn on or off a relay depending upon the price.

So far, using various tutorials and example code, I can connect to the wifi, get the sntp time, print to the serial port the time in the right format for now and 30 mins ahead and get the JSON price data from Octopus using a fixed URL.

What I’m having problems with is getting the time data into the URL. The only way I can see do it is to convert each of year, month, day, hour, minute, colons and letters required into lots of strings and then add them into one big string.

is there a more efficient way?

time_got_json_got_making_link.ino (6.01 KB)

If you are talking about getting the time from somewhere, like this from your code:

    unsigned long epochTime = timeClient.getEpochTime();

    String formattedTime = timeClient.getFormattedTime();

and using it to create a link like this from your code:

    String Link = "/v1/products/AGILE-18-02-21/electricity-tariffs/E-1R-AGILE-18-02-21-E/standard-unit-rates/?period_from=2021-03-04T12:05:00Z&period_to=2021-03-04T12:30:00Z" ;

There are lotsa ways to do it, most of which will make you feel like you are on your hands and knees. Welcome to computer programming.

There is no particularly efficient way to do this. And there is so little gong on, efficiency no matter how you measure it isn't likely to be a problem.

Capital 'S' Strings don't need to enter into it, unless you need to make one to pass off to some library. Until that point, regular small 's' strings or char arrays will be fine and the sprintf function works well to create formatted strings.

So learn about or know already and use char arrays, also check out sprintf, it may not be efficient but it will look prettier.

a7

We prefer that you put the sketch in your post between code-tags. The </> button is for code-tags.

What happens if it is 15 minutes before New Year ? Then calculating the future time in 30 minutes will fail.
The common way to do that is to use the number of seconds since 1970 (‘epoch’ or unix time).
Simply add the right amount of seconds, and use the standard function to convert it to hours, minutes, seconds, and so on.

Do you need the time in the URL for the API ?

The NTPClient library uses the non-standard functions such as .getDay(). That library is limited in its use. However, I see that library is also uses here: Get Epoch/Unix Time with the ESP8266 NodeMCU (Arduino) | Random Nerd Tutorials, so I guess the library is okay.

Of course you need all the .getDay() .getHours() .getMinutes() and other functions.
Are you worried about a few lines of code to make the right formatted text ? That is no problem, as long as your sketch is doing what it should do.
If you want, you can do the formatting with sprintf().

How long can you afford to wait for 20 seconds in the loop() with a delay() ? Isn’t there something that you would like to do while being stuck in that delay ?

While I was typing this, alto777 wrote the same

Koepel:
We prefer that you put the sketch in your post between code-tags. The </> button is for code-tags.

What happens if it is 15 minutes before New Year ? Then calculating the future time in 30 minutes will fail.
The common way to do that is to use the number of seconds since 1970 (‘epoch’ or unix time).
Simply add the right amount of seconds, and use the standard function to convert it to hours, minutes, seconds, and so on.

Do you need the time in the URL for the API ?

The NTPClient library uses the non-standard functions such as .getDay(). That library is limited in its use. However, I see that library is also uses here: Get Epoch/Unix Time with the ESP8266 NodeMCU (Arduino) | Random Nerd Tutorials, so I guess the library is okay.

Of course you need all the .getDay() .getHours() .getMinutes() and other functions.
Are you worried about a few lines of code to make the right formatted text ? That is no problem, as long as your sketch is doing what it should do.
If you want, you can do the formatting with sprintf().

How long can you afford to wait for 20 seconds in the loop() with a delay() ? Isn’t there something that you would like to do while being stuck in that delay ?

While I was typing this, alto777 wrote the same

Many thanks

use of tags noted.

Yes, the URL needs to specify the date and time from and to get the current price. and it needs to be in form
?period_from=2021-03-04T12:00:00Z&period_to=2021-03-04T12:30:00Z for example to get the price data for the 30mins from midday to 12:30 on March the 4th of this year. As far as I know, there is no URL that says “just give me the current price please”

Not just new year, but also end of every day and month will give incorrect value as so far I’ve only accounted for increasing the hour when adding 30mins…it’s on the list to do!

The 20s delay is only there while I’m trying things out, it only needs to be checked every 30 mins.
I’ll have a look at sprintf()

You don't have to quote my post. What I wrote is already there ;)

sprintf did the trick, thanks again

sprintf() is a good solution if you have plenty of memory available. The function itself needs memory and in addition to that you need the buffer into which the output is created from existing data, thus often doubling the memory requirement. This can be ameliorated to some extent by using a buffer with local scope to the function that it is used in so that memory is freed when the function exits