Temperature controlled relay with virtualwrite temp

Hello
Hardware :
ESP32
K-type
MAX6675
18VDC Lithium regulated to 12VDC
Tongling 12VDC relay

This project is to control a BBQ fire.

I am trying to trigger a relay to run a 12VDC fan, the fan is a four wire PWM but i have it wired up staright 12VDC as the PWM is out of my reach for now.
The relay is triggered by a k-type thermocouple wired up to a MAX7765 board.
The thermocouple also virtualwrites to my iphone blynk app (all good)

Everything is working as it should except the relay.
I can trigger the relay with a false signal to the ‘in’ wire, so the wiring and hardware should be correct.

I have measured the ESP32 ‘relay’ (4) pin with a multimeter and cannot detect anything.

Code:


[code]
#include <SPI.h>
#define BLYNK_PRINT Serial
#include "max6675.h"
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>

char auth[] = "removed";
char ssid[] = "removed";
char pass[] = "removed";

BlynkTimer timer; // Create a Timer object called "timer"!

int thermoDO = 19;
int thermoCS = 23;
int thermoCLK = 5;
int relay = 4;

MAX6675 thermocouple(thermoCLK, thermoCS, thermoDO);

void myFan()
{
  float t = thermocouple.readCelsius();
  if (t >= 40)
  {
    digitalWrite(relay, LOW);  // too hot, turn fan on
  }
  if (t <= 30)
  {
    digitalWrite(relay, HIGH);  // too cool, turn fan off
  }
  // otherwise leave fan doing whatever it was before

}

void myTimerEvent()
{

  float t = thermocouple.readCelsius();

  if (t >= 80)
    Blynk.notify("BIG HEAT - Temperature over 125C!");


  if (isnan (t))
    Blynk.notify("Failed to read thermocouple!");



  Blynk.virtualWrite(V4, thermocouple.readCelsius());
  Serial.print("C = ");
  Serial.println(thermocouple.readCelsius());


}


void setup()
{
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(1000L, myTimerEvent);
  timer.setInterval(1200L, myFan);
  pinMode (relay, OUTPUT);


}

void loop() {
  Blynk.run();
  timer.run(); // BlynkTimer is working...
}
[/code]

A few notes :

  1. Where have i gone wrong
  2. Is it good practice to create a separate void like a have for “myFan”
  3. Do i need a ground between ESP32 and relay board or will GPIO 4 send a signal without a closed circuit?
  4. I haven’t wired it up as PWM as i am not hardware savvy, if i were to wire FG (frequency feedback) and PWM to the ESP32 wouldn’t the fan run fulltime without a PWM signal?
  5. Sorry for my poor coding technique, I have the ability to learn off youtube only

Thankyou in advance if you reply.

Yes, it won’t work without it.

Your code is currently structured so that the temperature probe is being read 4 times every 1.2 seconds.

In void myTimerEvent() you are taking a reading from the probe and assigning it to the float variable t
Then, instead of saying Blynk.virtualWrite(V4, t); and Serial.println(t); you are forcing the code to re-read the sensor with Blynk.virtualWrite(V4, thermocouple.readCelsius()); and Serial.println(thermocouple.readCelsius());

If you made the float variable t a global variable (by declaring it at the top of your code in the same way that you’ve done with the integer variable relay) then it could be read once every second by void myTimerEvent() and the same reading re-used in void myFan()
in fact, you could either combine the two functions in to one, or call myFan() at the end of void myTimerEvent().

Also, your use of notifications in void myTimerEvent() is bad practice, as it will attempt to send the same notification on every execution of the function, provided either the (t >= 80) or (isnan (t)) tests are evaluated as true.
You can send a maximum of one notification every 5 seconds, and your code will exceed that in some situations.
You should make the if (isnan (t)) test the first thing that you do when a reading is taken, and do nothing further with that reading (by putting all of the other code inside that if (isnan (t)) logical test).
You should also have a flag to show that your excessive temperature notification has been sent once, and doesn’t need to be sent again. You’ll then clear that flag when the excessive temperature condition is no longer met, so subsequent occurrences will be notified.

Pete.

Thankyou Peter, i am still trying to fix this code.