I want to convert from Blynk to Arduino Cloud where i want an button for opening garage door in the app.
But i`m unsure how i can convert the code under to a code that works with Arduino iot cloud system.
The code under is working and it has worked for about two years.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
int relay = 16;
int magnet = 4;
int buzzer = 5;
int magnetValue;
int buttonPin = 13;
int ledState = 0;
int ButtonState;
bool Flag_buttonPressed = false; // Sets the button state as false
//Put WiFi cridentials and token here----------------------------------
char auth[] = "************";
char ssid[] = "********";
char pass[] = "**********";
//---------------------------------------------------------------------
BlynkTimer timer; // For activating the Blynk Timer
#define DoorIsOpened 1
#define DoorIsClosed 0
#define SignalCloseDoor LOW
#define NoSignalCloseDoor HIGH
void setup() {
Blynk.begin(auth, ssid, pass); // Something that Blynk needs to connect to wifi
Serial.begin(9600);
digitalWrite(relay, NoSignalCloseDoor); // Sets the relay HIGH (that means open circuit)
pinMode(buttonPin, INPUT);
pinMode(relay, OUTPUT); // sets the relay as OUTPUT
pinMode(magnet, INPUT_PULLUP); // Sets the magnet sensor as an INPUT_PULLUP
pinMode(buzzer, OUTPUT); // Sets the buzzer as an output
timer.setInterval(1000L, magnetSensor); // Runs the "magnetSensor" function every second
timer.setInterval(100L, switchPin);
}
void switchPin(){
ButtonState = digitalRead(buttonPin);
if(ButtonState == 1){
digitalWrite(relay, LOW);
delay(1000);
digitalWrite(relay, HIGH);
Serial.println("Button pushed!");
}
else{
Serial.println("Button released!");
}
}
void magnetSensor() { // Reads the state of the door (closed or open)
magnetValue = digitalRead(magnet);
if (magnetValue == DoorIsOpened) { // If the door is open...
Blynk.virtualWrite(V2, "GARASJEPORT Ă…PEN"); // ...write "garage door is open" (translated to english) to the Blynk app
}
else { // If the door is closed...
Blynk.virtualWrite(V2, "GARASJEPORT STENGT"); // ...write "Garage door is closed" (translated to english) to the Blynk app
Flag_buttonPressed = false; // reset the flag because door is closed
}
}
void buzzerTone() { // Makes a warning sound before garage door closes after button in Blynk app is pushed
if (!Flag_buttonPressed && magnetValue == DoorIsOpened ) {
Flag_buttonPressed = true;
for (int i = 0; i < 5; i++) {
tone(buzzer, 600);
delay(500);
noTone(buzzer);
delay(500);
}
}
}
BLYNK_WRITE(V1) { //Blynk app button
int pinData = param.asInt(); // Some code for sync the Blynk app button
magnetValue = digitalRead(magnet);
if (pinData == 1 && magnetValue == DoorIsOpened) {
buzzerTone(); // Activate the warning sound
digitalWrite(relay, SignalCloseDoor); //... Close the garage door
timer.setTimeout(1000L, RELAYoff); // Makes the signal to the door last for half second to ensure that it closes
}
if (pinData == 1 && magnetValue == DoorIsClosed) {
digitalWrite(relay, SignalCloseDoor);
timer.setTimeout(1000L, RELAYoff); // Gives the garage opener half second of power to ensure that it closes
}
}
void RELAYoff() { // turns the signal to the garage opener off
digitalWrite(relay, NoSignalCloseDoor);
}
void loop() {
Blynk.run();
timer.run();
}