Skip to content

Instantly share code, notes, and snippets.

@hazulifidastian
Last active December 10, 2022 06:22
Show Gist options
  • Save hazulifidastian/27177932b7b58caab689702216a6ef8d to your computer and use it in GitHub Desktop.
Save hazulifidastian/27177932b7b58caab689702216a6ef8d to your computer and use it in GitHub Desktop.
Uji coba queue arduino. Simulator ESP32 wokwi.com
// Learn about the ESP32 WiFi simulation in
// https://docs.wokwi.com/guides/esp32-wifi
#include <WiFi.h>
#include <Wire.h>
#include <ArduinoQueue.h>
#define NTP_SERVER "pool.ntp.org"
#define UTC_OFFSET 0
#define UTC_OFFSET_DST 0
struct Rainfall {
std::string timeSampling;
std::string sentAt;
unsigned short int counter;
float humidity;
float voltage;
float temperature;
};
ArduinoQueue <Rainfall> queue(50);
std::string getTime() {
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
Serial.println("Error");
return "";
}
int buffer_size = 80;
char buffer[buffer_size];
strftime(buffer, buffer_size, "%Y-%m-%d %H:%M:%S", &timeinfo);
return buffer;
}
// the setup function runs once when you press reset or power the board
void setup() {
Serial.begin(115200);
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(250);
}
configTime(UTC_OFFSET, UTC_OFFSET_DST, NTP_SERVER);
}
// the loop function runs over and over again forever
void loop() {
Serial.println("PUSH");
int size = 50;
for (int i = 0; i < size; i++) {
Rainfall rainfall = *new Rainfall{
getTime(),
"",
static_cast<unsigned short int>(std::rand() % 500),
static_cast<float>(std::rand() % 10),
static_cast<float>(std::rand() % 5),
static_cast<float>(std::rand() % 15),
};
queue.enqueue(rainfall);
Serial.println(rainfall.timeSampling.c_str());
delay(1000);
}
Serial.println("PUSHED");
// ----------------------
Serial.println("POP");
for (int i = 0; i < size; i++) {
Rainfall poppedRainfall;
poppedRainfall = queue.dequeue();
Serial.println(poppedRainfall.timeSampling.c_str());
}
Serial.println("POPPED");
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment