#define BLYNK_TEMPLATE_ID "TMPL6NuEigcDS"
#define BLYNK_TEMPLATE_NAME "Gate"
#define BLYNK_AUTH_TOKEN "91ygPDqTJ_3KeD-SXyj81lFsHDOgwOwR"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "esp_system.h"
#include "esp_chip_info.h"
#include "esp_heap_caps.h"
size_t totalHeap = heap_caps_get_total_size(MALLOC_CAP_8BIT); // 获取总的堆内存大小
size_t freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT); // 获取当前可用的堆内存大小
int freeHeapPercent = (freeHeap * 100) / totalHeap;
float temperature = temperatureRead(); // 获取温度
int chipTemperature = (temperature - 32) / 1.8;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
BlynkTimer timer;
bool power = false;
int value = 0;
unsigned long long lasttime = 0;
// This function is called every time the Virtual Pin 0 state changes
BLYNK_WRITE(V0)
{
// Set incoming value from pin V0 to a variable
value = param.asInt();
if (value == 1) {
power = true;
} else if (value == 0) {
power = false;
}
}
// This function is called every time the device is connected to the Blynk.Cloud
BLYNK_CONNECTED()
{
// Change Web Link Button message to "Congratulations!"
Blynk.syncVirtual(V0);
}
// This function sends Arduino's uptime every second to Virtual Pin 2.
void myTimerEvent()
{
// Update state
Blynk.virtualWrite(V2, chipTemperature);
Blynk.virtualWrite(V3, freeHeapPercent);
}
void setup()
{
// Debug console
Serial.begin(115200);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
timer.setInterval(1000L, myTimerEvent);
pinMode(19, OUTPUT);
}
void loop()
{
Blynk.run();
timer.run();
if (power) {
digitalWrite(19, HIGH);
Blynk.virtualWrite(V1, value);
} else {
digitalWrite(19, LOW);
Blynk.virtualWrite(V1, value);
}
if (millis() - lasttime >= 10000 || lasttime == 0) {
status();
}
}
void status() {
totalHeap = heap_caps_get_total_size(MALLOC_CAP_8BIT); // 获取总的堆内存大小
freeHeap = heap_caps_get_free_size(MALLOC_CAP_8BIT); // 获取当前可用的堆内存大小
freeHeapPercent = (freeHeap * 100) / totalHeap;
temperature = temperatureRead(); // 获取温度
chipTemperature = (temperature - 32) / 1.8;
}