I can fix code that doesn't work, but the above lines works to turn a switch on or off, but when in an IF/ELSE I can't get it to do both. Thanks
This is a simple project: the circuit reads a charge based on moisture--if moist reading is > 23, then switch should go off. If reading is < 23 then switch goes on.
The logic is working fine as seen via Serial.print, but I can't get it to turn off after it is on. When I try to reverse the logic then I can't get it to turn on after it is off?
Another way to say this is that
client.println("GET /relay/0?turn=on HTTP/1.0");
turns on at the correct time but when the moisture changes and the logic goes to:
client.println("GET /relay/0?turn=off HTTP/1.0");
the switch seems to ignore the second command.
see full sketch here:
#include <SPI.h> // wifi
#include <WiFiNINA.h> // wifi
int moistPin = A0; // bubble sensor variables A0 ? or 0?
int moistVal = 0;
int tooDry = 23; //set low parameter for bubbles
char ssid[] = "office1";
char pass[] = "";
int keyIndex = 0;
int status = WL_IDLE_STATUS;
char server[] = "192.168.5.240";
WiFiClient client;
void setup() {
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(100);
}
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
moistVal = analogRead(moistPin);
int percent = 2.718282 * 2.718282 * (.008985 * moistVal + 0.207762); //calculate percent for probes about 1 - 1.5 inches apart
delay (100);
Serial.print(moistVal,3);
Serial.print(percent,3);
Serial.println("% Bubbles ");
client.println("GET /relay/0?turn=off HTTP/1.0");
//send code from here
if (percent > tooDry) {
client.println("GET /relay/0?turn=off HTTP/1.0");
delay(100);
}
else {
if (client.connect(server, 80)) {
Serial.println("connected 2");
client.println("GET /relay/0?turn=on HTTP/1.0");
delay(1000);
client.println();
}
}
//
// end bubble sensor
if (client.connected()) {
client.stop();
}
delay(0);
}
High in loop You start by doing/trying a "turn off". Then You measure and act upon the result. Shouldn't that be commented out? Communicate when You what message to send.
As @ J-M-LJackson says, You close down the session/messaging at the end of loop but You don't open it up again.
Thank you, all good comments/questions.
This project was combined from 3 others, hence the mixed up coding. But it works well. It wirelessly connects sensors to control a bubble hose system / greenhouse.
My request / post had to do with my puzzle over why the GET line works on some lines and not others, even though the code flow test as correct.
Not sure how you mean "consistent," as it is now, the logic flows correctly between the two IF / ELSE options based on the moistVal readings.
The problem is that the GET line results are inconsistent even though they are using the same syntax.
One other clue may be what happens when I use a browser to directly send either URL command (ON or OFF) to the switch device: http://192.168.5.240/relay/0?turn=off
or http://192.168.5.240/relay/0?turn=off
These URLs return the following:
{"ison":false,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"http"}
or
{"ison":true,"has_timer":false,"timer_started":0,"timer_duration":0,"timer_remaining":0,"source":"http"}
I don't know what's you definition of "correctly"... it does compile for sure, but it is not logical...
I'm talking about the client.connect() call
if I simplify your loop, it reads something like this
void loop() {
int percent = ..;
client.println("GET /relay/0?turn=off HTTP/1.0");
if (percent > tooDry) {
client.println("GET /relay/0?turn=off HTTP/1.0");
} else {
if (client.connect(server, 80)) {
client.println("GET /relay/0?turn=on HTTP/1.0");
}
}
if (client.connected()) client.stop();
}
the first time you enter the loop, you send a "GET /relay/0?turn=off HTTP/1.0" but you did not even connect to the server.
then if percent is > tooDry you send again "GET /relay/0?turn=off HTTP/1.0" but you still did not connect to the server
this stays so until percent becomes <= tooDry. Then you actually connect to the server and send GET /relay/0?turn=on HTTP/1.0" and right after (because you are connected now) you close the connection.
➜ you only successfully send ON (because you connect to the server), you can never send OFF (because you are never connected to the server when you send the command).
Yes, totally! And thanks! I got it working yesterday just by trying a few options. But now I'm curious to see my new code in regard to your corrections?
Thanks again
#include <WiFiNINA.h>
#define sensorPin A5
char ssid[] = "office";
char pass[] = "";
int status = WL_IDLE_STATUS;
char server[] = "192.168.5.240";
WiFiClient client;
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(9600);
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid);
status = WiFi.begin(ssid, pass);
delay(10000);
}
digitalWrite(LED_BUILTIN, HIGH);
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
int reading = analogRead(sensorPin);
//float voltage = reading * 5.0;
//voltage /= 1024.0;
//float temperatureC = (voltage - 0.5) * 100 ;
//float temperatureF = (temperatureC * 9.0 / 5.0) + 32.0;
delay (1000);
Serial.print("Temp F: ");
Serial.println(reading);//Serial.println(temperatureF);
//SEND CODE FROM HERE ---
if (reading > 600) //if (temperatureF > 80) {
if (client.connect(server, 80)) {
Serial.println("connected 1");
client.println("GET /relay/0?turn=off HTTP/1.0");
Serial.println(reading);
client.println();
}
if (reading < 600) { //} else if (temperatureF < 80) {
if (client.connect(server, 80)) {
Serial.println("connected 2");
client.println("GET /relay/0?turn=on HTTP/1.0");
Serial.println(reading);
client.println();
//}
}
} else {
Serial.println("problem");
}
// ---- TO HERE
if (client.connected()) {
client.stop();
}
delay(1000);
}