Hi,
Thank again for all the resources. I’ve spent some time yesterday to go through the tutorials and Cloud API reference.
I did get the publish() , variable() and function() examples to work. Managed to use the TCP client working as well in one direction.
The problem is today when I tried to reflash the apps/sketches wrote yesterday, they didn’t seem to work today. I keep getting a 404:
{
"ok":false,
"error":"Function not found"
}
I’ve tried reflashing a few times, resetting the tokes and updating the html code but no joy.
For reference here is some of the code I used:
bool isBlinking;
int ledState;
unsigned long wait,lastTime;
void setup()
{
//Register our Spark function here
Spark.function("led", ledControl);
Spark.function("ledBlink", ledBlink);
// Configure the pins to be outputs
pinMode(D7, OUTPUT);
digitalWrite(D7, LOW);
wait = 1000;//default led delay
}
void loop()
{
if(isBlinking){
unsigned long now = millis();
if (now-lastTime>wait) {
lastTime = now;
ledState = 1-ledState;
digitalWrite(D7, ledState);
}
}
}
int ledControl(String command)
{
int state = 0;
if(command == "HIGH") state = 1;
else if(command == "LOW") state = 0;
else return -1;
isBlinking = false;
digitalWrite(D7, state);
return 1;
}
int ledBlink(String delayString){
int state = 0;
int value = atoi(delayString.c_str());
if(value < 0) value = 10;
if(value > 10000) value = 10000;
wait = value;
isBlinking = true;
return 1;
}
And client code:
<!DOCTYPE html>
<html>
<head>
<title>Spark POST test</title>
<meta name="viewport" content="width=device-width,initial-scale=2">
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.0/jquery.min.js"></script>
<script>
var deviceID = "###";
var accessToken = "###";
function sparkToggle(value){
$.post( "https://api.spark.io/v1/devices/"+deviceID+"/led", { access_token: accessToken, params: value }).done(sparkPostCallback);
}
function sparkBlink(value){
$.post( "https://api.spark.io/v1/devices/"+deviceID+"/ledBlink", { access_token: accessToken, params: value }).done(sparkPostCallback);
}
function sparkPostCallback(data){
console.log(data);
}
</script>
</head>
<body>
<form>
<input type="button" id="high" value="HIGH" onclick="sparkToggle('HIGH');"><br />
<input type="button" id="low" value="LOW" onclick="sparkToggle('LOW');"><br />
<input type="range" name="blink" min="10" max="1000" value="500" onchange="sparkBlink(this.value);blinkLabel.value = 'delay:'+this.value;"/><br />
<input type="text" name="blinkLabel" disabled/>
</form>
</body>
</html>
and here’s the modified TCP test:
TCPClient client;
String ip = "192.168.0.5";
bool wasConnected;
void ipArrayFromString(byte ipArray[], String ipString) {
int dot1 = ipString.indexOf('.');
ipArray[0] = ipString.substring(0, dot1).toInt();
int dot2 = ipString.indexOf('.', dot1 + 1);
ipArray[1] = ipString.substring(dot1 + 1, dot2).toInt();
dot1 = ipString.indexOf('.', dot2 + 1);
ipArray[2] = ipString.substring(dot2 + 1, dot1).toInt();
ipArray[3] = ipString.substring(dot1 + 1).toInt();
}
int connectToMyServer(String ip) {
byte serverAddress[4];
ipArrayFromString(serverAddress, ip);
if (client.connect(serverAddress, 9000)) {
return 1; // successfully connected
} else {
return -1; // failed to connect
}
}
void setup() {
Spark.function("connect", connectToMyServer);
for (int pin = D0; pin <= D7; ++pin) {
pinMode(pin, OUTPUT);
}
pinMode(A0,INPUT);
Serial.begin(9600);
Serial.println("OK Computer");
}
void loop() {
int A0Value = analogRead(A0);
if (client.connected()) {
if (client.available()) {
char pin = client.read() - '0' + D0;
char level = client.read();
if ('h' == level) {
digitalWrite(pin, HIGH);
} else {
digitalWrite(pin, LOW);
}
//client.println(level,DEC);
}
// if(!wasConnected){
// wasConnected = true;
// client.println(Spark.deviceID());//this crashes the server and I don't know why
// }
if(A0Value > 0) {
client.stop();
// wasConnected = false;
}
}else{
if(A0Value == 0) connectToMyServer(ip);
}
}
The only change from the documentation sample is I’m also hard coding the server ip and connecting/disconnecting A0 from ground to connect/disconnect from the TCP server.
The server is a minimal Processing application:
import oscP5.*;
import netP5.*;
OscP5 server;
TcpClient client;
int numClients;
void setup(){
server = new OscP5(this, 9000, OscP5.TCP);
}
void draw(){
numClients = server.tcpServer().getClients().length;
if(client == null && numClients > 0) client = server.tcpServer().getClient(0);
if(numClients == 0) client = null;
background(numClients == 0 ? color(192,0,0) : color(0,192,0));
}
void keyPressed(){
if(numClients > 0){
if(key == '=') client.send("7h");
if(key == '-') client.send("7l");
}
}
Going through the tutorials I have more questions that will go outside of the scope of the original question and I am more than happy to post different threads if that makes it easier for the community.
Related to this thread my questions would be:
- how can I test/debug/check that the the firmware I expect (from Spark Build) is on the board and can communicate with the Cloud API. So far the LED statuses are my only hints
- is there a limit for the Cloud API ? Will my board be temporarily banned if I make to many posts ? What are the best practices on this side ?
Thank you again,
George