Week 9: Radio, WiFi & Bluetooth (IoT)

This week's assignment was to program a microcontroller to obtain and respond to information from the internet/radio, with at least one input and output. I collaborated with Felicia Liang this week to tackle the idiosyncracies of Arduino in support of a major step of our final project.

Simple WiFi Server with ESP32

We started by setting up a WiFi Server so we could control our ESP32 board over WiFi connection. We set it up so that we were able to turn an LED on/off by clicking on the links in our browser.

Firebase + ESP32 + LED

Then, we followed this tutorial to control an LED through a Firebase Server.

TURN LED ON/OFF BY SETTING VALUE OF STRING TO "ON" OR "OFF" IN FIREBASE REALTIME DATABASE.
TURN LED ON/OFF BY CLICKING THE "TURN ON" OR "TURN OFF" BUTTON.

ESP32 + Stepper Motor

For our final project, we want to create a drawing machine that injects ink into water based on certain inputs. To start on the prototype, we 3D printed a syringe holder that is attached to a stepper motor. When the stepper motor runs, the holder depresses the plunger. We followed this tutorial to connect the stepper motor to the ESP32 via an ULN2003 motor driver and powered it with an Arduino Uno that was connected to our laptop.
Arduino Code

          /*
          Rui Santos
          Complete project details at https://RandomNerdTutorials.com/esp32-stepper-motor-28byj-48-uln2003/
          
          Permission is hereby granted, free of charge, to any person obtaining a copy
          of this software and associated documentation files.
          
          The above copyright notice and this permission notice shall be included in all
          copies or substantial portions of the Software.
          
          Based on Stepper Motor Control - one revolution by Tom Igoe
        */
        
        #include "Stepper.h"
        
        const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
        
        // ULN2003 Motor Driver Pins
        #define IN1 19
        #define IN2 18
        #define IN3 10
        #define IN4 4
        
        // initialize the stepper library
        Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
        
        void setup() {
          // set the speed at 5 rpm
          myStepper.setSpeed(5);
          // initialize the serial port
          Serial.begin(115200);
        }
        
        void loop() {
          // step one revolution in one direction:
          Serial.println("clockwise");
          myStepper.step(stepsPerRevolution);
          delay(1000);
        
          // step one revolution in the other direction:
          Serial.println("counterclockwise");
          myStepper.step(-stepsPerRevolution);
          delay(1000);
        }
        

Firebase + ESP32 + Stepper Motor

Finally, we tried to connect the the ESP32 and stepper motor to Firebase so that we could control the motor over Wifi. It worked! We'll likely build on this prototype over the next few weeks and incorporate it into the "machine building" assignment and ultimately our final project.

TURN STEPPER MOTOR ON/OFF BY SETTING VALUE OF STRING TO "ON" OR "OFF" IN FIREBASE REALTIME DATABASE. NOTE THAT THE SYRINGE WASN'T DEPRESSED IN THIS VIDEO BECAUSE WE DIDN'T LINE THE GEARS UP PROPERLY.
TURN STEPPER MOTOR ON/OFF BY CLICKING THE "TURN ON" OR "TURN OFF" BUTTON. THE SYRINGE DISPENSER WORKS!

Arduino Code

          #include "WiFi.h"                                // esp32 library
          #include "FirebaseESP32.h"                      // firebase library
          #include "Stepper.h"
          
          
          #define FIREBASE_HOST "https://ps70-week-9-default-rtdb.firebaseio.com/"  // the project name address from firebase id
          #define FIREBASE_AUTH "2eCvifu0nm6sZbEiz8NMaCrHV9wRS21cMwPCOcDF"    
          #define WIFI_SSID "MAKERSPACE"                                // input your home or public wifi name
          #define WIFI_PASSWORD "12345678"                            // password of wifi ssid
          
          // ULN2003 Motor Driver Pins
          #define IN1 19
          #define IN2 18
          #define IN3 10
          #define IN4 4
          
          
          String fireString = "";                                          received from firebase
          const int stepsPerRevolution = 2048;  // change this to fit the number of steps per revolution
          
          // initialize the stepper library
          Stepper myStepper(stepsPerRevolution, IN1, IN3, IN2, IN4);
          
          //Define FirebaseESP32 data object
          FirebaseData firebaseData;
          
          void setup() {
            // set the speed at 5 rpm
            myStepper.setSpeed(15);
            // initialize the serial port
            Serial.begin(115200);
            WiFi.begin(WIFI_SSID, WIFI_PASSWORD);                          // try to connect with wifi
          
            Serial.print("Connecting to ");
            Serial.print(WIFI_SSID);
            while (WiFi.status() != WL_CONNECTED) {
              Serial.print(".");
              delay(500);
            }
          
            Serial.println();
            Serial.print("Connected to ");
            Serial.println(WIFI_SSID);
            Serial.print("IP Address is : ");
            Serial.println(WiFi.localIP());                                // print local IP address
            Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH);                  // connect to firebase
            Firebase.reconnectWiFi(true);
            Firebase.set(firebaseData, "/STEPPER", "OFF");              // set initial string of "OFF"
          
          }
          
          void loop() {
          
            Firebase.get(firebaseData, "/STEPPER");                     // get stepper status input from firebase
            fireString = firebaseData.stringData();                        // change to e.g. intData() or boolData(
          
            if (fireString == "ON") {                    // compare the input of stepper status received from firebase
              Serial.println("Stepper Turned ON");
              myStepper.step(-stepsPerRevolution);              // make output stepper
              }
          
            else if (fireString == "OFF") {              // compare the input of stepper status received from firebase
              Serial.println("Stepper Turned OFF");
              myStepper.step(0); 
                }
          
          
          }