I just hit a wall trying to incorporate my variables into your code (I get them separately but in connecting the two I'm having some trouble) wondering if you could help!!!! @bko
My html code just added a couple of lines to your's (I'm still not quite done wrapping my head around what exactly JSON reads and what I can call from the parsed data to get the variables I want)
Event name:
<table id="dataTable" width="500px" border="2">
<tr>
<td> Core ID </td>
<td> Data </td>
<td> Timestamp </td>
<td> milliseconds difference </td>
<td> number of presses </td>
<td> duration</td>
</tr>
</table>
<br><br>
<button id="connectbutton" onclick="start()">Connect</button>
<script type="text/javascript">
function start(objButton) {
document.getElementById("connectbutton").innerHTML = "Running";
var eventName = document.getElementById('evText').value;
var accessToken = "xxxxx";
var requestURL = "https://api.spark.io/v1/events/?access_token=" + accessToken;
var eventSource = new EventSource(requestURL);
eventSource.addEventListener('open', function(e) {
console.log("Opened!"); },false);
eventSource.addEventListener('error', function(e) {
console.log("Errored!"); },false);
eventSource.addEventListener(eventName, function(e) {
var parsedData = JSON.parse(e.data);
var nowTime = new Date(parsedData.published_at);
var dt = document.getElementById("dataTable");
var rows = dt.rows.length;
var foundIt = false;
for(var i=0;i<rows;i++) {
var rowN = dt.rows[i];
if (false==foundIt && rowN.cells[0].innerHTML==parsedData.coreid) {
foundIt = true;
var lastTime = new Date(rowN.cells[2].innerHTML);
rowN.cells[1].innerHTML = parsedData.data;
rowN.cells[2].innerHTML = parsedData.published_at;
rowN.cells[3].innerHTML = parsedData.result;
rowN.cells[3].innerHTML = nowTime.getTime() - lastTime.getTime();
}
}
if (false == foundIt) {
var newRow = dt.insertRow(rows);
var cell1 = newRow.insertCell(0);
var cell2 = newRow.insertCell(1);
var cell3 = newRow.insertCell(2);
var cell4 = newRow.insertCell(3);
cell1.innerHTML = parsedData.coreid;
cell2.innerHTML = parsedData.data;
cell3.innerHTML = parsedData.published_at;
cell4.innerHTML = parasedData.result;
}
}, false);
}
</script>
From this, I'm trying to read from my spark code
const int strokeButton = 1;
const int blitz = 7;
unsigned int currentHour;
unsigned int currentMin;
unsigned int currentSec;
unsigned long lastTime = 0UL;
char publishString[40];
int strokeCount = 0;
int stroke = 0;
int lastButtonState = 0;
void setup() {
pinMode(strokeButton, INPUT);
pinMode(blitz, OUTPUT);
Serial.begin(9600);
Spark.variable("strokecount", &publishString, STRING);
}
void loop() {
stroke = digitalRead(strokeButton);
if (strokeCount <= 10) {
if(stroke != lastButtonState) {
if(stroke == HIGH) {
strokeCount++;
digitalWrite(blitz, HIGH);
}
else {
digitalWrite(blitz, LOW);
Serial.print("you have taken ");
Serial.print(strokeCount);
Serial.println(" strokes");
}
}
lastButtonState = stroke;
}
else {
digitalWrite(blitz, HIGH);
delay(1000);
strokeCount = 0;
}
sprintf(publishString, "%u", strokeCount);
Spark.publish("strokeCount", publishString);
}
The passing of publishString isn't quite working out and I think it largely has to do with the fact that I'm not calling the right things or parsing the right things in- any help would be tremendously appreciated!!! (reaching the point of frustration here haha......)