I have copy the script from the Esp 8266 to ESP32. All the things are working. Water lever indicator are also working. But i am facing one difficulty. My motor did not start up. Please some one help me. Ultrasonic sensor is working fine only motor did not start
//create new adafruit IO feed and name it as 'waterLevel'
//uncomment these two lines and write your wifi ssid/password
const char* ssid = "Home Cable";
const char* password = "Core44987";
//also uncomment and write here your own adafruit IO username and key.
#define AIO_USERNAME "yyyyyy"//replace it with your username
#define AIO_KEY "6165ec27055aef07d71787f"//replace it with your key
//uncomment these lines as well, write radius of tank and total height in units of inches,
#define RADIUS 24
#define MAX_HEIGHT 67.2
//modify this line if you have to connect relay to some pin other than D4
#define MOTOR_CONTROL_PIN 19
//Connect trig of ultrasonic sensor at D1 and echo at D2, modify this line otherwise
UltraSonicDistanceSensor distanceSensor(22,23); //D1 trig, D2=echo
//these are default water level limits, these values will be applied until on reset until user sends new limits from webpage.
int waterLevelLowerThreshold=100;
int waterLevelUpperThreshold=1850;
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
float volume;
float liters;
// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;
// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);
Adafruit_MQTT_Publish waterLevel = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/waterLevel");
/*************************** Sketch Code ************************************/
// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();
String inputString = ""; // a string to hold incoming data
String dataToSend="";
int waterLevelDownCount=0,waterLevelUpCount=0;
WebServer server(80);
void handleRoot() {
server.send_P(200, "text/html;charset=UTF-8", index_html);
}
void handleLevelRequest(){
server.send(200,"text",String(liters));
}
void handleNotFound(){
String message = "File Not Found\n\n";
server.send(404, "text/plain", message);
}
void handleStatus()
{
if(digitalRead(MOTOR_CONTROL_PIN)==0)//MOTOR ON
server.send(200, "text/plain","on");
else server.send(200, "text/plain","off");
}
void handleRangeSetting(){
waterLevelLowerThreshold=(server.arg(0)).toInt();
waterLevelUpperThreshold=(server.arg(1)).toInt();
Serial.print(waterLevelLowerThreshold);
Serial.print(":");
Serial.println(waterLevelUpperThreshold);
server.send(200, "text/plain", "");
}
void measure_Volume()
{
float heightInch=0.393701*distanceSensor.measureDistanceCm();
Serial.println(heightInch);
if(heightInch>MAX_HEIGHT)
heightInch=MAX_HEIGHT;
if(heightInch<0)
heightInch=0;
volume=3.14*RADIUS*RADIUS*(MAX_HEIGHT-heightInch);//MAX_HEIGHT-distance will give actual height,
liters=volume*0.0164 ;
Serial.println(liters);
if(liters<=waterLevelLowerThreshold)
waterLevelDownCount++;
else waterLevelDownCount=0;
if(liters>=waterLevelUpperThreshold)
waterLevelUpCount++;
else waterLevelUpCount=0;
waterLevel.publish(liters);
if(waterLevelDownCount==3)
{//TURN ON RELAY
Serial.println("motor turned on");
digitalWrite(MOTOR_CONTROL_PIN,LOW);//Relay is active LOW
}
if(waterLevelUpCount==3)
{//TURN OFF RELAY
Serial.println("motor turned off");
digitalWrite(MOTOR_CONTROL_PIN,HIGH);//Relay is active LOW
}
}
void runPeriodicFunc()
{
static const unsigned long REFRESH_INTERVAL1 = 2100; // 2.1sec
static unsigned long lastRefreshTime1 = 0;
if(millis() - lastRefreshTime1 >= REFRESH_INTERVAL1)
{
measure_Volume();
lastRefreshTime1 = millis();
}
}
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
Serial.print("Connecting to MQTT... ");
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
Serial.println(mqtt.connectErrorString(ret));
Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
}
Serial.println("MQTT Connected!");
}
void setup(void){
Serial.begin(115200);
delay(100);
pinMode(MOTOR_CONTROL_PIN, OUTPUT);
WiFi.begin(ssid, password);
Serial.println("");
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print("IP address:");
Serial.println(WiFi.localIP());
server.on("/", handleRoot);
server.on("/level",handleLevelRequest);
server.on("/configRange",handleRangeSetting);
server.on("/motor_status",handleStatus);
server.onNotFound(handleNotFound);
server.begin();
Serial.println("HTTP server started");
}
void loop(void){
runPeriodicFunc();
MQTT_connect();
server.handleClient();
}