Skip to content

Instantly share code, notes, and snippets.

@freol35241
Last active October 4, 2022 10:19
Show Gist options
  • Save freol35241/fb0848dd359d3aca25682d91da96fb7d to your computer and use it in GitHub Desktop.
Save freol35241/fb0848dd359d3aca25682d91da96fb7d to your computer and use it in GitHub Desktop.
Arduino code for ESP8266/ESP32 for fetching future electricity prices from the nordic energy price market "Nordpool" using Vattenfall's API
// MIT License
//
// Copyright (c) 2022 freol35241
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.
#ifndef NORDPOOL_H
#define NORDPOOL_H
// Expects the time.h to be updated via a NTP server
// For example:
// #include <time.h>
// configTime(3600, 3600, "pool.ntp.org");
//
// Requires:
// ArduinoJson (https://arduinojson.org/)
// DebugLog (https://github.com/hideakitai/DebugLog)
#include "Arduino.h"
#include "time.h"
#include <HTTPClient.h>
#include <DebugLog.h>
#include <ArduinoJson.h>
namespace Nordpool
{
String url_to_fetch(const String &area, tm *time_info)
{
char today[12]; // should be enough
strftime(today, sizeof(today), "%Y-%m-%d", time_info);
String url = "https://www.vattenfall.se/api/price/spot/pricearea/";
url += today;
url += "/";
url += "2099-12-31";
url += "/";
url += area;
return url;
}
int fetch_upcoming_prices(const String &area, float *prices)
{
HTTPClient client;
tm time_info;
if (!getLocalTime(&time_info))
{
LOG_ERROR("Failed to get local date and time!");
return -1;
}
String url = url_to_fetch(area, &time_info);
client.begin(url.c_str());
int rc = client.GET();
LOG_INFO("Http response code: ", rc);
if (rc != 200)
{
LOG_ERROR("Failed to fetch prices using: ", url);
return -1;
}
DynamicJsonDocument doc(12288);
DeserializationError error = deserializeJson(doc, client.getStream());
if (error)
{
LOG_ERROR("deserializeJson() failed: ", error.c_str());
return -1;
}
bool start_found = false;
int ix = 0;
for (JsonObject item : doc.as<JsonArray>())
{
if (!start_found)
{
uint8_t hour = String((const char *)item["TimeStampHour"]).substring(0, 2).toInt();
if (hour == time_info.tm_hour)
{
start_found = true;
}
else
{
continue;
}
}
prices[ix++] = item["Value"];
}
return ix;
}
}
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment