Board = WeMos ATMega2560 + ESP8266 (LOLIN(WEMOS) D1 R2 & mini in “Boards manager”)
Board on amazon - https://www.amazon.de/dp/B07HML8DZD/ref=pe_27091401_487027711_TE_SCE_dp_2
The task I want this thing to do:
When VirtualPin0 value is HIGH, it sends me PIR sensor readings - they show up in the Blynk IoT app.
So far I have managed to independently control both modules. Got PIR reading from the ATMega2560 in serial monitor and managed to control the ESP8266 from Blynk with virtual pins.
The problem:
I cant understand how to send PIR data from the Mega2560 to ESP8266 to Blynk and the other way around. My guess is that I need to first upload code to the ESP8266 module when mode (5,6,7 - ON, else OFF) is enabled << PICTURE BELOW >> to connect to Blynk servers, then I upload a different code to ATMega2560 using mode (1,2,3,4 - ON, else OFF) and run the whole thing from the same mode. Code is the big problem, because either Im blind or there just isnt enough information about a similar project or the board Im using.
Mode selection on the board:
Code:
#define BLYNK_TEMPLATE_ID "****"
#define BLYNK_DEVICE_NAME "****"
#define BLYNK_AUTH_TOKEN "********"
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
char auth[] = BLYNK_AUTH_TOKEN;
char ssid[] = "****";
char pass[] = "****";
int pirPin = 9;
int sens_value = 0;
int V0value;
BLYNK_WRITE(V0) // Starts/Stops sending data
{
V0value = param.asInt();
Blynk.virtualWrite(V1, V0value); // Sends back, if V0 was activated
}
void setup()
{
Serial.begin(115200);
delay(10);
Blynk.begin(auth, ssid, pass);
pinMode(pirPin, INPUT);
}
void loop()
{
if (V0value == HIGH)
{
getPirValue();
}else{
Serial.println("OFF");
delay(1000);
}
Blynk.run();
}
void getPirValue(void)
{
sens_value = digitalRead(pirPin);
if (sens_value == HIGH)
{
Serial.println(1);
Blynk.virtualWrite(V2, 1); // Sends back PIR sensor reading
}if (sens_value == LOW){
Serial.println(0);
Blynk.virtualWrite(V2, 0);
}
}
This works fine when uploaded on the EPS8266 module, untill I need to send digitalRead or analogRead value to Blynk, then its just sending 1s, no matter what the sensor is doing. Im guessing its because it cant read theme and the ATMega module has to send the reading to EPS8266? As mentioned before, I dont know how to make theme communicate.
Blynk library v1.0.1
Arduino IdE v1.8.19 (Windows Store 1.8.57.0)
Thanks in advance.