C++ or python (on RPi)

https://pastebin.com/GSziWEds

/*************************************************************
  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.
 
 *************************************************************
  =>
  =>          USB HOWTO: http://tiny.cc/BlynkUSB
  =>
 
  This sketch shows how to read values from Virtual Pins
 
  App project setup:
    Slider widget (0...100) on Virtual Pin V1
 *************************************************************/
 
/* Comment this out to disable prints and save space */
#define BLYNK_PRINT SwSerial
#define TRIGGER 9
#define ECHO    10
 
#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[] = "YOUR AUTH TOKEN HERE";
 
// This function will be called every time Slider Widget
// in Blynk app writes values to the Virtual Pin V1
BLYNK_WRITE(V5)
{
  int pinValue = param.asInt(); // assigning incoming value from pin V1 to a variable
 
  // process received value
}
 
void setup()
{
  // Debug console
  SwSerial.begin(9600);
  Serial.begin(9600);
  Blynk.begin(Serial, auth);
  pinMode(TRIGGER, OUTPUT);
  pinMode(ECHO, INPUT);
 
}
 
void loop()
{
  long duration, distance;
  digitalWrite(TRIGGER, LOW);  
  delayMicroseconds(2);
 
  digitalWrite(TRIGGER, HIGH);
  delayMicroseconds(10);
 
  digitalWrite(TRIGGER, LOW);
  duration = pulseIn(ECHO, HIGH);
  distance = (duration/2) / 29.1;
  Blynk.virtualWrite(V5, distance);
  delay(200);
  Blynk.run();
 
}

i have a question, can i use this c++ code in raspberry pi? i cant find any python code to run the ultrasonic sensor with blynk apps.

Please post (formatted) code here as it is better for viewing than 3rd party links. I edited your post acordingly.

As for using that codeā€¦ well, possibly, but you will have to figure out what changes to make in order to accommodate the WiringPi methods.

Python for Blynk is in Alpha stageā€¦ but there should be no reason you canā€™t find Ultrasonic sensor code in Python (Google is your friend :wink: ), and simply add in the Blynk library and required Blynk display and control options (well, depends on your Python experience :thinking:).

You can also look at using NodeJS on the RPi with Blynk.

thank you sir! will try that later

as for now, i found the python code to use value display widget on blynk.
but the problem is when i run the code, there is an error said that ;
----> ImportError : No module named ā€˜BlynkLibā€™

how can i solve this? :frowning_face:

these are the code that i run:

import BlynkLib
import time

BLYNK_AUTH = 'YourAuthToken'

# initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

