Trying to send the result to BLYNK

Hi,
I am new here. My project are using arduino uno as microcontroller. My project consist of 3 features. First is space used calculation using ultrasound sensor. Second is activity monitoring using PIR sensor and last one is RFID system. I am full aware that arduino cannot run 3 program simultaneously but I am trying my best to make it work. So now bck to my problem related to the BLYNK, I am trying to send the result I get at arduino to BLYNK so that I can monitor the result remotely. I am using serial connection as medium between arduino and BLYNK server.

Details
Blynk V0.4.10
Arduino IDE 1.8.4

Coding

#define BLYNK_PRINT DebugSerial
#include <SoftwareSerial.h>
#include <Servo.h>
#include <math.h>
#include <SPI.h>
#include <RFID.h>
Servo myservo;
SoftwareSerial DebugSerial(2, 3); // RX, TX
#include <BlynkSimpleStream.h>

/*set pin number*/
//SCK   13
//MISO  12
//MOSI  11
#define SS_PIN 10   //RFID
#define RST_PIN 9   //RFID
#define SS_PIN2 8   //RFID2
#define RST_PIN2 7  //RFID2
int PIR_output = 6; //PIR
int servo = 5;      //servo
#define trigPin 4  //sonar
#define echoPin 3  //sonar

/*common declaration*/
long duration, inches, cm = 0; //sonar

int st = 0; //servo
int empty = 0, full = 0, temp = 0;
int angle = 0;
int increment = 0;
int pos;

RFID rfidIn(SS_PIN, RST_PIN); //RFID
RFID rfidOut(SS_PIN2, RST_PIN2); //RFID2
int serNum[5];
int card1[][5] = {{111, 106, 104, 161, 204}}; //rafi
int card2[][5] = {{88, 253, 100, 57, 248}}; //hakim
bool access1 = false;
bool access2 = false;
bool card1status = false;
bool card2status = false;

/*timer*/
unsigned long prevtime = 0;   //pir
unsigned long prevtime2 = 0;  //servo
unsigned long prevtime3 = 0;  //print data
unsigned long card1time = 0;  //rfid1
unsigned long card2time = 0;  //rfid2
unsigned long notallowedIn = 0;
unsigned long notallowedOut = 0;
int movements = 0;

/*declaration of constants*/
int max_distance = 30; //sonar distance in cm
int height = 30, width = 60; // size of room in cm
int timePIR = 5000; //read pir every 5 seconds
int timeSonar = 10000; //read sonar for every 10 seconds
int timePrint = 30000; //print collected data for every 30 seconds

char auth[] = "3b0fbfe2cd5241f2958742a020d4601a";

void setup()
{
  // Debug console
  DebugSerial.begin(9600);
  //intialize  
  Serial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);


  //setting for input and output
  myservo.attach(servo);
  pinMode(PIR_output, INPUT);

  //Initialize RFID
  SPI.begin();
  rfidIn.init();
  rfidOut.init();

  //calculate the starting position of servo
  angle = round( atan2 (height, width * 0.5) * 180 / 3.14159265 ); // initial position of servo
  Serial.print("Initial angle of servo is "); Serial.println(angle);
  pos = angle;
  myservo.write(90);
  delay(2500);

}

void loop()
{
  Blynk.run();
  
  //read from RFID card reader
  rfidin();
  rfidout();

  //fetching data from pir sensor
  pir();

  //fetching data from sonar sensor
  if (millis() - prevtime2 >= timeSonar)
  {
    servomove();
  }
  //Print collected data
  if (millis() - prevtime3 >= timePrint)
  {
    printdata();
    prevtime3 = millis();
  }
}

void rfidin ()
{
  if (rfidIn.isCard()) {

    if (rfidIn.readCardSerial()) {
      //      Serial.print(rfidIn.serNum[0]);
      //      Serial.print(" ");
      //      Serial.print(rfidIn.serNum[1]);
      //      Serial.print(" ");
      //      Serial.print(rfidIn.serNum[2]);
      //      Serial.print(" ");
      //      Serial.print(rfidIn.serNum[3]);
      //      Serial.print(" ");
      //      Serial.print(rfidIn.serNum[4]);
      //      Serial.println("");

      if (1)
      {
        for (int x = 0; x < sizeof(card1); x++) {
          for (int i = 0; i < sizeof(rfidIn.serNum); i++ ) {
            if (rfidIn.serNum[i] == card1[x][i]) {
              access1 = true;
            } else {
              access1 = false;
              break;
            }
          }
          if (access1) break;
        }
      }

      else if (access1 == false)
      {
        for (int x = 0; x < sizeof(card2); x++) {
          for (int i = 0; i < sizeof(rfidIn.serNum); i++ ) {
            if (rfidIn.serNum[i] == card2[x][i]) {
              access2 = true;
            }
            else {
              access2 = false;
              break;
            }
          }
          if (access2) break;
        }
      }

      if (access1 && card1status == false) {
        Serial.println("Welcome Rafi!");
        card1time = millis();
        card1status = true;
      } else if (access2 && card2status == false) {
        Serial.println("Welcome Hakim!");
        card2time = millis();
        card2status = true;
      }
      else {
        if (millis() - notallowedIn >= 1000)
        {
          Serial.println("Not allowed!");
          notallowedIn = millis();
        }
      }
    }
    rfidIn.halt();
  }
}

