Smartphone sensor widgets to produce serial data?

Hi I am a new to Blynk, and have been trying out a personal project.

The goal is to extract smartphone sensor datas (e.g. gravity sensor) through the blynk sensor widget and to translate them into serial data read by the arduino and then passed on to to another software like TouchDesigner/Unity for other purposes.

There are 3 things I am confused about…

  1. How to extract the smartphone sensor data. I am aware of the information from http://docs.blynk.cc/#widgets-sensors-gravity page but have been unable to successfully extract them? Is there perhaps an example code I could refer to?
  2. How to reflect them into serial data, I am aware of the terminal print http://docs.blynk.cc/#widgets-displays-terminal
  3. Reflected in the app USB wasn’t online yet. I have successfully carried out the tutorial on how to use a pushbutton to control an LED on the arduino. But strangely even though the command prompt is left open for the reading of the smartphone sensor project, it just reflects as the USB wasn’t online. Have refreshed the code, checked, closed the serial monitor in arduino etc.

Hardware: Arduino UNO
Communication: USB (am using windows 10)
Smartphone: Android, vers 2.27.12

Sorry for the lengthy post! But please do point me in any direction possible! Am really stumped with this ): Thank you in advance!

Here is the code I mishmashed with my limited coding ability…

#define BLYNK_PRINT Serial


#include <SoftwareSerial.h>
SoftwareSerial SwSerial(10, 11); // RX, TX

#include <BlynkSimpleStream.h>

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

BLYNK_WRITE(V0) {
  //force of gravity applied to axis x
  int x = param[0].asFloat(); 
terminal.println(param[0].asFloat()); //x
  //force of gravity applied to axis y
  int y = param[1].asFloat();
terminal.println(param[1].asFloat()); //y
  //force of gravity applied to axis y
  int z = param[2].asFloat();
  terminal.println(param[2].asFloat()); //z

  terminal.flush();
}


void setup()
{
  // Debug console
  Serial.begin(9600);
  // Clear the terminal content
  terminal.clear();

  terminal.flush();
}

void loop()
{
  Blynk.run();
}

-Shin

There are some very important bits of your code missing, such as the bit that initialises the SwSerial port at your desired baud rate, and the bit that initialises your Blynk connection. Both should be in your void setup.

Pete.

Hi Pete!

Thanks for the tips, I have tried including the SwSerial bit and upon further research, I stumbled onto https://community.blynk.cc/t/blynk-serial-communication/19091 and https://community.blynk.cc/t/issues-using-usb-link-dsr-is-off-project-is-offline/17320/3. I realised that by using SwSerial, it causes my project to go offline on my phone app. Hence, I modified my code to use DebugSerial instead.

I managed to get the terminal up, it prints my test line of ‘nyoom’ however it does not print values from the phone’s gravity sensor, neither does it print at the timer intervals… Am still confused on how to read values from the phone sensor!

For now, I am testing the readings from the sensor by using the terminal print in the app. But eventually I will attempt to get the readings to print in the serial monitor of the arduino please advise if this is possible using Blynk?

Here is my updated code and thanks in advance!

#define BLYNK_PRINT DebugSerial

//#define BLYNK_PRINT Serial //uses software serial
//SoftwareSerial SwSerial(10, 11); // RX, TX (transmit and receive respectively)

#include <SoftwareSerial.h>
SoftwareSerial DebugSerial(0, 1); //Arduino uno board has one serial port at D0(RX) and D1(TX)
#include <BlynkSimpleStream.h>

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

WidgetTerminal terminal (V1);
BlynkTimer timer; 
int x;
int y;
int z;

BLYNK_WRITE(V0) { 
  int x = param[0].asFloat();   //force of gravity applied to axis x
  int y = param[1].asFloat();  //force of gravity applied to axis y
  int z = param[2].asFloat();  //force of gravity applied to axis z
}

BLYNK_WRITE(V1) {
  {
  terminal.println(param[0].asFloat()); //x
  terminal.println(param[1].asFloat()); //y
  terminal.println(param[2].asFloat()); //z
  }
  terminal.flush(); //ensure everything is sent
}

void setup()
{
  // Debug console
  //SwSerial.begin(9600); uses software serial
  Serial.begin(9600);
  Blynk.begin(Serial, auth); 
  terminal.clear(); //clear terminal content
  terminal.println(F("nyoom")); //test line - check if terminal is up and working
  terminal.flush(); //ensure everything is sent
  timer.setInterval(1000L, x);  //function "BLYNK_WRITE(V1) for x" runs every 1000ms = 1s)
}

void loop()
{
  Blynk.run();
  timer.run(); 
}

-Shin

This is delling your code to run the function called void x() once every 1000 milliseconds.
You don’t have a function called void x()

What widgets do you have attached to pins V0 and V1 in the app?
Can you post screenshots of how you’ve configured these widgets (in stop mode)?

Pete.

I’ve got the gravity sensor on V0 and terminal on V1

The idea of putting

timer.setInterval(1000L, x);

is such that

BLYNK_WRITE(V1)
{
  terminal.println(param[0].asFloat()); //x
  terminal.println(param[1].asFloat()); //y
  terminal.println(param[2].asFloat()); //z
  }
  terminal.flush(); //ensure everything is sent
}

will print the value of ‘x’ every 1000ms - which is the force of gravity applied to the axis x.
I hope this clarifies my intention

-Shin

For this to work you would need to change BLYNK_WRITE(V1) to BLYNK_WRITE(V0) and remove the timer from your void setup.

BLYNK_WRITE(V0) is a callback function that triggers each time the server receives a new set of data from the widget attached to pin V0. As this is where your gravity sensor is attached, this is the callback that is required, not a callback on V1.

If I was doing this type of testing I would assign the x,y and z values to variables, then send these variables to the terminal widget, as the combined “get the value and send it directly to the terminal” approach that you are using isn’t always able to be evaluated correctly.

Pete.

Hi Pete,

Thank you for clarifying, I get it now!! The BLYNK_WRITE(Vx) section really confused me because I was unsure which to callback to get the right values to print.
I managed to get the terminal on the app printing the desired values! :smiley:

However, when I attempted to get the prints on my arduino’s serial monitor by adding back Serial.println(‘xvalue’) value, the app reflected that the project was offline again… Is it not possible to concurrently get a serial print (on my computer) and terminal print (on my app) while using a USB connection?

-Shin

That’s correct. The USB serial connection to the Blynk server needs to be used exclusively for Blynk communications - you can’t use it for anything else.
This is why many code examples use SoftwareSerial to create an additional port that can be used for debugging. However, for your PC to be able to use this port it will need a TTL to USB serial converter, often known as an FTDI adapter.

In reality though, the USB serial connection method is only really useable as a way to get people up and running to ‘play’ with Blynk if all they have is an Uno with no other method of connecting to the internet. Your long term solution should be to abandon this connection method and go for a proper IoT device…

Pete.

1 Like

Hi Pete,

I see!! Now I understand how come the SoftwareSerial is unable to run concurrently with the USB serial connection… Thank you so much for the awesome tips and guidance ~ I’ll look into a board upgrade at my local store after further research to continue my project :D!

-Shin