Hello guys, I am building a iot project using 2 wemos boards where one acts as a counter which sends a high value when there's more than or equal to 1 person in the room. While the other acts as the relay system as well as window closing automation. I have an issue as when theres a person in the room i am unable to toggle the lights with my blynk app. but when the person exits i am abit to toggle. I have attached the counter as well as the relay code below. Thanks for the assistance guys:) [edited: i just added an image of how my circuit looks like below]
Relay code
// Template ID, Device Name and Auth Token are provided by the Blynk.Cloud
// See the Device Info tab, or Template settings
#define BLYNK_TEMPLATE_ID "Please fill in"
#define BLYNK_DEVICE_NAME "Please fill in"
#define BLYNK_AUTH_TOKEN "Please fill in"
// Comment this out to disable prints and save space
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <Servo.h>
#include <Servo.h>
Servo tap_servo;
int tap_servo_pin =4;
int RelayPin1 = D2;
int RelayPin2 = D3;
int RelayPin3 = D5;
int RelayPin4 = D6;
const int pinButton = D8;
char auth[] = BLYNK_AUTH_TOKEN;
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "test";
char pass[] = "";
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin 1
BLYNK_WRITE(V1)
{
int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
// You can also use:
// String i = param.asStr();
// double d = param.asDouble();
//Serial.print("V1 Slider value is: ");
//Serial.println(pinValue);
digitalWrite(RelayPin1, pinValue);
}
BLYNK_WRITE(V2) { //Button Widget is writing to pin V1
int pinValue = param.asInt();
digitalWrite(RelayPin2, pinValue);
}
BLYNK_WRITE(V3) { //Button Widget is writing to pin V2
int pinValue = param.asInt();
digitalWrite(RelayPin3, pinValue);
}
BLYNK_WRITE(V4) { //Button Widget is writing to pin V3
int pinValue = param.asInt();
digitalWrite(RelayPin4, pinValue);
}
void setup()
{
Blynk.run();
// Debug console
Serial.begin(9600);
tap_servo.attach(tap_servo_pin);
pinMode(RelayPin1, OUTPUT);
pinMode(RelayPin2, OUTPUT);
pinMode(RelayPin3, OUTPUT);
pinMode(RelayPin4, OUTPUT);
pinMode(pinButton, INPUT);
digitalWrite(RelayPin1, HIGH);
digitalWrite(RelayPin2, HIGH);
digitalWrite(RelayPin3, HIGH);
digitalWrite(RelayPin4, HIGH);
Blynk.begin(auth, ssid, pass);
// You can also specify server:
//Blynk.begin(auth, ssid, pass, "blynk.cloud", 80);
//Blynk.begin(auth, ssid, pass, IPAddress(192,168,1,100), 8080);
}
void loop()
{
int value = analogRead(A0);//read value
Serial.print("Value : ");
Serial.println(value);
{
if (value > 900)
{tap_servo.write(0);
delay(10);
}
if (value < 500)
{tap_servo.write(180);
delay(10);
}
}
int qq = digitalRead(RelayPin2);
int stateButton = digitalRead(pinButton);
if (stateButton == LOW && qq == HIGH){
digitalWrite(RelayPin2, HIGH);
}
else if (stateButton == LOW && qq == LOW){
digitalWrite(RelayPin2, LOW);
}
else if (stateButton == HIGH){
digitalWrite(RelayPin2, stateButton);
}
delay(20);
Blynk.run();
}
Counter code
#include <LiquidCrystal_I2C.h>
LiquidCrystal_I2C lcd(0x27, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE);
#define sensorPin1 D0
#define sensorPin2 D5
#define relay D10
int sensorState1 = 0;
int sensorState2 = 0;
int count=0;
void setup()
{
pinMode (sensorPin1,INPUT_PULLUP);
pinMode (sensorPin2, INPUT_PULLUP);
pinMode(relay, OUTPUT);
lcd.begin(16,2);
lcd.backlight();
lcd.setCursor(4,0);
lcd.print("COUNTER");
lcd.setCursor(0,1);
lcd.print("No Visitors ");
delay(200);
}
void loop()
{
sensorState1 = digitalRead(sensorPin1);
sensorState2 = digitalRead(sensorPin2);
if(sensorState1 == LOW){
count++;
delay(1000);
}
if(sensorState2 == LOW){
if(count==0){
}
else{
count--;
}
delay(1000);
}
if(count<=0)
{
digitalWrite(relay, LOW);
lcd.setCursor(0,1);
lcd.print("No visitors ");
}
else if (count>0 && count<10){
digitalWrite(relay, HIGH);
lcd.setCursor(0,1);
lcd.print("Visitors: ");
lcd.setCursor(12,1);
lcd.print(count);
lcd.setCursor(13,1);
lcd.print(" ");
}
else {
digitalWrite(relay, HIGH);
lcd.setCursor(0,1);
lcd.print("Visitors: ");
lcd.setCursor(12,1);
lcd.print(count);
}
}