void rfidout ()
{
  if (rfidOut.isCard()) {

    if (rfidOut.readCardSerial()) {
      //      Serial.print(rfidOut.serNum[0]);
      //      Serial.print(" ");
      //      Serial.print(rfidOut.serNum[1]);
      //      Serial.print(" ");
      //      Serial.print(rfidOut.serNum[2]);
      //      Serial.print(" ");
      //      Serial.print(rfidOut.serNum[3]);
      //      Serial.print(" ");
      //      Serial.print(rfidOut.serNum[4]);
      //      Serial.println("");

      if (1)
      {
        for (int x = 0; x < sizeof(card1); x++) {
          for (int i = 0; i < sizeof(rfidOut.serNum); i++ ) {
            if (rfidOut.serNum[i] == card1[x][i]) {
              access1 = true;
            } else {
              access1 = false;
              break;
            }
          }
          if (access1) break;
        }
      }

      else if (access1 == false)
      {
        for (int x = 0; x < sizeof(card2); x++) {
          for (int i = 0; i < sizeof(rfidOut.serNum); i++ ) {
            if (rfidOut.serNum[i] == card2[x][i]) {
              access2 = true;
            } else {
              access2 = false;
              break;
            }
          }
          if (access2) break;
        }
      }
    }

    if (access1 && card1status == true)
    {
      Serial.println("Good Bye Rafi!");
      card1time = millis() - card1time;
      float t1 = card1time * 0.001;
      Serial.print("Your total time in room: ");
      Serial.print(t1);
      Serial.println(" seconds");
      card1status = false;
    }
    else if (access2 && card2status == true)
    {
      Serial.println("Good Bye Hakim!");
      card2time = millis() - card2time;
      float t2 = card2time * 0.001;
      Serial.print("Your total time in room: ");
      Serial.print(t2);
      Serial.println(" seconds");
      card2status = false;
    }
    else {
      if (millis() - notallowedOut >= 1000)
      {
        Serial.println("Not allowed!");
        notallowedOut = millis();
      }
    }
  }
  rfidOut.halt();

}

void servomove()
{
  //direction of servo
  if (pos == angle)
    st = 0;

  if (pos == (180 - angle))
    st = 1;

  //moving the servo
  if (st == 0) {
    pos = pos + 1;
  }
  if (st == 1) {
    pos = pos - 1;
  }
  myservo.write(pos);

  //fething distance data from sonar
  data();

  //print servo position
  Serial.print(pos); Serial.print( " "); Serial.println(cm);
  delay(10);

  //calculating the percentage of usage of the room
  if (cm >= max_distance)
  {
    empty++;
  }
  else if (cm < max_distance)
  {
    full++;
  }
  if (pos == angle || pos == (180 - angle))
  {
    float value;
    float y;
    y = 180 - angle - angle;
    value = (full / y * 100);
    Blynk.virtualWrite(V5, value);
    Serial.print(value); Serial.println("% space used");
    Serial.print("Servo stop at "); Serial.println(pos);

    prevtime2 = millis();

    //print current movement index
    Serial.print("Movement index: ");
    Serial.println(movements);
  }

  if (temp != st)
  {
    empty = 0;
    full = 0;
  }
  temp = st;
}

void printdata()
{
  //print accumulated movement index
  Serial.print("Total movement index = ");
  Serial.println(movements);
  movements = 0;
}

void pir()
{
  if (digitalRead(PIR_output) == HIGH)
  {
    movements++;
  }
}

void data()
{
  pinMode(trigPin, OUTPUT);
  digitalWrite(trigPin, LOW);
  delayMicroseconds(2);
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(5);
  digitalWrite(trigPin, LOW);
  pinMode(echoPin, INPUT);
  duration = pulseIn(echoPin, HIGH);
  // convert the time into a distance
  inches = microsecondsToInches(duration);
  cm = microsecondsToCentimeters(duration);
  if (cm > max_distance)
  {
    cm = max_distance;
  }
}

