...
Codeblock | ||
---|---|---|
| ||
// This pico-c program returns the rank of the current energy price in the next nrHours.
// This is useful if you want to e.g. charge your car for 5 hours over night until 7AM:
// just call this program with the number of hours until 7AM and check if the result is <= 5
//
// Note: Change "api.awattar.at" to "api.awattar.de" when you are in Germany
//
// Apache License 2.0
// v0.1
// input0 = number of hours to check rank of current price
// output0 = current price
// output1 = rank of current price in given numbers of hours to check
// output2 = rank of current price in all available prices
//get the rank of a given price in an array of prices up to maxRank
int getRank(float price, int maxRank, float *prices) {
int n; // counter for hours
int newn;
int i; // counter
float tmpPrice;
int rank;
//bubble sort the first nHours prices
for (n = maxRank-1; n>0; n = newn) {
newn = 0;
for (i=0; i<n; i++) {
if (prices[i] > prices[i+1]) {
tmpPrice = prices[i+1];
prices[i+1] = prices[i];
prices[i] = tmpPrice;
newn = i;
}
}
}
//determine the rank of the current price amongst the first nHours of the sorted prices
rank = maxRank;
for (i=0; i<maxRank; i++) {
if (price == prices[i]) {
rank = i+1;
break;
}
}
return rank;
}
//declaration of all variables outside of loop to prevent out-of-memory errors
int nrHours; // number of hours to take into account
char* webContent; //content of https://api.awattar.at/v1/marketdata/current.yaml?tomorrow=include
char* marketprice; //marketprice content of https://api.awattar.at/v1/marketdata/current.yaml?tomorrow=include
char hournum[42]; //string to search for in webContent to get the marketprice of a specific hour
float prices[34]; // prices according to aWATTar API - up to 34 hours (API returns prices until EOD up to 14:00, later until end of next day)
float currentPrice; // the current price i.e. the price of the 0th hour
int n; // counter for hours
//for testing with pico-c interpreter see https://loxwiki.atlassian.net/wiki/spaces/LOX/pages/1520762981/PicoC+Interpreter
//though most functions will not work in interpreter
getinputevent(); //to clear buffer of changed inputs (to not run the loop twice)
while(TRUE) {
sleeps(30); //sleep some seconds to not fetch data again while input still changes and get correct data when aWATTar is late
webContent = httpget("api.awattar.at", "/v1/marketdata/current.yaml?tomorrow=include"); //fetch all available data
//fill marketprice array
for (n=0; n<34; n++) {
sprintf(hournum, "data_price_hour_rel_+%02d_amount: ", n);
marketprice = strstr(webContent, hournum);
if (marketprice == 0) break;
prices[n] = atof(marketprice+32); //fetch value after search-string "date_price_..."
}
free(webContent);
currentPrice = prices[0];
setoutput(0, currentPrice);
if (getinput(0) < n) {
nrHours = getinput(0);
} else {
nrHours = n;
}
setoutput(1, getRank(currentPrice, nrHours, prices));
setoutput(2, getRank(currentPrice, n, prices));
//sleep until next hour (or nrHours changes) so we need to fetch only once an hour
while(getcurrenttime()%3600 && !getinputevent()) {
sleeps(1);
}
}
|
...