Your sketch will not work without any libraries. Try the following as per @Lichtsignaal’s sketch.
#include <BlynkSimpleSerial.h>
Your sketch will not work without any libraries. Try the following as per @Lichtsignaal’s sketch.
#include <BlynkSimpleSerial.h>
Costas, the #include <BlynkSimpleSerial.h> is included in the script, but when I copied to the reply it does not show up.
Paste the sketch again and then press the </> icon.
#include <BlynkSimpleSerial.h>
char auth[] = “9107e9542b144af18db0bxxxxxxxxxx”;
void setup()
{
Serial.begin(9600);
Blynk.begin(auth);
}
void loop()
{
Blynk.run();
}
indent preformatted text by 4 spaces
I am still unable to get my Arduino Mega (hooked up via USB to a Raspberry Pi2) to connect to Blynk. I have done all the troubleshooting indicated above and still no results. Any help will be appreciated.
Obviously, it would be simple to just add a wifi shield to the Arduino and connect that way, but I have stuff running on my setup that I don’t think Blynk will handle and still need to rely on the R Pi for processing and output of that info.
@lfbaron ok I have hooked up my Nano to my Pi to help you out.
This is the script file that is working for me on my Model B Pi having installed socat on the device:
pi@raspberrypi:~/blynk-library/scripts $ cat blynk-ser.sh
#!/bin/bash
# Detect script path
pushd `dirname $0` > /dev/null
SCRIPTPATH=`pwd`
popd > /dev/null
# === Edit default options to match your need ===
FROM_TYPE="SER" # SER, TCP
TO_TYPE="SSL" # TCP, SSL
COMM_PORT_LINUX=/dev/ttyUSB0
COMM_PORT_OSX=/dev/tty.usbmodem
COMM_BAUD=9600
SERV_ADDR=cloud.blynk.cc
SERV_PORT_SSL=8441
SERV_PORT_TCP=8442
SERV_PORT_2WAY=8443
LSTN_PORT=8442
SRVR_CERT="$SCRIPTPATH/certs/server.crt"
CLNT_CERT="$SCRIPTPATH/certs/client.pem"
# === Edit the lines below only if absolutely sure what you're doing ===
# Setup exit handler
trap "echo Exited!; exit;" SIGINT SIGTERM
echo [ Press Ctrl+C to exit ]
usage="
This script redirects serial communication to the server.
You can specify port, baud rate, and server endpoint like this:
blynk-ser.sh -c <serial port> -b <baud rate> -s <server address> -p <server port>
The defaults are:
-c,--comm /dev/ttyUSB0 (on Linux)
COM1 (on Windows)
/dev/tty.usbserial (on OSX)
-b,--baud 9600
-s,--server cloud.blynk.cc
-p,--port 8442
If the specified serial port is not found, it will ask to enter another one.
The script also tries to reestablish connection if it was lost.
"
avrdude_warn="Warning: avrdude is running
Avoid uploading sketches and running this script at the same time.
If you didn't run avrdude, maybe it just stuck. You can kill it using:
killall avrdude
"
function detect_conflicts {
# Detect if avrdude is running
if pgrep avrdude; then echo -n "$avrdude_warn"; fi
}
detect_conflicts
# Detect socat
if ! hash socat 2>/dev/null; then
echo "This script uses socat utility, but could not find it."
echo
if [[ "$OSTYPE" != "linux-gnu" ]]; then
echo " Try installing it using: sudo apt-get install socat"
elif [[ "$OSTYPE" == "darwin"* ]]; then
echo " Try installing it using this guide: http://www.xappsoftware.com/wordpress/2013/10/10/how-to-run-socat-on-mac-os-x/"
fi
exit 1
fi
# Execute getopt
ARGS=$(getopt -o hf:c:b:l:t:s:p: -l "help,from:,comm:,baud:,listen:,to:,server:,port:,cert:" -n "blynk-gateway.sh" -- "$@");
# Bad arguments
if [ $? -ne 0 ];
then
echo -n "$usage"
exit 1
fi
eval set -- "$ARGS";
while true; do
case "$1" in
-h|--help)
shift
echo -n "$usage"
exit 0
;;
####
-f|--from)
shift
if [ -n "$1" ]; then
FROM_TYPE=$1
shift
fi
;;
-c|--comm)
shift
if [ -n "$1" ]; then
COMM_PORT_LINUX=$1
COMM_PORT_OSX=$1
shift
fi
;;
-b|--baud)
shift
if [ -n "$1" ]; then
COMM_BAUD=$1
shift
fi
;;
-l|--listen)
shift
if [ -n "$1" ]; then
LSTN_PORT=$1
shift
fi
;;
####
-t|--to)
shift
if [ -n "$1" ]; then
TO_TYPE=$1
shift
fi
;;
-s|--server)
shift
if [ -n "$1" ]; then
SERV_ADDR=$1
shift
fi
;;
-p|--port)
shift
if [ -n "$1" ]; then
SERV_PORT_SSL=$1
SERV_PORT_TCP=$1
SERV_PORT_2WAY=$1
shift
fi
;;
--cert)
shift
if [ -n "$1" ]; then
SRVR_CERT=$1
shift
fi
;;
####
--)
shift;
break;
;;
esac
done
# Construct command
GEN_ATTR="-d -d"
TCP_ATTR="nodelay" #,nonblock=1,rcvtimeo=1,sndtimeo=1
SER_ATTR="raw,echo=0,clocal=1,cs8,nonblock=1"
if [[ "$FROM_TYPE" == "SER" ]]; then
if [[ "$OSTYPE" != "linux-gnu" ]]; then
COMM_PORT=$COMM_PORT_LINUX
COMM_WCARD="/dev/ttyUSB* /dev/ttyACM*"
COMM_STTY="-F"
elif [[ "$OSTYPE" == "darwin"* ]]; then
COMM_PORT=$COMM_PORT_OSX
COMM_WCARD="/dev/tty.usbserial* /dev/tty.usbmodem*"
COMM_STTY="-f"
else
echo "Can't detect OS type!"
exit 1
fi
# Ask for serial port interactively if not found
if [ ! -e "$COMM_PORT" ]; then
echo $COMM_PORT not found.
echo -n "Select serial port [" `ls $COMM_WCARD 2> /dev/null` "]: "
read COMM_PORT
fi
echo Resetting device $COMM_PORT...
stty $COMM_STTY $COMM_PORT hupcl
# Disable restarting
#stty $COMM_STTY $COMM_PORT -hupcl
if [[ "$OSTYPE" != "linux-gnu" ]]; then
FROM_ATTR="FILE:$COMM_PORT,$SER_ATTR,b$COMM_BAUD"
elif [[ "$OSTYPE" == "darwin"* ]]; then
FROM_ATTR="GOPEN:$COMM_PORT,$SER_ATTR,ixoff=0,ixon=0,ispeed=$COMM_BAUD,ospeed=$COMM_BAUD,crtscts=0"
fi
elif [[ "$FROM_TYPE" == "TCP" ]]; then
FROM_ATTR="TCP-LISTEN:$LSTN_PORT,reuseaddr,fork,$TCP_ATTR"
else
echo "$FROM_TYPE is not supported."
exit 1
fi
if [[ "$TO_TYPE" == "TCP" ]]; then
echo "Warning: Server connection may be insecure!"
TO_ATTR="TCP:$SERV_ADDR:$SERV_PORT_TCP,$TCP_ATTR"
elif [[ "$TO_TYPE" == "SSL" ]]; then
if [ -e $SRVR_CERT ]; then
TCP_ATTR="cafile=$SRVR_CERT,$TCP_ATTR"
else
echo "Warning: $SRVR_CERT not found. Skipping server verification (connection may be insecure)!"
TCP_ATTR="verify=0,$TCP_ATTR"
fi
if [ -e $CLNT_CERT ]; then
TCP_ATTR="cert=$CLNT_CERT,$TCP_ATTR"
fi
TO_ATTR="openssl-connect:$SERV_ADDR:$SERV_PORT_SSL,$TCP_ATTR"
elif [[ "$TO_TYPE" == "2WAY" ]]; then
echo "2WAY is not supported yet."
exit 1
else
echo "$TO_TYPE is not supported."
exit 1
fi
while [ 1 ]; do
echo Connecting: "$FROM_ATTR <-> $TO_ATTR"
socat $GEN_ATTR $FROM_ATTR $TO_ATTR
detect_conflicts
echo Reconnecting in 3s...
sleep 3
done
When I run this script the Pi gives:
[ Press Ctrl+C to exit ]
Resetting device /dev/ttyUSB0...
Connecting: FILE:/dev/ttyUSB0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 <-> openssl-connect:cloud.blynk.cc:8441,cafile=/home/pi/blynk-library/scripts/certs/server.crt,nodelay
2016/02/26 22:14:36 socat[960] N opening character device "/dev/ttyUSB0" for reading and writing
2016/02/26 22:14:36 socat[960] N opening connection to AF=2 45.55.195.102:8441
2016/02/26 22:14:36 socat[960] N successfully connected from local address AF=2 192.168.10.98:57611
2016/02/26 22:14:37 socat[960] N SSL connection using ECDHE-RSA-AES128-GCM-SHA256
2016/02/26 22:14:37 socat[960] N SSL connection compression "none"
2016/02/26 22:14:37 socat[960] N SSL connection expansion "none"
2016/02/26 22:14:37 socat[960] N starting data transfer loop with FDs [5,5] and [6,6]
I can’t tell you at the moment what sketch I have on the Nano other than when I access it via Arduino Serial Monitor is throws up my token every 5 to 10 seconds. This is what it should be doing and you should perhaps check this first with your Mega. If it is is repeating your token at intervals we can assume your Mega is ok.
Try my script with the change for your USB port and let us see your terminal window feedback.
I think I am making progress, but not there yet.
I now get the following:
Connecting: FILE:/dev/ttyACM0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 <-> TCP:cloud.blynk.cc:8442,nodelay
2016/02/26 15:26:19 socat[3360] N opening character device “/dev/ttyACM0” for reading and writing
2016/02/26 15:26:20 socat[3360] N opening connection to AF=2 45.55.195.102:8442
2016/02/26 15:26:20 socat[3360] N successfully connected from local address AF=2 192.168.1.91:57380
2016/02/26 15:26:20 socat[3360] N starting data transfer loop with FDs [3,3] and [4,4]
2016/02/26 15:26:34 socat[3360] N socket 2 (fd 4) is at EOF
this last line repeats multiple times and then ends with:
2016/02/26 15:26:34 socat[3360] E write(4, 0xc1a778, 7): Broken pipe
2016/02/26 15:26:34 socat[3360] N exit(1)
Reconnecting in 3s…
On the app’s end, I am getting the offline message intermittently only, but it does not load any data
What does the Mega say in Serial Monitor?
Costas,
Serial Monitor shows my data stream and the token being feed periodically.
I used your script changing the USB port and I got:
Reconnecting in 3s…
Connecting: FILE:/dev/ttyACM0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 <-> openssl-connect:cloud.blynk.cc:8441,cafile=/home/pi/sketchbook/libraries/Blynk/scripts/certs/server.crt,nodelay
2016/02/26 15:51:46 socat[5519] N opening character device “/dev/ttyACM0” for reading and writing
2016/02/26 15:51:46 socat[5519] N opening connection to AF=2 45.55.195.102:8441
2016/02/26 15:51:46 socat[5519] N successfully connected from local address AF=2 192.168.1.91:53980
2016/02/26 15:51:46 socat[5519] N SSL connection using ECDHE-RSA-AES128-GCM-SHA256
2016/02/26 15:51:46 socat[5519] N starting data transfer loop with FDs [3,3] and [4,4]
2016/02/26 15:51:46 socat[5519] E read(3, 0x11c1cc8, 8192): Resource temporarily unavailable
2016/02/26 15:51:46 socat[5519] N exit(1)
Reconnecting in 3s…
Then I changed TO_TYPE SSL to TCP so that I try to access 8442 and get:
Connecting: FILE:/dev/ttyACM0,raw,echo=0,clocal=1,cs8,nonblock=1,b9600 <-> TCP:cloud.blynk.cc:8442,nodelay
2016/02/26 15:54:44 socat[6118] N opening character device “/dev/ttyACM0” for reading and writing
2016/02/26 15:54:44 socat[6118] N opening connection to AF=2 45.55.195.102:8442
2016/02/26 15:54:44 socat[6118] N successfully connected from local address AF=2 192.168.1.91:57491
2016/02/26 15:54:44 socat[6118] N starting data transfer loop with FDs [3,3] and [4,4]
2016/02/26 15:54:44 socat[6118] E read(3, 0x1884778, 8192): Resource temporarily unavailable
2016/02/26 15:54:44 socat[6118] N exit(1)
Reconnecting in 3s…
Think it is because the usb port is not in the dialout group.
In the Pi console type:
ls -la /dev/ttyACM0
What does it say?
I get:
crw-rw—T 1 root dialout 166, 0 Dec 31 1969 /dev/ttyACM0
Are you running the script with sudo?
Yes, I am running it with sudo
Can you try:
or
You will need to reboot the Pi or logout and log back in if you go with adding your Pi username to the dialout group.
I tried both and still get the errors per above post. I am sure there is a way to get it to work. Just did not think it would be this much trouble.
If you search the socat github at https://github.com/craSH/socat/blob/master/doc/xio.help there are 2 references to “Resource temporarily unavailable” (rows 600 and 626).
It relates to file (or maybe device) locking but it doesn’t mean much to me.
Last August @Dmitriy related the issue to file descriptors (whatever they are) in this thread Resource temporarily unavailable (USB)
Please also check that you’re using the last version from the master branch!
I had to setup my Raspberry Pi from scratch (damaged SD card) and had a lot of difficulty getting Blynk back to work using Arduino and the RPi. After a lot of searching I found a simple solution and thought I would post, as it may help others. These are the steps to finally make it work via the Raspberry Pi: