marcoschwartz / aREST

A RESTful environment for Arduino
http://aREST.io/
Other
1.2k stars 279 forks source link

IP:Port #254

Open michi1g opened 6 years ago

michi1g commented 6 years ago

Hi! I'm developing some wifi robot for a project. Unfortunately, I ran into a problem I don't know how to solve. My esp8266 is running a Webserver (Port 80) and the aRest Server (Port 8080). I have a script in my index.html (sorry, code tag didn't work properly):

$( document ).ready(function() { Device var address = ""; var device = new Device(address);

function tuWas() { //alert ('Event-Handler wurden hinzugefügt!'); console.log("juhuu"); device.callFunction("vor"); } function tuWas2() { //alert ('Event-Handler wurden hinzugefügt!'); console.log("yeeha"); device.callFunction("stop"); } var elem = document.getElementById('right'); elem.addEventListener ('touchstart', tuWas); elem.addEventListener ('touchend', tuWas2); });

I have jquery 2.1.4 running. Where do I have to put the Port? when I enter something in var address, it just adds up after the ip: address = "" ->http://192.168.4.1/vor?params=undefined address = ":8080" -> http://192.168.4.1/:8080/vor?params=undefined is there a way to solve this? I've tried for several hours now...

aRest.js

//Device definition function Device(address) { this.address = address; this.pinMode = function(pin, state) { $.ajaxq('queue', { url: 'http:' + this.address + '/mode/' + pin + '/o', crossDomain: true }).done(function(data) { console.log(data); }); };

this.digitalWrite = function(pin, state) { $.ajaxq('queue', { url: 'http:' + this.address + '/digital/' + pin + '/' + state, crossDomain: true }).done(function(data) { console.log(data); }); };

this.analogWrite = function(pin, state) { $.ajaxq('queue', { url: 'http:' + this.address + '/analog/' + pin + '/' + state, crossDomain: true }).done(function(data) { console.log(data); }); };

this.analogRead = function(pin, callback) { $.ajaxq('queue', { url: 'http:' + this.address + '/analog/' + pin, crossDomain: true }).done(function(data) { callback(data); }); };

this.digitalRead = function(pin, callback) { $.ajaxq('queue', { url: 'http:' + this.address + '/digital/' + pin, crossDomain: true }).done(function(data) { callback(data); }); };

this.getVariable = function(variable, callback) { $.ajaxq('queue', { url: 'http:' + this.address + '/' + variable, crossDomain: true }).done(function(data) { callback(data); }); };

this.callFunction = function(called_function, parameters, callback) { $.ajaxq('queue', { url: 'http:' + this.address + '/' + called_function + '?params=' + parameters, crossDomain: true }).done(function(data) { if (callback != null) {callback(data);} }); }; }

ajaxq.js `

// AjaxQ jQuery Plugin // Copyright (c) 2012 Foliotek Inc. // MIT License // https://github.com/Foliotek/ajaxq // Uses CommonJS, AMD or browser globals to create a jQuery plugin.

(function (factory) { if (typeof define === 'function' && define.amd) { // AMD. Register as an anonymous module. define(['jquery'], factory); } else if (typeof module === 'object' && module.exports) { // Node/CommonJS module.exports = factory(require('jquery')); } else { // Browser globals factory(jQuery); } }(function ($) { var queues = {}; var activeReqs = {};

// Register an $.ajaxq function, which follows the $.ajax interface, but allows a queue name which will force only one request per queue to fire.
// opts can be the regular $.ajax settings plainObject, or a callback returning the settings object, to be evaluated just prior to the actual call to $.ajax.
$.ajaxq = function(qname, opts) {

    if (typeof opts === "undefined") {
        throw ("AjaxQ: queue name is not provided");
    }

    // Will return a Deferred promise object extended with success/error/callback, so that this function matches the interface of $.ajax
    var deferred = $.Deferred(),
        promise = deferred.promise();

    promise.success = promise.done;
    promise.error = promise.fail;
    promise.complete = promise.always;

    // Check whether options are to be evaluated at call time or not.
    var deferredOpts = typeof opts === 'function';
    // Create a deep copy of the arguments, and enqueue this request.
    var clonedOptions = !deferredOpts ? $.extend(true, {}, opts) : null;
    enqueue(function() {
        // Send off the ajax request now that the item has been removed from the queue
        var jqXHR = $.ajax.apply(window, [deferredOpts ? opts() : clonedOptions]);

        // Notify the returned deferred object with the correct context when the jqXHR is done or fails
        // Note that 'always' will automatically be fired once one of these are called: http://api.jquery.com/category/deferred-object/.
        jqXHR.done(function() {
            deferred.resolve.apply(this, arguments);
        });
        jqXHR.fail(function() {
            deferred.reject.apply(this, arguments);
        });

        jqXHR.always(dequeue); // make sure to dequeue the next request AFTER the done and fail callbacks are fired

        return jqXHR;
    });

    return promise;

    // If there is no queue, create an empty one and instantly process this item.
    // Otherwise, just add this item onto it for later processing.
    function enqueue(cb) {
        if (!queues[qname]) {
            queues[qname] = [];
            var xhr = cb();
            activeReqs[qname] = xhr;
        }
        else {
            queues[qname].push(cb);
        }
    }

    // Remove the next callback from the queue and fire it off.
    // If the queue was empty (this was the last item), delete it from memory so the next one can be instantly processed.
    function dequeue() {
        if (!queues[qname]) {
            return;
        }
        var nextCallback = queues[qname].shift();
        if (nextCallback) {
            var xhr = nextCallback();
            activeReqs[qname] = xhr;
        }
        else {
            delete queues[qname];
            delete activeReqs[qname];
        }
    }
};

// Register a $.postq and $.getq method to provide shortcuts for $.get and $.post
// Copied from jQuery source to make sure the functions share the same defaults as $.get and $.post.
$.each( [ "getq", "postq" ], function( i, method ) {
    $[ method ] = function( qname, url, data, callback, type ) {

        if ( $.isFunction( data ) ) {
            type = type || callback;
            callback = data;
            data = undefined;
        }

        return $.ajaxq(qname, {
            type: method === "postq" ? "post" : "get",
            url: url,
            data: data,
            success: callback,
            dataType: type
        });
    };
});

var isQueueRunning = function(qname) {
    return (queues.hasOwnProperty(qname) && queues[qname].length > 0) || activeReqs.hasOwnProperty(qname);
};

var isAnyQueueRunning = function() {
    for (var i in queues) {
        if (isQueueRunning(i)) return true;
    }
    return false;
};

$.ajaxq.isRunning = function(qname) {
    if (qname) return isQueueRunning(qname);
    else return isAnyQueueRunning();
};

$.ajaxq.getActiveRequest = function(qname) {
    if (!qname) throw ("AjaxQ: queue name is required");

    return activeReqs[qname];
};

$.ajaxq.abort = function(qname) {
    if (!qname) throw ("AjaxQ: queue name is required");

    var current = $.ajaxq.getActiveRequest(qname);
    delete queues[qname];
    delete activeReqs[qname];
    if (current) current.abort();
};

$.ajaxq.clear = function(qname) {
    if (!qname) {
        for (var i in queues) {
            if (queues.hasOwnProperty(i)) {
                queues[i] = [];
            }
        }
    }
    else {
        if (queues[qname]) {
            queues[qname] = [];
        }
    }
};

}));

pakair commented 5 years ago

you wrote, address = ":8080" -> http://192.168.4.1/:8080/vor?params=undefined should be (remove / after .4.1) address = ":8080" -> http://192.168.4.1:8080/vor?params=undefined