When I get notification from PIR sensor or smoke sensor, my arduino disconnects from the wifi and I lose the ability of using Blynk until it reconnects two minutes later

/*************************************************************
  Download latest Blynk library here:
    https://github.com/blynkkk/blynk-library/releases/latest

  Blynk is a platform with iOS and Android apps to control
  Arduino, Raspberry Pi and the likes over the Internet.
  You can easily build graphic interfaces for all your
  projects by simply dragging and dropping widgets.

    Downloads, docs, tutorials: http://www.blynk.cc
    Sketch generator:           http://examples.blynk.cc
    Blynk community:            http://community.blynk.cc
    Follow us:                  http://www.fb.com/blynkapp
                                http://twitter.com/blynk_app

  Blynk library is licensed under MIT license
  This example code is in public domain.

 *************************************************************
  This example shows how to use Adafruit CC3000 breakout
  to connect your project to Blynk.

  For this example you need Adafruit_CC3000_Library library:
    https://github.com/adafruit/Adafruit_CC3000_Library

  Note: Firmware version 1.14 or later is preferred.

  1. Update pin definitions according to your setup.
  2. Change WiFi ssid, pass, and Blynk auth token
  3. Run :)

  Feel free to apply it to any other example. It's simple!
 *************************************************************/

/* Comment this out to disable prints and save space */
#define BLYNK_PRINT Serial


// These are the interrupt and control pins for СС3000
#define ADAFRUIT_CC3000_IRQ   3
#define ADAFRUIT_CC3000_VBAT  5
#define ADAFRUIT_CC3000_CS    10

#include <SPI.h>
#include <Adafruit_CC3000.h>
#include <BlynkSimpleCC3000.h>
#include <Servo.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "7ea06fe13bc04031b22948c5164cb4d9";

// Your WiFi credentials.
// Choose wifi_sec from WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
char ssid[] = "MOP";
char pass[] = "1234567890";
int wifi_sec = WLAN_SEC_WPA2;
int inputmotion=29;
//int inputsmoke=34;
int moisturepin = A14;
int tempPin = A11;
int percentValue =0;


BLYNK_READ(V5) //Blynk app has something on V5
{
  int tempval = analogRead(tempPin);
  float temp = tempval * 0.48828125;
  Blynk.virtualWrite(V5, temp); //sending to Blynk
}

BLYNK_READ(V6) //Blynk app has something on V6
{
  int moisval = analogRead(moisturepin);
  percentValue = map(moisval, 1023, 465, 0, 100);
  if(percentValue>100)
    percentValue = 100;
  Blynk.virtualWrite(V6, percentValue); //sending to Blynk
}

Servo servo;
BLYNK_WRITE(V3)
{
  servo.write(param.asInt());
}

void setup()
{
  // Debug console
  Serial.begin(9600);
  Blynk.begin(auth, ssid, pass, wifi_sec);
  servo.attach(40);
  pinMode(inputmotion, INPUT);
//  pinMode(inputsmoke, INPUT);

}


void loop()
{
  Blynk.run();
  int motionval = digitalRead(inputmotion);  // read input value
//  int smokeval = digitalRead(inputsmoke);
  //float moistureval = analogRead(inputmoisture);
  if (motionval == HIGH) {
  Blynk.notify("motion detected");
  delay(1);
  }
//  if (smokeval == HIGH) {
 // Blynk.notify("smoke detected");
  delay(1);
 // }
  int tempval = analogRead(tempPin);
  float temp = tempval * 0.48828125;
}

  1. Format your code to make it readable
  2. Use timers. Void Loop should ideally contain only Blynk.run - this is the reason of your disconnects
  3. Also learn how to change states and don’t send “million” of notifications every second. Only send when state changes.
  4. Combining timers + states is what you need

I tried to use timers, but I couldn’t and I tried to use void loop before the set up loop but the notifications didn’t work.

Can you please fix the code(only the part of notification) and send it back

I fix code for bitcoins only :slight_smile:

I recommend you start with going through examples that are included in the Blynk library and here:
https://examples.blynk.cc

This is a great way of learning Blynk basics and main concepts. Actually, everything you ask for is covered by Blynk examples.

2 Likes

…and accept only integer numbers