Hello,
I experience some strange problem. My board is Arduino MKR 1400 GSM. I use BLE connection over serial with HM-10 module. My sketch looks like follows:
#include "StdAfx.h"
#include "Defines.h"
#define BLYNK_PRINT Serial1
#define BLESerial Serial1
// Blynk App token
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
// Timer class instances..
MillisTimers tmLedFlash;
MillisTimers tmCmdDelay;
// timers stuff
bool g_setupStatus = true;
bool g_doorState = DoorStates::LOCK;
bool g_rLedState = false;
bool g_gLedState = false;
bool g_bLedState = false;
bool g_fsrStateChange = false;
uint32_t g_fsrStateChangeTime = 0UL;
void setup()
{
g_setupStatus = false; // Initialization failed at the stage, lets keep setting up
//setup door simulation pin
pinMode(doorPin, INPUT_PULLUP);
// set LED pin to output mode
pinMode(rLedPin, OUTPUT);
pinMode(gLedPin, OUTPUT);
pinMode(bLedPin, OUTPUT);
// all LED indicator on startup
digitalWrite(rLedPin, HIGH);
digitalWrite(gLedPin, HIGH);
digitalWrite(bLedPin, HIGH);
Serial.begin(baudrate);
while (!Serial)
{
; // Need to wait the USB connection to be established? wait, so can see setup logs in monitor
}
BLESerial.begin(baudrate);
Blynk.begin(BLESerial, auth); // Start talking to Blynk app
Serial.println("Waiting for connections...");
// init timers
tmLedFlash.init();
tmCmdDelay.init();
g_setupStatus = true;
// shut off leds when setup is completed
if (g_setupStatus)
{
digitalWrite(rLedPin, LOW);
digitalWrite(gLedPin, LOW);
digitalWrite(bLedPin, LOW);
}
}
void loop()
{
Blynk.run();
// read from the HM-10 and print in the Serial
if (BLESerial.available())
Serial.write(BLESerial.read());
// read from the Serial and print to the HM-10
if (Serial.available())
BLESerial.write(Serial.read());
g_doorState = digitalRead(doorPin);
if (g_doorState == DoorStates::OPEN)
{ // door opned
if (tmLedFlash.WhileRepeat(550, 0))
{
//digitalWrite(rLedPin, (g_rLedState) ? HIGH : LOW); // start blinking with led
Blynk.virtualWrite(V1, (g_rLedState) ? ON : OFF);
g_rLedState = !g_rLedState;
#ifdef VM_DEBUG
Serial.println("Door: OPEN");
#endif
}
}
else { // door locked
if (tmCmdDelay.WhileRepeat(550, 0))
{
// digitalWrite(rLedPin, LOW); // shut off the led
Blynk.virtualWrite(V1, OFF);
#ifdef VM_DEBUG
Serial.println("Door: LOCKED");
#endif
}
}
}
Stdafx.h
// StdAfx.h
#ifndef _STDAFX_h
#define _STDAFX_h
// Libraries
#include <stdarg.h>
#include <wiring_private.h> // for Hardware UART
#include <BlynkSimpleSerialBLE.h> // BLE bridge for Blynk
#include <Wire.h> // BUS communication dependancy
#include <LiquidCrystal_I2C.h> // LCD with I2C module
#include <LcdDisplay.h> // Layer class for I2C and LCD, own
#include <OneWire.h> // One wire stuff, e.g. temp sensors
#include <DallasTemperature.h> // External temperature one wire sensors
#include <MKRGSM.h>
//#include <Gsm.h>
#include <MillisTimers.h>
#endif
MillisTimers.cpp
#include <Arduino.h>
#include "MillisTimers.h"
void MillisTimers::init()
{
this->enabled = true;
this->passedCycles = -1;
this->lastMillis = millis();
}
/* **
Returns false if still delaying, true if time has passed
** */
bool MillisTimers::WhileRepeat(unsigned long delay, short repeatNumber)
{
unsigned long currentTime = millis();
short _repeats = repeatNumber;
if (!this->enabled)
return false;
if (currentTime - lastMillis >= delay)
{
if (_repeats)
{
if (_repeats == this->passedCycles)
{
this->enabled = false;
return false;
}
this->passedCycles++;
}
lastMillis = currentTime; // adjust the extra delay() in usage
return true;
}
return false;
}
bool MillisTimers::AfterRepeat(unsigned long delay, bool repeat)
{
if (this->enabled && millis() - lastMillis <= delay)
{
return false;
}
if (!repeat)
this->enabled = false;
return true;
}
bool MillisTimers::LastPeriod(unsigned long snappedTime, unsigned long period)
{
if (millis() - snappedTime <= period)
return true;
return false;
}
MillisTimers.h
// MillisTimers.h
#ifndef _MILLISTIMERS_h
#define _MILLISTIMERS_h
class MillisTimers
{
protected:
public:
void init();
bool WhileRepeat(unsigned long delay, short repeatNumber);
bool AfterRepeat(unsigned long delay, bool repeat);
bool LastPeriod(unsigned long snappedTime, unsigned long period);
private:
short passedCycles;
unsigned long lastMillis;
bool enabled;
};
#endif
The problem is that when I use any of the timers, e.g. here
if (tmLedFlash.WhileRepeat(550, 0))
{
//digitalWrite(rLedPin, (g_rLedState) ? HIGH : LOW); // start blinking with led
Blynk.virtualWrite(V1, (g_rLedState) ? ON : OFF);
g_rLedState = !g_rLedState;
#ifdef VM_DEBUG
Serial.println("Door: OPEN");
#endif
}
}
I periodically get junk data in serial monitor. All baud rates are set in and serial monitor is also set to the same rate.
The junk data is NULL or sometimes even part of Blynk auth token, any advice why it may happen?
Example serial monitor output:
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
ST
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
âDoor: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
âDoor: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
This one appeared when I connected back with blynk while the sketch was running. The longer string inside is the token. I changed it here for fake one for post purpose but maintained its length.
Door: OPEN
Door: OPEN
Door: OPEN
Door: OPEN
NN
Door: OPEN
Door: OPEN
Door: OPEN
ââââ Q3aAvoDsgsGtDFfAERhDDfzzsdSsFak5Door: OPEN
Door: OPEN
âDoor: OPEN
Door: OPEN
Door: OPEN
When blynk app disconnects the serial output returns to normal, once I connect back with the app I get junk data.
The junk data breaks the blinking and it takes a while to return to proper operation. Hoping for advice.