# register the task running every 3 sec
# (period must be a multiple of 50 ms)
def my_user_task():
    # do any non-blocking operations
    print('Action')
    blynk.virtual_write(2, time.ticks_ms() // 1000)

blynk.set_user_task(my_user_task, 3000)

# start Blynk (this call should never return)
blynk.run()

Did you install the library?

yes sir, i already installed the library using these command;

pip install blynk-library-python

Wellā€¦ I canā€™t help you with Python, sorry.

I created a folder, installed the library, created the test script (even though I use Local Server)ā€¦

pi@raspberrypi:~ $ cd /home/pi/Blynk_Python
pi@raspberrypi:~/Blynk_Python $ pip install blynk-library-python
Collecting blynk-library-python
Installing collected packages: blynk-library-python
Successfully installed blynk-library-python-0.1.1
pi@raspberrypi:~/Blynk_Python $ 

And the results from Thonnyā€¦

Python 3.5.3 (/usr/bin/python3)
>>> %Run UpTimeTest.py
Traceback (most recent call last):
  File "/home/pi/Blynk_Python/UpTimeTest.py", line 1, in <module>
    import BlynkLib
ImportError: No module named 'BlynkLib'
>>> 

But it is beyond meā€¦ I canā€™t seem to find where BlynkLib.py even ā€œinstallsā€ tooā€¦ in order to edit it for my Local Server useā€¦

I donā€™t have the energy to pursue this myselfā€¦ so I think I will stick with NodeJS :slight_smile:

its okay, thank you so much for the help sir! :slightly_smiling_face:

Well, I took another stab at itā€¦

Found the file BlynkLib.py in /home/pi/.local/lib/python2.7/site-packages/ (on my system )
EDIT - And on my RPi3 it appears it is in /usr/local/lib/python2.7/dist-packages

And I found that if you open a Terminal CLI, go to that folder and type python, you get the Python prompt >>>

And finally, after copy/pasting the entire example sketch at that prompt, it tries to connectā€¦ now I donā€™t have my App set for Cloud, so I edited the BlynkLib.py file for what I think wat the proper method, but now it is getting all sorts of errorsā€¦

So until I do, I recommend you try the similar method as listed above, but using the default Cloud Server.

OK, using Cloud Server I was able to connect ā€¦ and the Button on V1 shows CLI response, but nothing on the display Widget for uptime (EDIT - The widget needs to be set for 1 second reading frequency)ā€¦

But what a pain to get this far!!!.. somehow I donā€™t think this is the correct way :stuck_out_tongue:

pi@raspberrypi:~/.local/lib/python2.7/site-packages $ python
Python 2.7.13 (default, Nov 24 2017, 17:33:09) 
[GCC 6.3.0 20170516] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import BlynkLib
>>> import time
>>> 
>>> BLYNK_AUTH = '2c78d294e11xxxxxxxxxx19ac0361e2e'
>>> 
>>> # Initialize Blynk
... blynk = BlynkLib.Blynk(BLYNK_AUTH)
>>> 
>>> # Register Virtual Pins
... @blynk.VIRTUAL_WRITE(1)
... def my_write_handler(value):
...     print('Current V1 value: {}'.format(value))
... 
>>> @blynk.VIRTUAL_READ(2)
... def my_read_handler():
...     # this widget will show some time in seconds..
...     blynk.virtual_write(2, time.ticks_ms() // 1000)
... 
>>> # Start Blynk (this call should never return)
... blynk.run()
TCP: Connecting to blynk-cloud.com:80
Blynk connection successful, authenticating...
Access granted, happy Blynking!
Current V1 value: 1
Current V1 value: 0
Current V1 value: 1
Current V1 value: 0
Current V1 value: 1
Current V1 value: 0

And my ā€œfinalā€ :stuck_out_tongue: resultsā€¦

Apparently I needed to run python2.7 in order to get mine to work from the script file in a normal CLI windowā€¦

pi@raspberrypi:~ $ cd /home/pi/Blynk_Python
pi@raspberrypi:~/Blynk_Python $ python2.7 UpTimeTest.py
TCP: Connecting to blynk-cloud.com:80
Blynk connection successful, authenticating...
Access granted, happy Blynking!
Current V1 value: 1
Current V1 value: 0
Current V1 value: 1
Current V1 value: 0

Now onto trying to set it up for my Local Serverā€¦ meanwhile this little python2.7 bit should have you up and running :wink:

And for Local Server, edit the BlynkLib.py file and find this line about midway downā€¦

class Blynk:
    def __init__(self, token, server='blynk-cloud.com', port=None, connect=True, ssl=False):

ā€¦edit IP and port acordinglyā€¦

class Blynk:
    def __init__(self, token, server='xxx.xxx.xxx.xxx', port=8080, connect=True, ssl=False):

Quick addendumā€¦

Install the Blynk library with THIS command and it will install in the Python 3.x versionā€¦ then you can use Thonny to edit and run scripts. @vshymanskyy can you update your GitHub directions accordingly?

pip3 install blynk-library-python

That installed it in this directory in case you need to edit it for Local Server useā€¦

/home/pi/.local/lib/python3.5/site-packages/

2 Likes

sir! i can now run the example code. :smile:

i found this on pi forum, they said that almost all code examples you find around the internet are for the python 2.x version. Thereā€™s different ways of coding in python 3.x and some examples wont run in it that ran in 2.x.

so i tried to run emā€™ on python 2.7 instead of python 3.5 and it works just like yours.

million thanks you for your help sir! :muscle:

Good to knowā€¦ and too bad, I am liking how Thonny worksā€¦ but it seems it needs Python 3.4 and laterā€¦ guess I will try IDLE for Python 2.x

sir can i ask you few more questions?

i have these terminal widget example code run on my py 2 but there is error said;

AttributeError: 'module' object has no attribute 'dupterm'

and these are the code;

"""
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
  Social networks:            http://www.fb.com/blynkapp
                              http://twitter.com/blynk_app
This example shows how to turn a Terminal widget into
the REPL console.
In your Blynk App project:
  Add a Terminal widget, bind it to Virtual Pin V5.
  Run the App (green triangle in the upper right corner).
"""

import BlynkLib
import os

BLYNK_AUTH = 'YourAuthToken'

def hello():
    print('Welcome!')

# initialize Blynk
blynk = BlynkLib.Blynk(BLYNK_AUTH)

term = blynk.repl(5)
os.dupterm(term)

# start Blynk (this call should never return)
blynk.run()

Are you using the correct example? That doesnā€™t look like one of theseā€¦

EDIT forget it :blush: ā€¦ I see now that you are using the 2nd one :stuck_out_tongue:

I have no ideaā€¦ I am still working with the Up Time and trying to add in GPIO control of LEDs :smiley:

REPL example was targeting the WiPy hardware. It wonā€™t work in linux atm. Iā€™ll probably remove it at all :wink:

okay sir, thanks for all the help! :slightly_smiling_face:

okay sir noted.