Hi everyone,
Been pulling my hair out on this one for a week now with no end in sight. I’m using this custom component called powerpal_ble to capture data from my Powerpal. The problem is that the API I need to POST to only supports HTTPS and I’m unable to configure http_request component to work properly with any HTTPS endpoint. It works fine with unsecured ones though. I’m using M5 Atom ESP32 to run this. Relevant sections from powerpal_ble.cpp are pasted below:
#ifdef USE_HTTP_REQUEST
this->stored_measurements_.resize(1); //TODO dynamic
this->cloud_uploader_->set_method("POST");
if(this->cloud_uploader_ != nullptr) {
this->store_measurement_(
pulses_within_interval,
unix_time,
(uint32_t)roundf(pulses_within_interval * (this->pulses_per_kwh_ / kw_to_w_conversion)),
(pulses_within_interval / this->pulses_per_kwh_) * this->energy_cost_
);
if (this->stored_measurements_count_ == 1) {
this->upload_data_to_cloud_();
}
}
void Powerpal::store_measurement_(uint16_t pulses, time_t timestamp, uint32_t watt_hours, float cost) {
this->stored_measurements_count_++;
this->stored_measurements_[this->stored_measurements_count_].pulses = pulses;
this->stored_measurements_[this->stored_measurements_count_].timestamp = timestamp;
this->stored_measurements_[this->stored_measurements_count_].watt_hours = watt_hours;
this->stored_measurements_[this->stored_measurements_count_].cost = cost;
}
void Powerpal::upload_data_to_cloud_() {
this->stored_measurements_count_ = 0;
if (this->powerpal_device_id_.length() && this->powerpal_apikey_.length()) {
StaticJsonDocument<2048> doc; // 768 bytes, each entry may take up 15 bytes (uint16_t + uint32_t + uint32_t + float + bool)
JsonArray array = doc.to<JsonArray>();
for (int i = 0; i < 1; i++) {
JsonObject nested = array.createNestedObject();
nested["cost"] = this->stored_measurements_[i].cost;
nested["is_peak"] = false;
nested["pulses"] = this->stored_measurements_[i].pulses;
nested["timestamp"] = this->stored_measurements_[i].timestamp;
nested["watt_hours"] = this->stored_measurements_[i].watt_hours;
}
std::string body;
serializeJson(doc, body);
this->cloud_uploader_->set_body(body);
// empty triggers, but requirement of using the send function
std::vector<http_request::HttpRequestResponseTrigger *> response_triggers_;
this->cloud_uploader_->send(response_triggers_);
} else {
// apikey or device missing
}
}
this->powerpal_api_root_.append(this->powerpal_device_id_);
this->cloud_uploader_->set_url(this->powerpal_api_root_);
http_request::Header acceptheader;
acceptheader.name = "Accept";
acceptheader.value = "application/json";
http_request::Header contentheader;
contentheader.name = "Content-Type";
contentheader.value = "application/json";
http_request::Header authheader;
authheader.name = "Authorization:";
authheader.value = this->powerpal_apikey_.c_str();
ESP_LOGI(TAG, "auth_header: %s", authheader.value);
//ESP_LOGI(TAG, "auth_header_name: %s", authheader);
std::list<http_request::Header> headers;
headers.push_back(acceptheader);
headers.push_back(contentheader);
headers.push_back(authheader);
this->cloud_uploader_->set_headers(headers);
#endif
From powerpal_ble.h:
std::string powerpal_api_root_ = "https://readings.powerpal.net/api/v1/meter_reading/";
std::string powerpal_device_id_; // = "00002bc3";
std::string powerpal_apikey_; // = "4a89e298-b17b-43e7-a0c1-fcd1412e98ef";
float energy_cost_;
I came across this but just can’t figure out how to use it in the code above.
Any help would be greatly appreciated! Thanks in advance.