Wednesday, September 14, 2022

How to Control Water Flow with Arduino IoT Cloud and a Solenoid Parts 1 and 2

In this two part series we look at how to control a Solenoid using an ESP32 board and the Arduino IoT Cloud. In part one we focus on what a solenoid is and the hardware needed to drive a solenoid open or closed. In part 2 we focus on setting up the Arduino IoT Cloud control.

\



Link to tutorial on setting up device on Arduino IoT Cloud: https://docs.arduino.cc/arduino-cloud/getting-started/esp-32-cloud


//**************Arduino Code from Tutorial*********************
#include "thingProperties.h"

#define SOLENOID_PIN 21

void setup() {
  pinMode(SOLENOID_PIN,OUTPUT);
  digitalWrite(SOLENOID_PIN,LOW);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() {
  ArduinoCloud.update();
  
  if(water_Scheduler.isActive() || solenoidState) {
    digitalWrite(SOLENOID_PIN, HIGH);
  }
  else {
    digitalWrite(SOLENOID_PIN, LOW);
  } 
}



/*
  Since SolenoidState is READ_WRITE variable, onSolenoidStateChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onSolenoidStateChange()  {
  // Add your code here to act upon SolenoidState change
  /*
  if (solenoidState) {
    digitalWrite(SOLENOID_PIN, HIGH);
  }
  else {
    digitalWrite(SOLENOID_PIN, LOW);
  } 
  */
}

/*
  Since WaterScheduler is READ_WRITE variable, onWaterSchedulerChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onWaterSchedulerChange()  {
  // Add your code here to act upon WaterScheduler change
}