Hi
i’ve just started working on a project with an esp32s3
i’ve made a program to send some data to Prometheus, except that the program doesn’t seem to be willing to run if i don’t use serial
Code
#include <Wire.h>
#include <esp_sleep.h>
#include <PromLokiTransport.h>
#include <PrometheusArduino.h>
#define WAKEUP_TIME_SEC 300 // 5 minutes
#define NO_TOUCH 0xFE
#define THRESHOLD 100
#define ATTINY1_HIGH_ADDR 0x78
#define ATTINY2_LOW_ADDR 0x77
unsigned char low_data[8] = {0};
unsigned char high_data[12] = {0};
PromLokiTransport transport;
PromClient client(transport);
// Create a write request for 2 series.
WriteRequest req(2);
TimeSeries ts1(5, “battery_level”, “{job=“esp32-test”,host=“esp32”}”);
TimeSeries ts2(5, “water_level”, “{job=“esp32-test”,host=“esp32”,foo=“bar”}”);
int loopCounter = 0;
void setup() {
Wire.begin();
Serial.begin(115200);
esp_sleep_enable_timer_wakeup(WAKEUP_TIME_SEC * 1000000); // Time in microseconds
transport.setWifiSsid("REDACTED");
transport.setWifiPass("REDACTED");
if (!transport.begin()) {
Serial.println(transport.errmsg);
while (true) {};
}
// Configure the client
client.setUrl("REDACTED");
client.setPath("REDACTED");
client.setPort(REDACTED);
if (!client.begin()) {
Serial.println(client.errmsg);
while (true) {};
}
// Add our TimeSeries to the WriteRequest
req.addTimeSeries(ts1);
req.addTimeSeries(ts2);
Serial.print("Setup Complete");
};
void loop() {
Serial.println("loop");
collect_send();
delay(5000);
};
void collect_send(void)
{
int64_t time;
time = transport.getTimeMillis();
if (!ts1.addSample(time, check_battery())) {
Serial.println(ts1.errmsg);
}
if (!ts2.addSample(time, check())) {
Serial.println(ts2.errmsg);
}
loopCounter++;
if (loopCounter >= 4) {
Serial.println("4loop");
// Send data
loopCounter = 0;
PromClient::SendResult res = client.send(req);
if (!res == PromClient::SendResult::SUCCESS) {
Serial.println(client.errmsg);
}
ts1.resetSamples();
ts2.resetSamples();
esp_deep_sleep_start();
}
}
void getHigh12SectionValue(void)
{
memset(high_data, 0, sizeof(high_data));
Wire.requestFrom(ATTINY1_HIGH_ADDR, 12);
while (12 != Wire.available());
for (int i = 0; i < 12; i++) {
high_data[i] = Wire.read();
}
delay(10);
}
void getLow8SectionValue(void)
{
memset(low_data, 0, sizeof(low_data));
Wire.requestFrom(ATTINY2_LOW_ADDR, 8);
while (8 != Wire.available());
for (int i = 0; i < 8 ; i++) {
low_data[i] = Wire.read(); // receive a byte as character
}
delay(10);
}
double check_battery(void)
{
uint32_t Vbatt = 0;
for(int i = 0; i < 16; i++) {
Vbatt = Vbatt + analogReadMilliVolts(A0); // ADC with correction
}
float Vbattf = 2 * Vbatt / 16 / 1000.0;
return (double)Vbattf;
}
int check()
{
int sensorvalue_min = 250;
int sensorvalue_max = 255;
int low_count = 0;
int high_count = 0;
while (1)
{
uint32_t touch_val = 0;
uint8_t trig_section = 0;
low_count = 0;
high_count = 0;
getLow8SectionValue();
getHigh12SectionValue();
for (int i = 0; i < 8; i++)
{
if (low_data[i] >= sensorvalue_min && low_data[i] <= sensorvalue_max)
{
low_count++;
}
}
for (int i = 0; i < 12; i++)
{
if (high_data[i] >= sensorvalue_min && high_data[i] <= sensorvalue_max)
{
high_count++;
}
}
for (int i = 0 ; i < 8; i++) {
if (low_data[i] > THRESHOLD) {
touch_val |= 1 << i;
}
}
for (int i = 0 ; i < 12; i++) {
if (high_data[i] > THRESHOLD) {
touch_val |= (uint32_t)1 << (8 + i);
}
}
while (touch_val & 0x01)
{
trig_section++;
touch_val >>= 1;
}
return(trig_section*5);
}
}
I know i’m missing something, but i can’t put my finger on it