简体   繁体   中英

Update (download files from server) Raspberry Pi 3

I'm trying to download files from another server to RPI and run command on RPI server with exec command to update data on RPI.

My first though was to check if server has some new versions with xhr request and then to download them with xhr request to a file I want with use of RPI server but couldn't get it work.

So is there any way to download some files to RPI and then use them with exec on RPI server (sudo move or something like taht)?

Found a way. I'm using node.js with child_process to execute command to RPI. The command I use to download something from a server is wget http://ipaddress:port/path_to_file/

If You want to put it in specific folder You have to open it with cd path_to_folder

Whole code:

var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require('socket.io')(http);
var exec = require('child_process').exec;
var request = require('request');

http.listen(6669);

io.on('connection', function(socket){
    socket.on("Update",function(){
         update();
    }
}

//////////FUNCTIONS//////////
function update() {
    try {
         console.log("Start updating!");
         if(checkServerFile('http://ip_address:port/path_to_file')){
                console.log("Starting download...");
                execute('cd /home/pi/server/update/ && sudo wget http://ip_address:port/update/update.sh && sudo bash /home/pi/server/update/update.sh');
         }      
    } 
    catch (err) {
         console.log(err);
    }
}

function checkServerFile(path){
    var result = false;
    result = request(path, function(err, resp){
         if(resp.statusCode === 200){
                return true;
         } 
    });
    console.log(result);
    return result;
}

function execute(command) {
    var cmd = exec(command, function(error){
         console.log("error: ", error);
    });
}

Function

checkServerFile

checks if the file exists on the server

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM