modified: src/main.cpp
This commit is contained in:
parent
7685d5260e
commit
9d94100877
46
src/main.cpp
46
src/main.cpp
@ -133,9 +133,42 @@ void setup() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void executeCommand(String user_command, WiFiClient* client, int Mode) { // Mode 0: Serial, Mode 1: TCP
|
||||||
|
if (user_command == "temp.read") {
|
||||||
|
float temperature = dht22.readTemperature();
|
||||||
|
if (Mode == 0){
|
||||||
|
Serial.printf("Temperature: %.2f °C\n", temperature);
|
||||||
|
}
|
||||||
|
if (Mode == 1 && client && *client) {
|
||||||
|
client->println(temperature);
|
||||||
|
}
|
||||||
|
user_command = ""; // Clear command after processing
|
||||||
|
}
|
||||||
|
if (user_command == "hum.read") {
|
||||||
|
float humidity = dht22.readHumidity();
|
||||||
|
if (Mode == 0){
|
||||||
|
Serial.printf("Humidity: %.2f %%\n", humidity);
|
||||||
|
}
|
||||||
|
if (Mode == 1 && client && *client) {
|
||||||
|
client->println(humidity);
|
||||||
|
}
|
||||||
|
user_command = ""; // Clear command after processing
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void loop() {
|
void loop() {
|
||||||
String user_command;
|
String user_command;
|
||||||
WiFiClient client; // Declare client at the start
|
WiFiClient client; // Declare client at the start
|
||||||
|
|
||||||
|
// Serial command handling
|
||||||
|
if (Serial.available()) {
|
||||||
|
String user_command = Serial.readStringUntil('\n'); // Read serial command
|
||||||
|
user_command.trim(); // Remove whitespace
|
||||||
|
Serial.printf("Received command: %s\n", user_command.c_str()); // Print received command
|
||||||
|
executeCommand(user_command, nullptr, 0); // Pass nullptr for Serial mode
|
||||||
|
}
|
||||||
|
|
||||||
|
// TCP command handling
|
||||||
if(server.hasClient()) { // Check if there is a client connected
|
if(server.hasClient()) { // Check if there is a client connected
|
||||||
Serial.println("A new client connected to web server.");
|
Serial.println("A new client connected to web server.");
|
||||||
server.setNoDelay(true);
|
server.setNoDelay(true);
|
||||||
@ -151,21 +184,10 @@ void loop() {
|
|||||||
currentLine.trim(); // Remove whitespace
|
currentLine.trim(); // Remove whitespace
|
||||||
user_command = currentLine;
|
user_command = currentLine;
|
||||||
Serial.printf("Received command: %s\n", user_command.c_str());
|
Serial.printf("Received command: %s\n", user_command.c_str());
|
||||||
|
executeCommand(user_command, &client, 1); // Pass pointer for TCP mode
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (user_command == "temp.read") {
|
|
||||||
float temperature = dht22.readTemperature();
|
|
||||||
client.println(temperature);
|
|
||||||
Serial.printf("Temperature: %.2f °C\n", temperature);
|
|
||||||
user_command = ""; // Clear command after processing
|
|
||||||
}
|
|
||||||
if (user_command == "hum.read") {
|
|
||||||
float humidity = dht22.readHumidity();
|
|
||||||
client.println(humidity);
|
|
||||||
Serial.printf("Humidity: %.2f %%\n", humidity);
|
|
||||||
user_command = ""; // Clear command after processing
|
|
||||||
}
|
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user