#define BLYNK_PRINT Serial
#define BLYNK_TEMPLATE_ID "TMPL5gudIlu_d"
#define BLYNK_TEMPLATE_NAME "esp32 LM35 oled version 2"
#define BLYNK_AUTH_TOKEN "YQ_FFMlV3-4eEdzrXQAoLjXvUHI9Gh5p"
#define ledPin 5
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define PIN_LM35 34
#define ADC_VREF_mV 5000.0 // in millivolt, el sensor era para Arduino
#define ADC_RESOLUTION 4096.0
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define SCREEN_ADDRESS 0x3C ///(0x3D) See datasheet for Address;
char ssid[] = "Wokwi-GUEST";
char pass[] = "";
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, -1);
BlynkTimer tiempo;
static const uint8_t image_data_temp[448] = {
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0xe0, 0x00, 0x00,
0x00, 0x00, 0x00, 0x41, 0xb0, 0x00, 0x00,
0x00, 0x00, 0x00, 0xe3, 0x18, 0x00, 0x00,
0x00, 0x00, 0x01, 0xf2, 0x08, 0x00, 0x00,
0x00, 0x00, 0x00, 0x02, 0x08, 0x00, 0x00,
0x00, 0x00, 0x01, 0xfa, 0x0b, 0xfc, 0x00,
0x00, 0x00, 0x07, 0x0e, 0x08, 0x00, 0x00,
0x00, 0x3c, 0x3c, 0x02, 0x08, 0x00, 0x00,
0x00, 0x38, 0xe0, 0x02, 0x08, 0x00, 0x00,
0x00, 0x31, 0x80, 0x02, 0x0b, 0xe0, 0x00,
0x00, 0x23, 0x00, 0x02, 0x08, 0x00, 0x00,
0x00, 0x06, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x0c, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x08, 0x00, 0x02, 0xeb, 0xfc, 0x00,
0x00, 0x18, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x10, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x30, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x20, 0x00, 0x02, 0xeb, 0xe0, 0x00,
0x00, 0x20, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x01, 0x60, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x03, 0x40, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x07, 0x40, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x03, 0x40, 0x00, 0x02, 0xeb, 0xfc, 0x00,
0x01, 0x60, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x20, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x30, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x10, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x18, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x08, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x0c, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x06, 0x00, 0x02, 0xe8, 0x00, 0x00,
0x00, 0x23, 0x00, 0x06, 0xec, 0x00, 0x00,
0x00, 0x31, 0x80, 0x08, 0x62, 0x00, 0x00,
0x00, 0x38, 0xe0, 0x10, 0x39, 0x00, 0x00,
0x00, 0x3c, 0x38, 0x20, 0x3c, 0x80, 0x00,
0x00, 0x00, 0x0c, 0x20, 0x3e, 0x80, 0x00,
0x00, 0x00, 0x07, 0xe0, 0x7e, 0xc0, 0x00,
0x00, 0x00, 0x00, 0x40, 0x7e, 0x40, 0x00,
0x00, 0x00, 0x1f, 0x40, 0xfe, 0x40, 0x00,
0x00, 0x00, 0x0e, 0x61, 0xfe, 0xc0, 0x00,
0x00, 0x00, 0x04, 0x2f, 0xfe, 0x80, 0x00,
0x00, 0x00, 0x00, 0x27, 0xfc, 0x80, 0x00,
0x00, 0x00, 0x00, 0x13, 0xf9, 0x00, 0x00,
0x00, 0x00, 0x00, 0x08, 0xe2, 0x00, 0x00,
0x00, 0x00, 0x00, 0x06, 0x0c, 0x00, 0x00,
0x00, 0x00, 0x00, 0x03, 0xf8, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00
};
void Lectura()
{ // This function describes what will happen with each timer tick
int adcVal = analogRead(PIN_LM35);
float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
float temperatura=milliVolt/10;
Blynk.virtualWrite(V2, temperatura);
display.clearDisplay();
int ancho_img = 50;
int alto_img = 64;
display.drawBitmap(0, 0, image_data_temp, ancho_img, alto_img, SSD1306_INVERSE);
display.display();
display.setCursor(55, 17);
display.print(temperatura);
display.display();
/* display.setCursor(55, 17);
display.print(temperatura);
display.display();*/
if (temperatura > 35)
{
Blynk.logEvent("alta_temperatura");
}
}
void setup() {
Serial.begin(9600);
Serial.println("Programa de control de temperatura");
// en Wokwi
Serial.println("Pulsa sobre el sensor LM35 para cambiar el valor de Tª");
display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS); // initialize the OLED
// initialize OLED display with I2C address 0x3C
if (!display.begin(SSD1306_SWITCHCAPVCC, 0x3C))
{
Serial.println(F("failed to start SSD1306 OLED"));
while (1);
}
delay(2000); // wait two seconds for initializing
display.clearDisplay(); // clear display
display.setTextSize(2); // set text size
display.setTextColor(WHITE); // set text color
display.setCursor(0, 0);
Blynk.begin(BLYNK_AUTH_TOKEN, ssid, pass);
tiempo.setInterval(1000L, Lectura); // Setting interval to send data to Blynk Cloud
pinMode(ledPin, OUTPUT); // GPIO 5 es la salida donde está el LED
}
BLYNK_WRITE(V0) // Se ejecuta cuando cambia el valor del pin virtual V0
{
if(param.asInt() == 1) // lee el valor del pin virtual y lo compara con ‘1’
digitalWrite(ledPin,HIGH); // enciende el LED
else
digitalWrite(ledPin,LOW); // apaga el LED
}
BLYNK_CONNECTED()
{
Blynk.syncVirtual(V0); // sincroniza el estado del widget
}
void loop() {
Blynk.run(); // Runs all Blynk stuff
tiempo.run(); // runs BlynkTimer
}