Hello,
Particle Photon User with WiFi communication with blynk-lib v0.5.4 here, by using Blynk Server (no local one). Sketch running and works fine, but my iPhone/Android-App says “device offline”.
So I debugged with serial printout and got a lot of “Login timeouts”. I changed the auth-token and for the first seconds it works also on the smartphone, but I got “Login timeouts” again in the serial log.
Thx for review,
Ingo
// Ingo Lohs, Version 1.4 - 18.11.2018 - works with Particle Photon v0.7.0
// myled-mosefet-blynk-v2.ino
// https://www.hackster.io/ingo-lohs/mypir-sensor-activate-analog-rgb-stripe-controlled-by-photon-2e8240
#define BLYNK_DEBUG // Optional, this enables lots of prints
#define BLYNK_PRINT Serial
#include <blynk.h>
#include <Adafruit_TSL2561_U.h>
// Change von Version 1.1 > 1.2:
// Änderung der Default-RGB-Werte via zweites Zergba (zusätzliches Blynk-Widget) gebunden an V6, um dynamisch die Default-RGB-Werte abändern zu könnnen ohne erneut den Code flashen zu müssen
// Change von Version 1.2 > 1.3:
// Vorbelegung der Variablen red, green, blue mit der Zuweisung von jew. 255: so ist bei einem Restart der Default für das Licht als "weiss" vorbelegt
// Change von Version 1.3 > 1.4:
// Aufnahme des Lichtsensors TSL2561 inkl. seiner Adafruit-Library - Default-Address is: 0x39
// Firmware-Update von v0.6.3 > v0.7.0
// Änderung der PINs von Control-LED und PIR-Sensor, um SDA und SCL für den TSL2561 verfügbar zu haben
// Sensor overload kommt im Dunkeln und so ist der analogvalue = 1 gesetzt
// Erweiterung: * vorstellbar wäre eine while-Scheife, die solange LED on hat wie auch die PIR-Detektion anhält
// Quelle: http://diotlabs.daraghbyrne.me/7-communicating-events/pir/
// https://learn.adafruit.com/pir-passive-infrared-proximity-motion-sensor/using-a-pir
// https://gist.github.com/zsup/9496462
// http://community.garadget.com/t/instructions-for-home-grown-garadget-clone/69 --> Relay Belegung und Schaltung
// https://learn.adafruit.com/rgb-led-strips
int inputPin = D4; // choose the input pin (for PIR sensor)
int ledPin = D3; // LED Pin
int pirState = LOW; // we start, assuming no motion detected
int val = 0; // variable for reading the pin status
int boardLed = D7; // photon onBoard LED
int calibrateTime = 2000; // wait for the thingy to calibrate
int light_threshold = 100; // brightness as analog value to decide its time for more light as analog value
int analogvalue; // Here we are declaring the integer variable analogvalue, which we will use later to store the value of the photoresistor.
unsigned long lastmillis = 0; // time for iteration the loop
int red = 255;
int green = 255;
int blue = 255;
#define REDPIN A5
#define GREENPIN WKP
#define BLUEPIN A4
// #define FADESPEED 5 // make this higher to slow down - used in function ColorFade as Option
int delayColorFade = 15; // presents the value in seconds
char auth[] = "xxx6d99"; // cap_cow_pir_led_flur_oben
//char auth[] = "<<your blync auth-code here>>";
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
// *********
BLYNK_WRITE(V6) // zeRGBa - Default
{
// The param can contain multiple values, in such case:
red = param[0].asInt();
green = param[1].asInt();
blue = param[2].asInt();
}
// *********
BLYNK_WRITE(V4) // Blynk app WRITES Slider widget
{
light_threshold = param.asInt();
Serial.println("******************************");
Serial.print(light_threshold);
Serial.println(" analog value = new Threshold for Light");
Serial.println("******************************");
}
// *********
void update_light_threshold()
{
Blynk.virtualWrite(V3, light_threshold);
}
// *********
BLYNK_WRITE(V2) // Blynk app WRITES Slider widget
{
delayColorFade = param.asInt(); // macht aus den Blynk-Werten millisekunden
Serial.println("******************************");
Serial.print(delayColorFade);
Serial.println(" milli-sec = new Delay for Function ColorFade");
Serial.println("******************************");
}
// *********
void update_delayColorFade()
{
Blynk.virtualWrite(V1, delayColorFade);
}
// *********
BLYNK_WRITE(V0) // zeRGBa - Color
{
// The param can contain multiple values, in such case:
int red = param[0].asInt();
int green = param[1].asInt();
int blue = param[2].asInt();
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
}
// *********
void setup() {
Serial.begin(9600);
pinMode(REDPIN, OUTPUT);
pinMode(GREENPIN, OUTPUT);
pinMode(BLUEPIN, OUTPUT);
pinMode(boardLed,OUTPUT); // on-board LED
pinMode(ledPin, OUTPUT); // control PIR LED
pinMode(inputPin, INPUT); // declare sensor as input
digitalWrite(boardLed,HIGH); // Now flash the D7 LED on and off
Particle.publish("PIR-Motion", "PIR now online - 1/2", PRIVATE); // informs user via Particle Cloud
digitalWrite(boardLed,LOW);
// *** TSL2561 - Start
Serial.println("Light Sensor Test"); Serial.println("");
/* Initialise the sensor */
if(!tsl.begin())
{
/* There was a problem detecting the ADXL345 ... check your connections */
Serial.print("Ooops, no TSL2561 detected ... Check your wiring or I2C ADDR!");
while(1);
}
/* Display some basic information on this sensor */
displaySensorDetails();
/* Setup the sensor gain and integration time */
configureSensor();
/* We're ready to go! */
digitalWrite(boardLed,HIGH); // Now flash the D7 LED on and off
Particle.publish("PIR-Motion", "TSL2561 now online - 2/2", PRIVATE); // informs user via Particle Cloud
digitalWrite(boardLed,LOW);
// *** TSL2561 - Ende
Blynk.begin(auth); // Start Blynk
}
// *********
void loop() {
Blynk.run();
// if the sensor is calibrated
if (calibrated())
{
if ((millis() - lastmillis) > 1000) {
lastmillis = millis();
readTSL(); // values from TSL2561
reportTheData(); // motion detected?
update_delayColorFade(); // Update Blynk-App Input
update_light_threshold(); // Update Blynk-App Input
}
}
}
// *********
bool calibrated() {
return millis() - calibrateTime > 0;
}
// *********
void reportTheData() {
// check the PIR-Sensor
val = digitalRead(inputPin);
// if the sensor reads high
// or there is now motion
if (val == HIGH) {
// the current state is no motion
// i.e. it's just changed
// announce this change by publishing an eent
if (pirState == LOW) {
// we have just turned on
Particle.publish("PIR Motion", "Motion detected", PRIVATE);
pirState = HIGH;
setLED( pirState );
if (analogvalue /*light*/ <= light_threshold) {
Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
Particle.publish("PIR Motion", String(light_threshold), PRIVATE);
Particle.publish("PIR Motion", "LED Streifen an", PRIVATE);
ColorFade();
} else {
//Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
}
}
} else {
if (pirState == HIGH) {
// we have just turned of
// Update the current state
pirState = LOW;
setLED( pirState );
}
}
}
// *********
void setLED( int state )
{
digitalWrite(ledPin, state);
}
// *********
void ColorFade() {
// LED Streifen anschalten
analogWrite(REDPIN, red);
analogWrite(GREENPIN, green);
analogWrite(BLUEPIN, blue);
delay(delayColorFade * 1000); // 15 Sekunden bleibt Licht per Default an bzw. solange, wie über den Blynk Slider eingestellt wurde
/* Fading ist ausgeschaltet
int r, g, b;
// fade from blue to violet
for (r = 0; r < 256; r++) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from violet to red
for (b = 255; b > 0; b--) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from red to yellow
for (g = 0; g < 256; g++) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
// fade from yellow to green
for (r = 255; r > 0; r--) {
analogWrite(REDPIN, r);
delay(FADESPEED);
}
// fade from green to teal
for (b = 0; b < 256; b++) {
analogWrite(BLUEPIN, b);
delay(FADESPEED);
}
// fade from teal to blue
for (g = 255; g > 0; g--) {
analogWrite(GREENPIN, g);
delay(FADESPEED);
}
*/
// LED Streifen ausschalten
analogWrite(REDPIN, 0);
analogWrite(BLUEPIN, 0);
analogWrite(GREENPIN, 0);
}
// ********** Neuaufnahme von Funktionen für den TSL2561 - Start
void readTSL()
{
/* Get a new sensor event */
sensors_event_t event;
tsl.getEvent(&event);
/* Display the results (light is measured in lux) */
if (event.light)
{
analogvalue = (event.light);
Serial.print(analogvalue); Serial.println(" lux");
Blynk.virtualWrite(V7, analogvalue);
Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
}
else
{
/* If event.light = 0 lux the sensor is probably saturated
and no reliable data could be generated! */
Serial.println("Sensor overload - 1 lux");
analogvalue = 1; // Ersatz im Falle eines Problems
//Blynk.virtualWrite(V7, analogvalue);
//Particle.publish("PIR Motion", String(analogvalue), PRIVATE);
}
}
/**************************************************************************/
/*
Displays some basic information on this sensor from the unified
sensor API sensor_t type (see Adafruit_Sensor for more information)
*/
/**************************************************************************/
void displaySensorDetails(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
Serial.println("------------------------------------");
Serial.print ("Sensor: "); Serial.println(sensor.name);
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" lux");
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" lux");
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" lux");
Serial.println("------------------------------------");
Serial.println("");
}
/**************************************************************************/
/*
Configures the gain and integration time for the TSL2561
*/
/**************************************************************************/
void configureSensor(void)
{
/* You can also manually set the gain or enable auto-gain support */
// tsl.setGain(TSL2561_GAIN_1X); /* No gain ... use in bright light to avoid sensor saturation */
//tsl.setGain(TSL2561_GAIN_16X); /* 16x gain ... use in low light to boost sensitivity */
tsl.enableAutoRange(true); /* Auto-gain ... switches automatically between 1x and 16x */
/* Changing the integration time gives you better sensor resolution (402ms = 16-bit data) */
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS); /* fast but low resolution */
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_101MS); /* medium resolution and speed */
//tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_402MS); /* 16-bit data but slowest conversions */
/* Update these values depending on what you've set above! */
Serial.println("------------------------------------");
Serial.print ("Gain: "); Serial.println("auto");
Serial.print ("Timing: "); Serial.println("13 ms");
Serial.println("------------------------------------");
}
// ********** Neuaufnahme von Funktionen für den TSL2561 - Ende