EDIT #2: So guess what, I'm almost there! I am facing what seems to be the last of my problems, well, as long as programming is concerned. This is actually very interesting, I never ran into such a thing before. The matter is in the following code, my javascript function. I usually never post for problems that seem very simple to solve, but I really have no idea what's going on here.
The issue seems to be in the first condition of the update function. See the line that says alert('hey'); ? Well, if I erase that line, for some unknown reason, nothing is sent to the action function. Nor to the Arduino, to the console... Just nothing happens. It's absolutely fascinating, as I like to call it. I have no idea. I thought maybe the alert() created some kind of delay that was necessary to read the arduino output, but when i create a delay with setTimeout, nothing happens either. It's incredible.
Just one more time: without the alert, the action function is not called, I checked by making the function print something if it's called. Nothing is printed, nothing. It's just not called. But with the alert, the function is called and the arduino turns on the LED.
Do you have any explanation? Here is my code:
function update(command=0) {
// if command send it
if (command!=0) {
$.getJSON('/action?command='+command);
alert('hey');
}
// read no matter what
$.getJSON('/read', {}, function(data) {
if (data.state != 'failure' && data.content != '') {
$('.notice').text(data.content);
$('.notice').hide().fadeIn('slow');
setTimeout(function () { $('.notice').fadeOut(1000); }, 1500);
}
setTimeout(update, 5000);
});
}
update();
I am attempting to create a web interface accessible from any computer to control my Arduino.
I'm getting closer. One of my problems is that, using the following code, when I press a button to send a command to the Arduino, the Arduino does get it (the LED blinks as configured), then does send a message, and the Python script does retrieve the data, but it does NOT display it properly. The string misses some characters, and index.html is not returned as desired.
Basically, a function is called when a button is pressed, and I need to return the result of the function in a different function than the one the result was generated from.
Here is the code:
# -*- coding: utf-8 -*-
import cherrypy, functools, json, uuid, serial, threading, webbrowser, time
try:
ser = serial.Serial('COM4', 9600)
time.sleep(2)
ser.write('1')
except:
print('Arduino not detected. Moving on')
INDEX_HTML = open('index.html', 'r').read()
def timeout(func, args = (), kwargs = {}, timeout_duration = 10, default = None):
class InterruptableThread(threading.Thread):
def __init__(self):
threading.Thread.__init__(self)
self.result = default
def run(self):
self.result = func(*args, **kwargs)
it = InterruptableThread()
it.start()
it.join(timeout_duration)
if it.isAlive():
return it.result
else:
return it.result
def get_byte(useless):
return ser.read().encode('Utf-8')
def json_yield(command):
@functools.wraps(command)
def _(self, command):
if (command == 'Turn the LED on'):
ser.write('2')
time.sleep(2)
print('wrote to port')
print('ok, ok')
try:
m = ''
while 1:
print('reading...')
byte = timeout(get_byte, ('none',), timeout_duration = 5)
if byte == '*' or byte == None: break
m = m + byte
content = m
time.sleep(1)
return json.dumps({'state': 'ready', 'content':content})
except StopIteration:
return json.dumps({'state': 'done', 'content': None})
return _
class DemoServer(object):
@cherrypy.expose
def index(self):
return INDEX_HTML
@cherrypy.expose
@json_yield
def yell(self):
yield 'nothing'
@cherrypy.expose
@json_yield
def command(self):
yield 'nothing'
if __name__ == '__main__':
t = threading.Timer(0.5, webbrowser.open, args=('http://localhost:8080',))
t.daemon = True
t.start()
cherrypy.quickstart(DemoServer(), config='config.conf')