In this tutorial we will be going over how to use a Wia Dot One with two stepper motors to make an IoT window shade controlled by an Ultrasonic Distance Sensor
Requirements- Wia Dot One Buy yours here
- 2 Stepper Motors Buy yours here
- Ultrasonic Distance Sensor Buy yours here
- Battery Power Pack Buy yours here
- MicroUSB cable Buy yours here
- Male to male jumper cables
- Account on Wia
- Account at Wia
You will need to be registered and /or logged in to your Wia account at https://www.wia.io/.
- Set up your Dot One
You will need to have a set up Dot One and you can find the tutorial on how to do that Here.
- Set up your code project
Now you will need to create a space and then a block project on your Wia Dashboard
Creating the code Project- Now you need to set up a code project so we can code the distance sensor to create an event which triggers the shade to roll up or roll down.
- We also will be coding the stepper motors to roll the shade up and down
// This is the code for the stepper motors
#include <WiFi.h>
#include <Wia.h>
// Include the AccelStepper library:
#include <AccelStepper.h>
// Motor pin definitions:
#define motor1Pin1 16 // IN1 on the ULN2003 driver
#define motor1Pin2 17 // IN2 on the ULN2003 driver
#define motor1Pin3 21 // IN3 on the ULN2003 driver
#define motor1Pin4 22 // IN4 on the ULN2003 driver
#define motor2Pin1 23 // IN1 on the ULN2003 driver
#define motor2Pin2 19 // IN2 on the ULN2003 driver
#define motor2Pin3 18 // IN3 on the ULN2003 driver
#define motor2Pin4 26 // IN4 on the ULN2003 driver
// Define the AccelStepper interface type; 4 wire motor in half step mode:
#define MotorInterfaceType 8
// Initialize with pin sequence IN1-IN3-IN2-IN4 for using the AccelStepper library with 28BYJ-48 stepper motor:
AccelStepper stepper1 = AccelStepper(MotorInterfaceType, motor1Pin1, motor1Pin3, motor1Pin2, motor1Pin4);
AccelStepper stepper2 = AccelStepper(MotorInterfaceType, motor2Pin1, motor2Pin3, motor2Pin2, motor2Pin4);
bool toggleRoll = true;
unsigned long previousMillis = 0;
const long interval = 21000;
String currentState = "0";
String previousState = "0";
Wia wiaClient = Wia();
void setup()
{
WiFi.begin();
delay(2500); // Set the maximum steps per second:
stepper1.setMaxSpeed(1000);
stepper2.setMaxSpeed(1000);
}
void loop()
{
unsigned long currentMillis = millis();
currentState = wiaClient.getDeviceState("roller");
delay(1000);
digitalWrite(16, LOW);
digitalWrite(17, LOW);
digitalWrite(21, LOW);
digitalWrite(22, LOW);
digitalWrite(23, LOW);
digitalWrite(19, LOW);
digitalWrite(18, LOW);
digitalWrite(26, LOW);
if (currentState != previousState)
{
toggleRoll = !toggleRoll;//
while (digitalRead(pin) == HIGH)//
{//
delay(100);//
}
previousState = currentState;
previousMillis = millis();
}
if (toggleRoll)
{
while (currentMillis - previousMillis < interval)
{
currentMillis = millis();
// Set the speed of the motor in steps per second:
stepper1.setSpeed(-900);
stepper2.setSpeed(900);
// Step the motor with constant speed as set by setSpeed():
stepper1.runSpeed();
stepper2.runSpeed();
}
}
else
{
while (currentMillis - previousMillis < interval)
{
currentMillis = millis();
// Set the speed of the motor in steps per second:
stepper1.setSpeed(900);
stepper2.setSpeed(-900);
// Step the motor with constant speed as set by
setSpeed():
stepper1.runSpeed();
stepper2.runSpeed();
}
}
}
- Now that you have the motors ready we need to set up the distance sensor
// This is the code for the Distance Sensor
#include <WiFi.h>
#include <Wia.h>
const int PIR = 16;
Wia wiaClient = Wia();
void setup()
{
WiFi.begin();
delay(2500);
pinMode(PIR, INPUT);
}
void loop()
{
if (digitalRead(PIR) == HIGH)
{
wiaClient.createEvent("ToggleShade");
delay(2500);
}
}
Connecting the stepper motors- Now you will need to connect the stepper motors to their drivers and connect their drivers to your Dot One.
- You will need an external power supply because the steppers require 5V in order to operate properly
- Each motor is connected to four pins and then to 5V and ground. The five volts and ground are on the battery as you can see in figure 3 while the pins are on the Dot One as you can see in figure 2.
- I also used this power supply to provide power to the Dot One so the whole unit is not tethered to the wall.
- Lastly you must connect the motors to the driver boards as you can see there are two boards with cables coming in from the sides of the box which go to the motor.
Figure 2
Figure 3
Setting up the flow- Now that we have written the code and wired up the device we have to make our flow which will allow the Ultrasonic Sensor to initiate the shade
- To start off create a flow and drag an Event Created node, found in the Trigger tab under Wia, into the workspace.
- In the settings of the event trigger select your Dot One which is connected to the ultrasonic sensor as seen in figure 4.
Figure 4
- Now drag a List Devices node, found in the Logic tab under Wia, into the workspace.
- Next drag a Run Function node, found in the Logic tab under Wia, into the workspace and write the code seen below in the node as seen in figure 5.
- Replace the device ID text with the device id of your device.
var deviceId = 'REPLACE_WITH_DEVICE_ID';input.devices.forEach(function(device) { if (device.id === deviceId) { output.body = device.state.roller ? (parseInt(device.state.roller.value) + 1) : 1;}});
Figure 5
- Lastly drag an Update State node, found in the Logic tab under Wia, into the workspace and select your Dot One controlling the stepper motors.
- Also put your key in which is the name of the state, rollerand in value put in ${input.body} as seen in figure 6.
Figure 6
- Lastly connect all the nodes as seen in figure 7 and turn on the flow and you’re IoT Window Shade is ready to go!
Comments
Please log in or sign up to comment.