So I know I should be able to figure this out but for some stupid reason I can’t see the problem.
This is a baby step approach but what I am attempting to do is turn on a relay with temperature change. I have a working program to display the temperature on a webpage, I used GPIO 2 to turn on an led ,later will be replaced with a relay. So to make sure it wasn’t a wiring issue I wrote a simple flashing led and it does what is expected. I then built the circuit to display the temp and it works. I used the Led in place of the relay and programmed a < than statement to fire GPIO 2.
I cannot get the led to fire when I change the temp from ambient to a higher temp, I know this should be simple but I am not seeing the problem. Code compiles ,I have mover the if statement around in the program and still it don’t work. Any help would be appreciated
Start your co
#include <ESP8266WiFi.h>
#include <math.h>
int led=2;
// http://arduino.esp8266.com/stable/package_esp8266com_index.json
unsigned int Rs = 150000;
double Vcc = 3.3;
// WiFi parameters
const char* ssid = “*****”;
const char* password = “************”;
// Pin
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
pinMode (led,OUTPUT);
// Connect to WiFi network
WiFi.mode(WIFI_STA);
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
// Start the server
server.begin();
Serial.println(“Server started”);
// Print the IP address
Serial.println(WiFi.localIP());
}
void loop() {
delay(1000);
Serial.println(Thermister(AnalogRead()));
float temp = Thermister(AnalogRead());
WiFiClient client = server.available();
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response
client.println(“Refresh: 3”); // refresh the page automatically every 5 sec
client.println();
client.println(“<!DOCTYPE html>”);
client.println(“<html xmlns=’http://www.w3.org/1999/xhtml’>”);
client.println(“<head>\n<meta charset=’UTF-8′>”);
client.println(“<title>Fireplaceh Wifi Temp Sensor</title>”);
client.println(“<link href=’https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css’ rel=’stylesheet’>”);
client.println(“</head>\n<body>”);
client.println(“<div class=’container’>”);
client.println(“<div class=’panel panel-default’ margin:15px>”);
client.println(“<div class=’panel-heading’>”);
client.println(“<H2>Fireplace Wifi Temp Sensor</H2>”);
client.println(“<div class=’panel-body’ style=’background-color: powderblue’>”);
client.println(“<pre>”);
client.print(“Temperature (°F) : “);
client.println(temp, 2);
client.println(“</pre>”);
client.println(“</div>”);
client.println(“</div>”);
client.println(“</div>”);
client.print(“</body>\n</html>”);
}
int AnalogRead() {
int val = 0;
for(int i = 0; i < 20; i++) {
val += analogRead(A0);
delay(1);
}
val = val / 20;
return val;
}
double Thermister(int val) {
double V_NTC = (double)val / 1024;
double R_NTC = (Rs * V_NTC) / (Vcc – V_NTC);
R_NTC = log(R_NTC);
double Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * R_NTC * R_NTC ))* R_NTC );
//Temp = Temp – 273.15;
Temp = Temp – 303.01;
Temp=(Temp*9.0)/5.0+32;
return Temp;
if (Temp<80){
digitalWrite(led,HIGH);
}else{
digitalWrite (led,LOW);
}
Serial.print (Temp);
}
de here
.
Hi.
In your function called Thermister(), you get and calculate the temperature.
Then, you have this line:
return Temp;
When the code gets to that line, the function returns the temperature value, and ignores the rest of the lines that follow. The return statement gets you out of the function. So, the next lines that control the LED will never run.
The return statement should always be used at the end of the function.
I hope this makes sense.
Regards,
Sara
Thank you , and yes It makes sense, unfortunately I was not realizing this. In the past you suggested that I try by reading code one line at a time to help better understand the codes as written, great advice but still learning.
Your suggestion works perfectly, now to add to this project. Would really like to eventually figure out a way to control fan speed proportional to temperature but that is a ways off. Also want to make sure I don’t compromise any safety concerns in the event some one would like to try this .I guess that’s where I would put a disclaimer statement for this , again thanks.
update; I think I did it wrong ,more likely less than 100 hrs, still not sure of the math but the current draw is correct
Question on math, so I put a meter in series with the +3.3 and the circuit and now read that the circuit while connected to WiFi is consuming 20.6uA or .02mA which is .00002 A. So (A)*(H)*(!000)=mAh , that means if I use a 2000 mAh battery powering the Wemos D1 at 5v and the circuit is using 3.3v that it could last 100000 hrs. Seems wrong ,just wondering if you could clear this up, either way I am going to test it and I am certain that readings will change whenI put a relay into the circuit.
Thanks
Hi.
The math seems correct.
What is weird is the current consumption. Are you sure you’re measuring on the right range?
Regards,
Sara
Fluke 87 in series with the 3.3 volts to project.What should it be ? It can’t possibly be 100000 hrs so I must be doing something wrong. Also, at what voltage drop of the battery pack will drop out the Wemos? The fact that I am using a 2000mAh battery pack really doesn’t mean that I will fully deplete it does it?
Hi.
I’m not sure, but I don’t think so. I think when it goes below some value, it stops working.
The best way to know is to use it on your project and monitor how it behaves.
Regards,
Sara
Exactly what I am currently doing, just to figure out battery life. Nothing connected other than the thermistor and connected to web. When this gets complete I will then remove the built in led, that eats power or use a sleep mode if I can figure it out.
Running 30 hrs so far, reading 4.57 volts across 5v supply. When I put the relay circuit together I am certain to draw more as it turns on the fan circuit but baby steps now.
Thanks for your support.
I believe the minimum voltage is 2.58 so I have to try to determine how long the power bank run before it gets to that level.
At some point in time it will drop to that level but I don’t know how to capture that time
I believe the minimum voltage is 2.58 so I have to try to determine how long the power bank run before it gets to that level.
At some point in time it will drop to that level but I don’t know how to capture that time
Hi.
To know exactly how much time the batteries last, you can save the time to a file in the filesystem in regular periods of time, for example. It will be saved even when the ESP loses power. Then, you can power it up and upload a sketch that reads the file and check what was the last time.
I never tried to do this. So, at the moment I don’t have any examples for that specific matter. But, I can show you examples to create, read and write files to the filesystem.
Regards,
Sara
My WiFi Eero6 showed the last time connected was 1/24@ 5:35 am , I started it on 1/22 12:30 so approximately 41 hrs. Not bad for a single cell 2000 mAh power pack.
I considered removing the onboard led to see how much difference in battery life would be . I still may do that but I like that I can see if it is communicating, that has valve to me also.
I also have a larger power bank 10000 mAh but it only works as a back up power source, left connected to the Wemos it seems to shut down, not sure why yet.
Thanks , would be interested in file system at some point .
My WiFi Eero6 showed the last time connected was 1/24@ 5:35 am , I started it on 1/22 12:30 so approximately 41 hrs. Not bad for a single cell 2000 mAh power pack.
I considered removing the onboard led to see how much difference in battery life would be . I still may do that but I like that I can see if it is communicating, that has valve to me also.
I also have a larger power bank 10000 mAh but it only works as a back up power source, left connected to the Wemos it seems to shut down, not sure why yet.
Thanks , would be interested in file system at some point .
Hi.
Some power banks go into deep sleep mode if power consumption is very low. And then, you need to reset them manually. However, I don’t know if that’s the case with your power bank.
Regards,
Sara
That is the case😂now if I could only get the Wemos to do that I would be in good shape. Tried a few things, haven’t gotten it to do that yet.
So I have the sleep mode working on a ESP8266-12E Lolin , problem with getting it to display on web page.
So here is the senario;
If I put the sleep code in <setup> then I get “Serial does not name type”
if I place it in <Loop> after Temp it reads on web page but won’t sleep
if I place it <Loop> after Temp,will not display on web page but does go to sleep.
[code]
#include <ESP8266WiFi.h>
#include <math.h>
// http://arduino.esp8266.com/stable/package_esp8266com_index.json
unsigned int Rs = 150000;
double Vcc = 3.3;
// WiFi parameters
const char* ssid = “*****”;
const char* password = “************”;
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
Serial.println();
// Connect to WiFi network
WiFi.mode(WIFI_STA);
Serial.println();
Serial.println();
Serial.print(“Connecting to “);
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(“.”);
}
Serial.println(“”);
Serial.println(“WiFi connected”);
// Start the server
server.begin();
Serial.println(“Server started”);
// Print the IP address
Serial.println(WiFi.localIP());
WiFi.begin(ssid, password);
Serial1.println(“”);
delay(1000);
Serial.println(Thermister(AnalogRead()));
float temp = Thermister(AnalogRead());
}
void loop() {
delay(1000);
Serial.println(Thermister(AnalogRead()));
float temp = Thermister(AnalogRead());
WiFiClient client = server.available();
client.println(“HTTP/1.1 200 OK”);
client.println(“Content-Type: text/html”);
client.println(“Connection: close”); // the connection will be closed after completion of the response
client.println(“Refresh: 3”); // refresh the page automatically every 5 sec
client.println();
client.println(“<!DOCTYPE html>”);
client.println(“<html xmlns=’http://www.w3.org/1999/xhtml’>”);
client.println(“<head>\n<meta charset=’UTF-8′>”);
client.println(“<title>Fireplac Wifi Temp Sensor</title>”);
client.println(“<link href=’https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css’ rel=’stylesheet’>”);
client.println(“</head>\n<body>”);
client.println(“<div class=’container’>”);
client.println(“<div class=’panel panel-default’ margin:15px>”);
client.println(“<div class=’panel-heading’>”);
client.println(“<H2>Fireplac Wifi Temp Sensor</H2>”);
client.println(“<div class=’panel-body’ style=’background-color: powderblue’>”);
client.println(“<pre>”);
client.print(“Temperature (° F) : “);
client.println(temp, 2);
client.println(“</pre>”);
client.println(“</div>”);
client.println(“</div>”);
client.println(“</div>”);
client.print(“</body>\n</html>”);
}
int AnalogRead() {
int val = 0;
for (int i = 0; i < 20; i++) {
val += analogRead(A0);
delay(1);
}
val = val / 20;
return val;
}
double Thermister(int val) {
double V_NTC = (double)val / 1024;
double R_NTC = (Rs * V_NTC) / (Vcc – V_NTC);
R_NTC = log(R_NTC);
double Temp = 1 / (0.001129148 + (0.000234125 + (0.0000000876741 * R_NTC * R_NTC )) * R_NTC );
//Temp = Temp – 273.15;
Temp = Temp – 303.01;
Temp = (Temp * 9.0) / 5.0 + 32;
// Deep sleep mode for 30 seconds, the ESP8266 wakes up by itself when GPIO 16 (D0 in NodeMCU board) is connected to the RESET pin
Serial.println(“I’m awake, but I’m going into deep sleep mode for 30 seconds”);
ESP.deepSleep(30e6);
return Temp;
}
[/code]
I wrote a very long reply, but I lost it all because RNT wanted me to login, even though I had the ‘remember me’ checked. I can’t really rewrite the long reply all over again.
In short, it will be the relay that will consume the most depending how often it is on. You may want to look into solid state relays. But then it can be inappropriate for your application. Since you have power at the fan, just use a 5V adapter, and do away with batteries which have to be recharged.
Bernie , currently I am getting 4 days on my battery so I am good for that issue and yes a relay coil would put a strain on the battery life. I would use N/C contact and pulse for a few sec then sleep for 2 days .
Hi Larry.
Here are a few things that will make it easier to understand what might be going one:
- your code runs linearly. So, when the code reaches the deep sleep command, it goes to sleep and doesn’t proceed to the rest of the code;
- You can’t have a web server running while the ESP is in deep sleep. When the ESP is in deep sleep mode, the Wi-Fi is turned off. You can’t access the web server.
- When the ESP wakes up, it runs the code from the start. It doesn’t go to where it left before.
I hope this helps.
Regards,
Sara