Hi,
I’m trying to understand why my code seems to continuously disconnect and reconnect to my network. If I run the terminal widget example using the same network settings I get more stability (although I still get Heartbeat timeout).
[51435] Heartbeat timeout
[56327] Connecting to blynk-cloud.com:8442
[56631] Ready (ping: 119ms).
on
[101517] Connecting to blynk-cloud.com:8442
[101826] Ready (ping: 119ms).
All LEDs switched ON
[127951] Heartbeat timeout
[132832] Connecting to blynk-cloud.com:8442
[133153] Ready (ping: 120ms).
off
[138155] Connecting to blynk-cloud.com:8442
[138465] Ready (ping: 118ms).
All LEDs switched OFF
[154465] Heartbeat timeout
[157468] Connecting to blynk-cloud.com:8442
[157778] Ready (ping: 118ms).
Although the code is kind of working I am frustrated at how many times I need to send the terminal commands for them to work.
Is my code structured correctly to achieve the proper result?
#define BLYNK_PRINT Serial
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h>
#include <Adafruit_NeoPixel.h>
#define PIN 3
#define NUMPIXELS 30
int R;
int G;
int B;
int SPEED = 50;
boolean aOn = false;
boolean bOn = false;
boolean cOn = false;
boolean dOn = false;
boolean eOn = false;
boolean fOn = false;
boolean gOn = false;
boolean allOn = false;
String inString = "";
Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUMPIXELS, PIN, NEO_GRB + NEO_KHZ800);
WidgetTerminal terminal(V1);
char auth[] = "YourAuthToken";
// Mac address should be different for each device in your LAN
byte arduino_mac[] = { 0xDE, 0xEF, 0xCA, 0xEE, 0xEE, 0xED };
IPAddress arduino_ip ( 192, 168, 0, 21);
IPAddress dns_ip ( 192, 168, 0, 1);
IPAddress gateway_ip ( 192, 168, 0, 1);
IPAddress subnet_mask(255, 255, 255, 0);
void setup()
{
Serial.begin(9600);
Blynk.begin(auth, "blynk-cloud.com", 8442, arduino_ip, dns_ip, gateway_ip, subnet_mask, arduino_mac);
strip.begin();
strip.show(); // Initialize all pixels to 'off'
while (Blynk.connect() == false) {
// Wait until connected
}
Serial.println("Welcome to your computer lights");
Serial.println("For preset colors select 1-8");
Serial.println("Switch ON or OFF using 'on' or 'off' commands");
Serial.println("'rainbow' 'theater' 'theaterrainbow' also available");
terminal.println("Welcome to your computer lights");
terminal.println("For preset colors select 1-8");
terminal.println("Switch ON or OFF using 'on' or 'off' commands");
terminal.println("'rainbow' 'theater' 'theaterrainbow' also available");
terminal.flush();
}
BLYNK_WRITE(V1)
{
String inString = param.asStr();
Serial.println(inString);
if (Blynk.connect() == false)
{
terminal.println("Connection Lost");
}
if (inString == "1")
{
R = 255;
G = 0;
B = 0;
Serial.println("Color set to Red");
terminal.println("Color set to Red");
terminal.flush();
}
if (inString == "2")
{
R = 255;
G = 0;
B = 132;
Serial.println("Color set to Pink");
terminal.println("Color set to Pink");
terminal.flush();
}
if (inString == "3")
{
R = 0;
G = 0;
B = 255;
Serial.println("Color set to Blue");
terminal.println("Color set to Blue");
terminal.flush();
}
if (inString == "4")
{
R = 0;
G = 255;
B = 255;
Serial.println("Color set to Turquoise");
terminal.println("Color set to Turquoise");
terminal.flush();
}
if (inString == "5")
{
R = 0;
G = 255;
B = 0;
Serial.println("Color set to Green");
terminal.println("Color set to Green");
terminal.flush();
}
if (inString == "6")
{
R = 255;
G = 150;
B = 0;
Serial.println("Color set to Yellow");
terminal.println("Color set to Yellow");
terminal.flush();
}
if (inString == "7")
{
R = 255;
G = 70;
B = 0;
Serial.println("Color set to Orange");
terminal.println("Color set to Orange");
terminal.flush();
}
if (inString == "8")
{
R = 255;
G = 255;
B = 255;
Serial.println("Color set to White");
terminal.println("Color set to White");
terminal.flush();
}
if (inString == "off")
{
R = 0;
G = 0;
B = 0;
backwardsColorWipe(strip.Color(R, G, B), SPEED);
Serial.println("All LEDs switched OFF");
terminal.println("All LEDs switched OFF");
terminal.flush();
}
if (inString == "on")
{
if (R == 0 && G == 0 && B == 0)
{
R = 255;
G = 255;
B = 255;
}
colorWipe(strip.Color(R, G, B), SPEED);
Serial.println("All LEDs switched ON");
terminal.println("All LEDs switched ON");
terminal.flush();
}
if (inString == "rainbow")
{
Serial.println("Rainbow selected");
terminal.println("Rainbow selected");
terminal.flush();
rainbow(SPEED);
}
if (inString == "theater")
{
Serial.println("Theater Chase selected");
terminal.println("Theater Chase selected");
terminal.flush();
theaterChase(strip.Color(R, G, B), SPEED);
}
if (inString == "theaterrainbow")
{
Serial.println("Theater Chase Rainbow Selected");
terminal.println("Theater Chase Rainbow Selected");
terminal.flush();
theaterChaseRainbow(SPEED);
}
if (inString == "refresh")
{
refreshMenu();
}
if (inString == "sega")
{
if (aOn)
{
Serial.println("Segment A switched OFF");
terminal.println("Segment A switched OFF");
terminal.flush();
}
else {
Serial.println("Segment A switched ON");
terminal.println("Segment A switched ON");
terminal.flush();
}
segmentAOn(R, G, B);
}
if (inString == "segb")
{
if (bOn)
{
Serial.println("Segment B switched OFF");
terminal.println("Segment B switched OFF");
terminal.flush();
}
else {
Serial.println("Segment B switched ON");
terminal.println("Segment B switched ON");
terminal.flush();
}
segmentBOn(R, G, B);
}
if (inString == "segc")
{
if (cOn)
{
Serial.println("Segment C switched OFF");
terminal.println("Segment C switched OFF");
terminal.flush();
}
else {
Serial.println("Segment C switched ON");
terminal.println("Segment C switched ON");
terminal.flush();
}
segmentCOn(R, G, B);
}
if (inString == "segd")
{
if (dOn)
{
Serial.println("Segment D switched OFF");
terminal.println("Segment D switched OFF");
terminal.flush();
}
else {
Serial.println("Segment D switched ON");
terminal.println("Segment D switched ON");
terminal.flush();
}
segmentDOn(R, G, B);
}
if (inString == "sege")
{
if (eOn)
{
Serial.println("Segment E switched OFF");
terminal.println("Segment E switched OFF");
terminal.flush();
}
else {
Serial.println("Segment E switched ON");
terminal.println("Segment E switched ON");
terminal.flush();
}
segmentEOn(R, G, B);
}
if (inString == "segf")
{
if (aOn)
{
Serial.println("Segment F switched OFF");
terminal.println("Segment F switched OFF");
terminal.flush();
}
else {
Serial.println("Segment F switched ON");
terminal.println("Segment F switched ON");
terminal.flush();
}
segmentFOn(R, G, B);
}
if (inString == "segg")
{
if (gOn)
{
Serial.println("Segment G switched OFF");
terminal.println("Segment G switched OFF");
terminal.flush();
}
else {
Serial.println("Segment G switched ON");
terminal.println("Segment G switched ON");
terminal.flush();
}
segmentGOn(R, G, B);
}
if (inString == "toggle")
{
if (allOn)
{
Serial.println("All LEDs switched OFF");
terminal.println("All LEDs switched OFF");
terminal.flush();
}
else {
Serial.println("All LEDs switched ON");
terminal.println("All LEDs switched ON");
terminal.flush();
}
switchAll(R, G, B);
}
if (aOn == true && bOn == true && cOn == true && dOn == true && eOn == true && fOn == true && gOn == true) {
allOn = true;
}
terminal.flush();
inString = "";
}
void loop()
{
Blynk.run();
}
void segmentAOn(int r, int g, int b) {
if (aOn == false) {
for(int i=0; i<4; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
aOn = true;
allOn = false;
}
}else{
for(int i=0; i<4; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
aOn = false;
allOn = false;
}
}
}
void segmentBOn(int r, int g, int b) {
if (bOn == false) {
for(int i=4; i<8; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
bOn = true;
allOn = false;
}
}else{
for(int i=4; i<8; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
bOn = false;
allOn = false;
}
}
}
void segmentCOn(int r, int g, int b) {
if (cOn == false) {
for(int i=8; i<12; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
cOn = true;
allOn = false;
}
}else{
for(int i=8; i<12; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
cOn = false;
allOn = false;
}
}
}
void segmentDOn(int r, int g, int b) {
if (dOn == false) {
for(int i=12; i<18; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
dOn = true;
allOn = false;
}
}else{
for(int i=12; i<18; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
dOn = false;
allOn = false;
}
}
}
void segmentEOn(int r, int g, int b) {
if (eOn == false) {
for(int i=18; i<22; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
eOn = true;
allOn = false;
}
}else{
for(int i=18; i<22; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
eOn = false;
allOn = false;
}
}
}
void segmentFOn(int r, int g, int b) {
if (fOn == false) {
for(int i=22; i<26; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
fOn = true;
allOn = false;
}
}else{
for(int i=22; i<26; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
fOn = false;
allOn = false;
}
}
}
void segmentGOn(int r, int g, int b) {
if (gOn == false) {
for(int i=26; i<30; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
gOn = true;
allOn = false;
}
}else{
for(int i=26; i<30; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
gOn = false;
allOn = false;
}
}
}
void switchAll(int r, int g, int b) {
if (allOn == false) {
for(int i=0; i<30; i++) {
strip.setPixelColor(i, strip.Color(r, g, b));
strip.show();
allOn = true;
aOn = true;
bOn = true;
cOn = true;
dOn = true;
eOn = true;
fOn = true;
gOn = true;
}
}else{
for(int i=0; i<30; i++) {
strip.setPixelColor(i, strip.Color(0, 0, 0));
strip.show();
allOn = false;
aOn = false;
bOn = false;
cOn = false;
dOn = false;
eOn = false;
fOn = false;
gOn = false;
}
}
}
void colorWipe(uint32_t c, uint16_t wait) {
for(uint16_t i=0; i<strip.numPixels(); i++) {
if (Serial.available() == 0) {
strip.setPixelColor(i, c);
strip.show();
delay(wait);
}
}
allOn = true;
}
void backwardsColorWipe(uint32_t c, uint16_t wait) {
for(uint16_t i=1 + (strip.numPixels()); i>0; i--) {
if (Serial.available() == 0) {
strip.setPixelColor(i-1, c);
strip.show();
delay(wait);
}
}
allOn = true;
}
void rainbow(uint8_t wait) {
uint16_t i, j;
for(j=0; j<256; j++) {
if (Serial.available() == 0) {
for(i=0; i<strip.numPixels(); i++) {
if (Serial.available() == 0) {
strip.setPixelColor(i, Wheel((i+j) & 255));
}
}
strip.show();
delay(wait);
}
}
allOn = true;
}
void theaterChase(uint32_t c, uint8_t wait) {
for (int j=0; j<10; j++) { //do 10 cycles of chasing
if (Serial.available() == 0) {
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
if (Serial.available() == 0) {
strip.setPixelColor(i+q, c); //turn every third pixel on
}
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
if (Serial.available() == 0) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
}
allOn = true;
}
void theaterChaseRainbow(uint8_t wait) {
for (int j=0; j < 256; j++) { // cycle all 256 colors in the wheel
if (Serial.available() == 0) {
for (int q=0; q < 3; q++) {
for (int i=0; i < strip.numPixels(); i=i+3) {
if (Serial.available() == 0) {
strip.setPixelColor(i+q, Wheel( (i+j) % 255)); //turn every third pixel on
}
}
strip.show();
delay(wait);
for (int i=0; i < strip.numPixels(); i=i+3) {
if (Serial.available() == 0) {
strip.setPixelColor(i+q, 0); //turn every third pixel off
}
}
}
}
}
allOn = true;
}
void refreshMenu()
{
Serial.println("Welcome to your computer lights");
Serial.println("For preset colors select 1-8");
Serial.println("Switch ON or OFF using 'on' or 'off' commands");
Serial.println("'rainbow' 'theater' 'theaterrainbow' also available");
terminal.println("Welcome to your computer lights");
terminal.println("For preset colors select 1-8");
terminal.println("Switch ON or OFF using 'on' or 'off' commands");
terminal.println("'rainbow' 'theater' 'theaterrainbow' also available");
terminal.flush();
}
// Input a value 0 to 255 to get a color value.
// The colours are a transition r - g - b - back to r.
uint32_t Wheel(byte WheelPos) {
WheelPos = 255 - WheelPos;
if(WheelPos < 85) {
return strip.Color(255 - WheelPos * 3, 0, WheelPos * 3);
} else if(WheelPos < 170) {
WheelPos -= 85;
return strip.Color(0, WheelPos * 3, 255 - WheelPos * 3);
} else {
WheelPos -= 170;
return strip.Color(WheelPos * 3, 255 - WheelPos * 3, 0);
}
}