//funtion used for sonar coding
long microsecondsToInches(long microseconds)
{
  return microseconds / 74 / 2;
}

long microsecondsToCentimeters(long microseconds)
{
  return microseconds / 29 / 2;
}

Welcome, did you forget to ask the question?

3 Likes

Oh ya sorry… my question is how can I merge the blynk coding with mine… i tried and i got result like in the picture i attached on my thread.

DSR OFF means “good to go”.

Blynk coding is not a single thing you just merge with something… it is an App, server, library combination that you first need to learn how it works, then you can take other code, break it down into manageable, timed, chunks and integrate it all.

Best we can do is recommend you start reading the Documentation, Help Files and start working with the Examples (all those links are at the upper right of this page) until you reach that understanding stage.

We can help answer questions as you learn, but we can’t take your massive code and Blynkify it for you.

2 Likes

I got “Device offline” on my android.
Im wonder what is wrong.

Thank you for your respond.
I did try the example and manage to do it successfully. I try merge the coding with mine ( you can see the coding i attach) and it didnt work. I am sorry if you think i am trying to get help without any effort to do it by myself.

with this info, no one can figure out…

what didn’t work?
what is the expected outcome and what is the actual result?
any error messages?
any serial monitor output?
???

1 Like

I never said that :slight_smile: But based on deduction and your limited time reading posts in this forum, I surmised that you have very little Blynk experience, so I was simply recommending some direction… Start with the basics and get really comfortable with how Blynk works before trying a larger merger like this… voice of experience here, take it or leave it :wink:

what didn’t work?
The value I want to push is not coming out at the apps.
what is the expected outcome and what is the actual result?
expected result is i can monitor the system at blynk app
any error messages?
no error during compiling
any serial monitor output?
the serial output shows my auth token

Currently, I solved all the problem above. I deleted all Serial.println command and the Blynk worked!
I use serial usb to connect my arduino to Blynk server. So now I want to improve it.

My question is can I add esp8266 as wifi module to my arduino and send the data through it? If yes can you suggest the sketch I must use to build the program in the esp?

I appreciate your help.

Thanks

Okay thanks

Thank you for your info. Really helpful. I didnt notice it before as the system keep looping at server connecting to blynk server and disconnect. I thought that was error message. I already solved the problem. Thanks man!

yes, but it is not a beginner level. you can find lots of topics regarding how others do this, on the forum.

if you do not have any serious reason to use the arduino, better use just the esp alone. it is much more transparent and easier to debug.

I am reading this http://help.blynk.cc/how-to-connect-different-hardware-with-blynk/arduino/esp8266-with-at-firmware

It seems like arduino uno did not support it. I am using arduino uno.

if you do not have any serious reason to use the arduino, better use just the esp alone. it is much more transparent and easier to debug.

I tried transfer my coding into esp wroom. I changed the output according to the board but did not working. From serial message I got symbol language. I go back to arduino uno as thats the only way I can do this.

Maybe I just stick to serial connection if connecting esp is too hard for me.

sending data from uno thorough esp is more complicated than transplanting existing code from uno to esp. just my 2 cents…

i’m not sure what you mean by this, but if you referring to unintelligible characters, than thats is most often a mismatched baud rate.

Yes, that is a common mistake with the USB-link… people forget that the server link is used by the programming/serial port, so NO serial prints can be used at the same time.

UNO will work fine with an ESP as WiFi Adapter… typically an ESP-01 is used for that purpose. The document just shows a different Arduino board, but the principle is the same.

Other “stand alone” ESP8266 boards like the NodeMCU or Wemos D1 Mini will work better all by themselves because they have a programming port and more GPIO pins, thus can be programmed directly in the Arduino IDE and can hook up to sensors etc.

Keep reading, testing and plugging away at it, and you will catch on in time :smiley: I have been working with Arduinos for years and I am still learning new things.

I am aware that esp use 115200 baudrate and my uno use 9600 baudrate.
I tried upload 9600 baudrate program from example into esp and run it for troubleshoot. No problem appeared.

When I try uploading my project program into esp i got that unintelligible characters. Then I go back to Uno.

UNO will work fine with an ESP as WiFi Adapter

I got D1 mini and NodeMCU board… it is so easy to use. can straight away flash the firmware and use it.
I will give a try to connect my UNO to ESP.
Thanks for your support.

1 Like