@@ -61,6 +61,8 @@ void controlActuators(float temperature, float airHumidity);
6161
6262void controlSoilHum (int soilMoisture);
6363
64+ void updateActuatorState (const char * actuatorName, const char * variableLabel, int pin, String* currentState, String* lastState, const char * strMessage);
65+
6466void callback (char * topic, byte* payload, unsigned int length);
6567
6668void setup () {
@@ -210,47 +212,46 @@ void controlSoilHum(int soilMoisture) {
210212}
211213
212214void callback (char * topic, byte* payload, unsigned int length) {
215+ // Build the message string from the payload bytes.
213216 String message;
214- for (int i = 0 ; i < length; i++) {
217+ for (unsigned int i = 0 ; i < length; i++) {
215218 message += (char )payload[i];
216219 }
217220 message.trim ();
218221 const char * strMessage = message.c_str ();
219- printf (" Mensaje recibido en el topic %s: %s\n " , topic, strMessage);
220- if (String (topic).indexOf (VARIABLE_LABEL_FAN)) {
221- const uint8_t fanValue = atoi (strMessage);
222- printf (" Valor del ventilador: %d\n " , fanValue);
223- if (fanValue == 1 ) {
224- fanCurrentState = " ON" ;
225- Serial.println (" Ventilador encendido remotamente." );
226- } else if (fanValue == 0 ) {
227- fanLastValue = " OFF" ;
228- Serial.println (" Ventilador apagado remotamente." );
229- }
230-
231- if (fanCurrentState != fanLastValue) {
232- const float value = fanCurrentState == " ON" ? HIGH : LOW;
233- digitalWrite (FAN_PIN, value);
234- ubidots.add (VARIABLE_LABEL_FAN, value);
235- ubidots.publish (DEVICE_LABEL);
236- }
222+
223+ // Print received topic and message for debugging.
224+ printf (" Message received on topic %s: %s\n " , topic, strMessage);
225+
226+ // Dispatch to the appropriate handler based on the topic.
227+ String topicStr (topic);
228+ if (topicStr.indexOf (VARIABLE_LABEL_FAN) != -1 ) {
229+ updateActuatorState (" Fan" , VARIABLE_LABEL_FAN, FAN_PIN, &fanCurrentState, &fanLastValue, strMessage);
230+ } else if (topicStr.indexOf (VARIABLE_LABEL_WATER_PUMP) != -1 ) {
231+ updateActuatorState (" Water Pump" , VARIABLE_LABEL_WATER_PUMP, WATER_PUMP_PIN, &waterPumpCurrentState, &waterPumpLastValue, strMessage);
232+ }
233+ }
237234
238- } else if (String (topic).indexOf (VARIABLE_LABEL_WATER_PUMP)) {
239- const uint8_t waterPumpValue = atoi (strMessage);
240- printf (" Valor de la bomba de agua: %d\n " , waterPumpValue);
241- if (waterPumpValue == 1 ) {
242- waterPumpCurrentState = " ON" ;
243- Serial.println (" Bomba de agua encendida remotamente." );
244- } else if (waterPumpValue == 0 ) {
245- waterPumpLastValue = " OFF" ;
246- Serial.println (" Bomba de agua apagada remotamente." );
247- }
235+ void updateActuatorState (const char * actuatorName, const char * variableLabel, int pin, String* currentState, String* lastState, const char * strMessage) {
236+ // Convert the received message to an integer value.
237+ const uint8_t actuatorValue = atoi (strMessage);
238+ printf (" %s value received: %d\n " , actuatorName, actuatorValue);
239+
240+ // Set the actuator's current state based on the received value.
241+ if (actuatorValue == 1 ) {
242+ *currentState = " ON" ;
243+ Serial.printf (" %s turned ON remotely.\n " , actuatorName);
244+ } else if (actuatorValue == 0 ) {
245+ *currentState = " OFF" ;
246+ Serial.printf (" %s turned OFF remotely.\n " , actuatorName);
247+ }
248248
249- if (waterPumpCurrentState != waterPumpLastValue) {
250- const float value = waterPumpCurrentState == " ON" ? HIGH : LOW;
251- digitalWrite (WATER_PUMP_PIN, value);
252- ubidots.add (VARIABLE_LABEL_WATER_PUMP, value);
253- ubidots.publish (DEVICE_LABEL);
254- }
249+ // Update the actuator only if there is a change in state.
250+ if (*currentState != *lastState) {
251+ const float digitalValue = (*currentState == " ON" ) ? HIGH : LOW;
252+ digitalWrite (pin, digitalValue);
253+ ubidots.add (variableLabel, digitalValue);
254+ ubidots.publish (DEVICE_LABEL);
255+ *lastState = *currentState;
255256 }
256257}
0 commit comments