• Hardware model + communication type: Arduino Nano with SIM800L GSM module
• Blynk server region :fra1
• Blynk Library version : latest
The following code works great. However, the device gets disconnected when I uncomment BLYNK_WRITE(V4) logic and tries to reconnect on blynk.cloud:80. What might cause this issue?
The V4 datastream is an integer with values 0-1 same as V0 which works fine
/*************************************************************
Attention! Please check out TinyGSM guide:
https://tiny.cc/tinygsm-readme
Change GPRS apm, user, pass, and Blynk auth token to run :)
You’ll need:
- Blynk IoT app (download from App Store or Google Play)
- Arduino Nano board
- Decide how to connect to Blynk
(USB, Ethernet, Wi-Fi, Bluetooth, ...)
There is a bunch of great example sketches included to show you how to get
started. Think of them as LEGO bricks and combine them as you wish.
For example, take the Ethernet Shield sketch and combine it with the
Servo example, or choose a USB sketch and add a code from SendData
example.
*************************************************************/
/* Fill-in information from Blynk Device Info here */
#define BLYNK_TEMPLATE_ID "tpl"
#define BLYNK_TEMPLATE_NAME "name"
#define BLYNK_AUTH_TOKEN "token"
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM800
// Default heartbeat interval for GSM is 60
// If you want override this value, uncomment and set this option:
//#define BLYNK_HEARTBEAT 30
#include <TinyGsmClient.h>
#include <BlynkSimpleTinyGSM.h>
// Your GPRS credentials
// Leave empty, if missing user or pass
char apn[] = "internet";
char user[] = "";
char pass[] = "";
///////////////////////////////PHOTO RESISTORS///////////////////////////////////////
// Pin definitions
const int photoResistorPins[4] = { A0, A1, A2, A3 }; // Photoresistor analog input pins
const int numSensors = 4; // Number of sensors
// Variables to store ambient light levels for each sensor
int ambientLight[numSensors];
// Variables to store current sensor values
int currentLight[numSensors];
int sensitivityThreshold = 200;
int wifiDetectorEnabled =0;
// Time interval for recalibrating ambient light
const unsigned long recalibrationInterval = 10000; // 10 seconds
const unsigned long suddenLightChangeInterval = 100;
/////////////////////////////////////////////////////////////////////////////////////
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(6, 5); // RX, TX
TinyGsm modem(SerialAT);
BLYNK_WRITE(V0) {
uint8_t value = param.asInt();
// any code you place here will execute when the virtual pin value changes
Serial.print("Blynk.Cloud is writing something to V0: ");
Serial.println(value, DEC);
digitalWrite(LED_BUILTIN, value);
}
BLYNK_WRITE(V1) {
uint8_t value = param.asInt();
// any code you place here will execute when the virtual pin value changes
Serial.print("Blynk.Cloud is writing something to V1: ");
Serial.println(value, DEC);
sensitivityThreshold = value;
}
//BLYNK_WRITE(V4) {
// uint8_t value = param.asInt();
// // any code you place here will execute when the virtual pin value changes
// Serial.print("Blynk.Cloud is writing something to V4: ");
// Serial.println(value, DEC);
// wifiDetectorEnabled = value;
//}
void lightChangeEvent() {
// Read current light levels and check for sudden changes
for (int i = 0; i < numSensors; i++) {
currentLight[i] = analogRead(photoResistorPins[i]);
// Calculate the difference between current light and ambient light
int lightDifference = abs(currentLight[i] - ambientLight[i]);
// If the difference exceeds the threshold, print a detection message
if (lightDifference > sensitivityThreshold) {
Serial.print("Sudden light change detected on sensor ");
Serial.print(i);
Serial.print("! Light difference: ");
Serial.println(lightDifference);
Blynk.virtualWrite(V0, 0); //switch off speaker relay
digitalWrite(LED_BUILTIN, 0);
Blynk.logEvent("speaker_off"); // send notification to phone
}
}
}
void recalibrationEvent() {
Serial.println("Recalibrating ambient light levels...");
for (int i = 0; i < numSensors; i++) {
ambientLight[i] = analogRead(photoResistorPins[i]);
}
// Print new ambient light levels for debugging
Serial.println("New Ambient Light Levels:");
for (int i = 0; i < numSensors; i++) {
Serial.print("Sensor ");
Serial.print(i);
Serial.print(": ");
Serial.println(ambientLight[i]);
}
String ambient;
for (int i = 0; i < numSensors; i++) {
ambient += (String)ambientLight[i] + " ";
}
Blynk.virtualWrite(V2, ambient);
}
BlynkTimer timer;
void setup() {
pinMode(LED_BUILTIN, OUTPUT); //LED output
// Debug console
Serial.begin(115200);
delay(10);
// Set GSM module baud rate
SerialAT.begin(9600);
delay(3000);
// Restart takes quite some time
// To skip it, call init() instead of restart()
Serial.println("Initializing modem...");
modem.restart();
// Unlock your SIM card with a PIN
//modem.simUnlock("1234");
Blynk.begin(BLYNK_AUTH_TOKEN, modem, apn, user, pass);
timer.setInterval(recalibrationInterval, recalibrationEvent);
timer.setInterval(suddenLightChangeInterval, lightChangeEvent);
for (int i = 0; i < numSensors; i++) {
ambientLight[i] = analogRead(photoResistorPins[i]);
}
// Print initial ambient light levels
Serial.println("Initial Ambient Light Levels:");
for (int i = 0; i < numSensors; i++) {
Serial.print("Sensor ");
Serial.print(i);
Serial.print(": ");
Serial.println(ambientLight[i]);
}
}
void loop() {
Blynk.run();
timer.run();
// You can inject your own code or combine it with other sketches.
// Check other examples on how to communicate with Blynk. Remember
// to avoid delay() function!
}