[SOLVED] Connection between the cloud server and the local server

I use a local server for most of my projects. The server is not shared on the Internet. Sometimes I need to remotely read or control some signals from outside. Can I somehow connect my account (or signals) on the cloud server with the account (signals) on the local server?

You can send API calls from local to cloud but not the other way around unless you open up your local server to accept incoming traffic.

API - a great idea. If I can use the API, I can also use WebHook and it will work both ways. Cloud server as a quite a stranger external system for the local server. Perfect. Thank you, Costas

and solution…

5 Likes

Beautiful.

@krzyspx Very nice. After visualising it for the first time, webhooks and this API thing finally clicked in my head :stuck_out_tongue_winking_eye:

Do both accounts, being on separate servers, need to be the exact same login credentials, or will just having the valid AUTH code for a different account’s project also work?

AUTH is enough.

1 Like

Out of interest did you port forward local server or were you doing an artificial test inside your LAN?

this is BLYNK - so you have freedom :slight_smile: . AUTH do not matter

I do not really understand
my router has only standard HTTP / S ports open. Nothing special for local server. API does not need anything more.

@krzyspx I’ll put it another way.

Is the sketch for the MCU that’s connected to the cloud server using a LAN or WAN address in the API call to the local server?

Maybe WAN is not important to you.

Oh that’s it
there is only one microcontroller connected to local server and APP. These three elements are connected by LAN (WiFi). Project on CLOUD server is just a remote desktop for a real project on a local server. At home APP connects to the local server, outsite home - to the cloud server. WebHook on the local server transports data to and from the cloud server via API. I am sending only 2 - 3 most important data I want to have access from anywhere in the world.

1 Like

And a detailed description (in Polish but is a translator) how to quickly and simply connect the cloud server to the local server
http://blynk.pl/widget-api-webhook-serwer-lokalny-i-cloud-serwer-razem/

@mohan_sundaram after about 12 months of badgering our WISP we have finally got WAN access to our Blynk controlled security cameras. However this is at the expense of the router being “locked” so we only have access to ports that we specifically ask the WISP to open for us.

When we have time we will test your bash script but from a quick glance it looks to have all the right commands.

Thank you @mohan_sundaram
This is interesting solution and can be installed on any computer (not only with BLYNK installed)
Question - how often are the servers polled and can this time be set somehow?

Encountered errors in some cases of the script. I was not thinking straight.

Problems:

  1. If cloudToken is wrong and localToken is correct, the script shows tokens are ok. Should have been if either of them is “Invalid token”, must exit with an error. Corrected it by using 2 errorflags instead of one.
  2. If a pin that is not being used is tried for retrieval, the return text message has a single quote in “Requested pin doesn’t exist in the app.” This screws up the string assignment and comparison. Extracted, using cut, just the first word “Requested” for comparison.
  3. Valid values are returned either as [“0”] or [“1”] as text. Extracted the value alone using cut.

Corrected code:

#!/bin/sh
###############################################
# Written by Mohan Sundaram                   #
# To sync variables between Blynk servers -   #
# local and cloud. Needed if local WAN port   #
# does not have a Public IP and/or ports      #
# cannot be forwarded for any reason - ISP    #
# blocking ports, router admin not feasible   #
# or just lack of requisite knowhow.          #
#                                             #
# Should run on any system supporting bash    #
# Typically any variant of *NIX/Linux/BSD/OSX #
###############################################

# Assign the cloud and local IPs/tokens here
# Export from local server to cloud
# Import from cloud server to local
cloudIP="188.166.206.43"
localIP="10.0.0.20"
cloudToken="abc"
localToken="xyz"

# Variables to be exported and imported
# Please have spaces between each pin name. 
# No commas, punctuation marks or brackets.
ExportVar="V6 V7"
ImportVar="V3 V4"

#check if curl is installed and exit with error condition if not installed
command -v curl >/dev/null 2>&1 || { echo >&2 "curl not installed or found. Please install curl and make sure it is available in execution path. Aborting."; exit 1; }

# Form commands/URLs for extraction and assignment 
getcloudval="curl -sS http://$cloudIP/$cloudToken/get/$import_var"
putcloudval="http://$cloudIP/$cloudToken/update/$export_var?value=$export_value"

getlocalval="curl -sS http://$localIP/$localToken/get/$export_var"
putlocalval="http://$localIP/$localToken/update/$import_var?value=$import_value"

# Check if tokens are correct.
errortoken="Invalid token"
import_var="V1"
export_var="V1"
errorcflag=0
errorlflag=0
cloudmsg=$(getcloudval)
localmsg=$(getlocalval)

[[ $cloudmsg == $errortoken ]] && (echo "Wrong cloud token"; errorcflag=1; )
[[ $localmsg == $errortoken ]] && (echo "Wrong local token"; errorlflag=1; )
[[ $errorcflag || $errorlflag ]] && exit 0

#Start loop for import and export
errorcount=0
errormsg=“Requested”

for export_var in $ExportVar
        do
                export_value=$($getlocalval|cut -d " " -f 1|cut -d '"' -f 2)
                if [export_value==errormsg]
                then
                        errorcount=errorcount+1
                else
                        $putcloudval
                fi
        done

for import_var in $ImportVar
        do
                import_value=$($getcloudval|cut -d " " -f 1|cut -d '"' -f 2)
                if [export_value==errormsg]
                then
                        errorcount=errorcount+1
                else
                        $putlocalval
                fi
        done
echo "Import and export task completed. Errors encountered $errorcount."
exit 0
1 Like

The time setting would be done in the cron entry. This script will be run every time by cron at the matching time as per spec. Are you wanting to see if we can do that in the script itself?

On the command line, without invoking cron, you could run it every 30 seconds by:
while true; do sleep 30; /path/blynk-server-sync>>output.txt; done &

I feel safer doing from crontab than this. Don’t know how the sleep timer would load the system. This (while…do…done) can also be incorporated in the script itself after the tokens and pins have been verified.

@mohan_sundaram
I do not know Linux, but can I set CRON to run a script every 5 or 10 seconds? In order to send information, eg about a key press, data exchange times must be a few seconds, not minutes

In Linux and other *NIX systems, we have a facility called cron. This wakes up every second, reads a file where all processes that need to be started up automatically on a timed basis is entered and executes those that match. You can see this file by typing ‘contab -e’ in the prompt. Every user has a crontab and we also have a system-wide crontab owned by root.

The crontab entry will look like

          • batchfile arguments

The 5 asterisks are wildcards for seconds, minutes, hours, day of the month, month, day of the week

eg. If you give 2 in the first place, the command will be executed on the 2nd second of every minute. If you say*/20, then the command will be executed 20 times in a minute. For a deeper explanation, please see Cron and Crontab usage and examples