• 1
    You will need...

    In this tutorial, we will write a DroidScript app to control a relay module using the ESP32 board over WiFi.

    You will need:

    · ESP32 board (we used ESP32_core_board_v2)

    · An Android device

    · The DroidScript app

    · Arduino IDE

    · Relay module

    · Micro USB Cable

    · Female to Female Jumper (Dupont) Connectors x3

    · WiFi

  • 2
    Step 1: Setting Up the Arduino IDE

    Step 1 – The first thing we will need to do is add the ESP32

    board to the Arduino IDE. To do this, follow this guide:

    https://randomnerdtutorials.com/installing-the-esp...

    We then need to add the library we are going to use to run a webserver we can communicate with on the ESP32.

    To add the ESPAsyncWebServer library to the IDE go to: https://github.com/me-no-dev/ESPAsyncWebServer

    and download the library as a zip file. Go back to the Arduino IDE and to Sketch, Include Library, Add .ZIP Library then find the zip file you just downloaded and click open.

    This library starts an asynchronous web server on the Arduino that we can connect to over the WIFI network to tell the ESP32 to do things or read data back from the device.

  • 3
    Step 2: Arduino Webserver Code

    Add the following code in the Arduino IDE and fill in your

    own WIFI SSID and Password. Once complete, go to Tools, Board, and select ESP32 Dev Module (or whichever your board is). You can now plug your board into your PC using the micro USB cable and you should see it appear under Tools, Port. Select your board and port here and then upload the code.

    #include "WiFi.h"
    #include "ESPAsyncWebServer.h"
     
    const char* ssid = "myRouter";  //replace
    const char* password =  "myPassword"; //replace
    
    AsyncWebServer server(80);
    
    int relayPin = 23;
     
    void setup(){
    
      pinMode(relayPin, OUTPUT);
      digitalWrite(relayPin, HIGH);
      
      Serial.begin(115200);
     
      WiFi.begin(ssid, password);
     
      while (WiFi.status() != WL_CONNECTED) {
        delay(1000);
        Serial.println("Connecting to WiFi..");
      }
     
      Serial.println(WiFi.localIP());
     
      server.on("/hello", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", "Hello World");
      });
    
      server.on("/relay/off", HTTP_GET   , [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", "ok");
        digitalWrite(relayPin, HIGH);
      });
       server.on("/relay/on", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain","ok");
        digitalWrite(relayPin, LOW);
      });
      
      server.on("/relay/toggle", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain","ok");
        digitalWrite(relayPin, !digitalRead(relayPin));
      });
      
      server.on("/relay", HTTP_GET, [](AsyncWebServerRequest *request){
        request->send(200, "text/plain", String(digitalRead(relayPin)));
      });
      
      server.begin();
    }
     
    void loop(){}