I'm trying to read data in using BLE and parsing the data from a Web page to variables in Arduino.
Been reading the serial data processing tutorials....
If I have several buttons and sliders on a web page.
On Web Page When Button1 is clicked it sends a value of <1> that is supposed to go to Variable 'A' in arduino
Button2 will send a <1> to Variable 'B' on arduino.
Slider1 will send a value between <1-255> to Variable 'C' and Slider2 will send a value between <1-255> to Variable 'D'.
How do I add these to the buffer that is read by the arduino so I can parse the values to the correct variable on Arduino?
I have found a sample script that will let me do one value but I'm confused as to how to expand this to include the controls I need.
Any pointers to tutorials and/or advise is appreciated, I'm new to this.
Thanks
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<meta name="viewport" content="width=device-width, user-scalable=no
initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0" />
<title>LED On/Off BLE</title>
<style>
@import 'ui/css/evothings-app.css';
</style>
<script>
// Redirect console.log to Evothings Workbench.
if (window.hyper && window.hyper.log) { console.log = hyper.log }
window.onerror = function(msg, url, line)
{
console.log(msg + ": " + url + ":" + line)
}
</script>
<script src="cordova.js"></script>
<script src="libs/jquery/jquery.js"></script>
<script src="libs/evothings/evothings.js"></script>
<script src="libs/evothings/arduinoble/arduinoble.js"></script>
</head>
<body ontouchstart="">
<h1>BLE LED ON/OFF</h1>
<p id="info">Initializing...</p>
<button class="yellow wide" onclick="app.connect()">CONNECT</button>
<button class="green wide big" onclick="app.ledOn()">LED ON</button>
<button class="red wide big" onclick="app.ledOff()">LED OFF</button>
<script>
// Application object.
var app = {}
// Connected device.
app.device = null
// Turn on LED.
app.ledOn = function()
{
app.device && app.device.writeDataArray(new Uint8Array([1]))
}
// Turn off LED.
app.ledOff = function()
{
app.device && app.device.writeDataArray(new Uint8Array([0]))
}
app.showMessage = function(info)
{
document.getElementById('info').innerHTML = info
}
// Called when BLE and other native functions are available.
app.onDeviceReady = function()
{
app.showMessage('Touch the connect button to begin.')
}
app.connect = function()
{
evothings.arduinoble.close()
evothings.arduinoble.connect(
'ble_led', // Name of BLE shield.
function(device)
{
app.device = device
app.showMessage('Connected! Touch buttons to turn LED on/off.')
},
function(errorCode)
{
app.showMessage('Connect error: ' + errorCode + '.')
})
}
document.addEventListener('deviceready', app.onDeviceReady, false)
</script>
</body>
</html>
On Web Page When Button1 is clicked it sends a value of <1> that is supposed to go to Variable 'A' in arduino
Button2 will send a <1> to Variable 'B' on arduino.
Slider1 will send a value between <1-255> to Variable 'C' and Slider2 will send a value between <1-255> to Variable 'D'.
The easiest way to do what you want is to send an identifier with the data say, A, B, C or D like this , etc. That way the Arduino code can parse the input, find the start of message identifier (the <), identify the source of the data (A, B, C or D) and read the value until the end of message marker (the >)
I have found a sample script that will let me do one value but I'm confused as to how to expand this to include the controls I need.
None of that crap is necessary. Use a form, with named (submit) buttons. Then, when a submit button is pressed, the names of all the button, and their states (pressed or released), will be part of the GET request.
Wazhere:
I'm trying to read data in using BLE and parsing the data from a Web page to variables in Arduino.
On Web Page When Button1 is clicked it sends a value of <1> that is supposed to go to Variable 'A' in arduino
Button2 will send a <1> to Variable 'B' on arduino.
Slider1 will send a value between <1-255> to Variable 'C' and Slider2 will send a value between <1-255> to Variable 'D'.
This is all very confusing for me. I don't understand the HTML because it seems to include capabilities (like "evothings.arduinoble.connect(" that you have not explained. I have no idea how your web page / web server sends BLE data. What web server are you using? What is hosting it?
From an Arduino point of view the simplest thing is to send the status of all the buttons in the same order every time in a message like <12, 233,7,190>
In case there is any confusion the <> should surround a single message - whether it contains one value or several values..
If it is desirable to send messages for a single button I suggest you send them as (for example) <A,12> because that will make the parsing easier.
And while the comma is commonly used as a separator it is not essential - any character can be used.
Robin2-- what would you use if you were to do this from a web page? Need to have it connect from a pi3 to Arduino over BLE and to the end user via web page over TCP/IP network connection.
So the pi runs the web server and all the pretty graphics that show up in the browser. It sends commands to the Arduino over BLE, which is pretty simple serial.
The important question is: does the web page need to know the state of the Arduino at all times? Can you log in to that page next month and still see all the values you set? Probably. So the web server needs to send all values every time it talks to the Arduino.
Yes the pi should read the values currently running on the Arduino so I will write any current value to a variable that can be read by the pi each time it connects then set the slider and switch positions so they are what is currently running.
To give you an overview, I'm using the Adafruit motor shield on a uno. I have hard pots and switches that can be used to control 4 trains.
What I'm trying to do is set the pi as a hub or gateway to allow web based controls to the setup. That way any network connected mobile device can control via the web page.
Not sure what I should use to get the Webserver on the pi to talk to the Arduino over BLE so it can be controlled by the web page. I was thinking a node app but not sure how to get that working. I'm open to using anything that will work that I can get examples to get me started. The only thing I have working are the hard wired controls so I can scrap any thing else I have tried.
Wazhere:
I'm willing to start over to do this right
I suggest you break the project into two separate parts.
Write an Arduino program and a corresponding RPi program to allow the Arduino to send/receive data to/from the RPi and show the results. This Python - Arduino demo may give you a starting point.
When you can do that you will then have the values on the RPI and you can write the rest of your RPi code to send/receive values from the Web.
I have used Python Bottle to create simple PC based webservers.