Hey folks, I'm working on a project which has esp8266, dht11 and a buzzer. I'm also using wifiManager library.
//Default values of tempMin and tempMax
char tempMin[3] = "18";
char tempMax[3] = "30";
float f_tempMin;
float f_tempMax;
void setup(){
//Some code here
//I'm getting tempMin and tempMax using wifiManager's custom parameter function. I can successfuly get those values.
//Here I convert char[] to float with using atof() function.
f_tempMin = atof(tempMin);
f_tempMax = atof(tempMax);
}
void loop(){
t = DHT.getCelsius();
Serial.println(t);
h = DHT.getHumidity();
Serial.println(h);
if (t < f_tempMin || t > f_tempMax)
{
Serial.println("Buzzer Öttü");
for (int i = 0; i < 1; i++) {
digitalWrite(buzzer, HIGH);
Serial.println("BUZZ");
delay(2000);
digitalWrite(buzzer, LOW);
delay(1000);
}
}
//Here is more code and esp goes deepSleep
delay(250);
ESP.deepSleep(2 * 60 * 1000 * 1000);
}
I tried to explain briefly my code. My problem is in the first lifecycle I can get the values of f_tempMin and f_tempMax as they are entered by user. But in the second lifecycle(After esp wakes up from deep sleep) f_tempMax and f_tempMin are go back to their default values(0).
What I want to do is just like SSID and PASSWORD informations I want to get f_tempMax and f_tempMin only once use them in void loop().
Where and How should I declare those f_tempMax and f_tempMin?
//I'm getting tempMin and tempMax using wifiManager's custom parameter function. I can successfuly get those values.
SHOW US!
Where and How should I declare those f_tempMax and f_tempMin?
If they are going to be valued in setup() and used in loop(), you only have one choice as to where, and the how should be obvious from what they are to contain. Declaring them as char, and assigning float values to them wouldn't make sense.
septillion:
And what does the log from serial look like?
Here it is. I added some Serial.Println() functions to see what is happening and when is happening.
Just after line "second location of f_tempMin and f_tempMax", you can see the values.
in the first cycle they are 20.00 and 25.00 as I entered.
but in the second cycle they become 0.00 and 0.00
I think problem occurs because in the first cycle void setup() is being executed but in the second cycle, void setup() is not being executed, only void loop() is being executed. So f_tempMin and f_tempMax is going back to their default values which is zero.
If it is my case, how can I solve this?
mounting FS...
mounted file system
reading config file
opened config file
{"tempMin":"20","tempMax":"25"}
parsed json
*WM: Adding parameter
*WM: tempMin
*WM: Adding parameter
*WM: tempMax
*WM:
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result:
*WM: 0
*WM: SET AP STA
*WM:
*WM: Configuring access point...
*WM: tempSens
*WM: AP IP address:
*WM: 192.168.4.1
*WM: HTTP server started
*WM: Request redirected to captive portal
*WM: Request redirected to captive portal
*WM: Request redirected to captive portal
*WM: Handle root
*WM: Scan done
*WM: EVOLUTIONDC
*WM: -73
*WM: Sent config page
*WM: WiFi save
*WM: Parameter
*WM: tempMin
*WM: 20
*WM: Parameter
*WM: tempMax
*WM: 25
*WM: Sent wifi save page
*WM: Connecting to new AP
*WM: Connecting as wifi client...
*WM: Connection result:
*WM: 3
Should save config
43110
saving config
{"tempMin":"20","tempMax":"25"}second location of f_tempMin and f_tempMax
20.00
25.00
43116
Requesting temperatures...
24.00
85.00
second location of f_tempMin and f_tempMax
20.00
25.00
connecting to
emoncms.org
Requesting URL:
/input/post.json?node=21&apikey=bc133c987ea418e4ebd18204bea240c0&json={temperature:24.00,humidity:85.00,vcc:3133}
HTTP/1.1 200 OK
Date: Thu, 08 Jun 2017 12:37:23 GMT
Server: Apache/2.2.22 (Ubuntu)
X-Powered-By: PHP/5.3.10-1ubuntu3.25
Content-Length: 2
Connection: close
Content-Type: application/json
ok
closing connection
DONE
48352
Going to sleep
Second Deep sleep
mounting FS...
mounted file system
reading config file
opened config file
{"tempMin":"20","tempMax":"25"}
parsed json
*WM: Adding parameter
*WM: tempMin
*WM: Adding parameter
*WM: tempMax
*WM:
*WM: AutoConnect
*WM: Connecting as wifi client...
*WM: Using last saved values, should be faster
*WM: Connection result:
*WM: 3
*WM: IP Address:
*WM: 192.168.1.11
16927
16928
Requesting temperatures...
24.00
85.00
[b]second location of f_tempMin and f_tempMax
0.00
0.00[/b]
Buzzer Öttü
BUZZ
connecting to
emoncms.org
Requesting URL:
/input/post.json?node=21&apikey=bc133c987ea418e4ebd18204bea240c0&json={temperature:24.00,humidity:85.00,vcc:3134}
closing connection
DONE
24161
Going to sleep
Second Deep sleep
You'll need to post ALL of your code. Use the Reply button, NOT the Quick Reply field. Use the Additional Options link to attach your code, if it exceeds the 9000 character limit.
It looks like your code does re-read the config file, and see the proper values for the temperature limits, after waking up. Without seeing all of your code, it is difficult to see why the data read from the config file is not actually used.
PaulS:
You'll need to post ALL of your code. Use the Reply button, NOT the Quick Reply field. Use the Additional Options link to attach your code, if it exceeds the 9000 character limit.
It looks like your code does re-read the config file, and see the proper values for the temperature limits, after waking up. Without seeing all of your code, it is difficult to see why the data read from the config file is not actually used.
I've post them you see above. Can you have look at them now?
I suspect that you need to create a boolean variable, global in scope, beenSleeping, initialized to false. Set it to true before going to sleep. In setup(), if you have !beenSleeping, set up the WiFiManager. If you have been, there is no reason to do all that stuff with the WiFiManager.
PaulS:
I suspect that you need to create a boolean variable, global in scope, beenSleeping, initialized to false. Set it to true before going to sleep. In setup(), if you have !beenSleeping, set up the WiFiManager. If you have been, there is no reason to do all that stuff with the WiFiManager.
Okay I'm gonna try this. If I understand correctly you say that there is no need use WiFiManager at every cycle. Where I'm confuse is after waking up is setup function executed again?
By the way, I tried to define f_tempMin and f_tempMax in global scope as this;
but this gave me random default value.(default but they are not zero and after every waking up they are still at their same value.) Do you think problem occurs because of WiFiManager or the way I define I define those variables?
you cannot trust that the sequence of anything outside of your functions happens sequentially, so who knows what will happen when this initialization happens?
Since you KNOW tempMin and tempMax, just initialize them:
you cannot trust that the sequence of anything outside of your functions happens sequentially, so who knows what will happen when this initialization happens?
Since you KNOW tempMin and tempMax, just initialize them:
Now I know tempMin and tempMax but as a next step I want to get them from user. So it may vary in the future. I tried many things but still couldn't figure it out.
Basicly I want user to enter tempMin and tempMax ONCE, afterwards I'm gonna need them to trig an alarm.