I’m trying to use a push button to send a REST GET command using TCPClient. I’m able to successfully send the GET command, but the Core keeps flashing a red SOS and then eleven(11) red blinks before it reboots. This is happening very time I hit my push button. According to the Troubleshooting Docs, 11 red blinks means Invalid Case. What exactly does this mean and how do I fix it?
const int buttonPin = D0;
long lastDebounceTime = 0;
const long debounceDelay = 5000;
byte server[] = { 192, 168, 1, 150 };
TCPClient client;
void setup()
{
pinMode(buttonPin, INPUT);
Serial.begin(9600);
attachInterrupt(buttonPin, print, RISING);
Time.zone(-8);
};
void loop(){};
void print()
{
unsigned long m = millis();
if (m - lastDebounceTime > debounceDelay)
{
sendData();
}
lastDebounceTime = m;
}
void sendData()
{
delay(1000);
if (client.connect(server, 8080)){
Serial.println("Connection successful");
client.println("GET /controller/rest/control/195/50 HTTP/1.1");
client.println("Host: 192.168.1.150");
client.println("Content-Length: 0");
client.println("Connection: close");
client.println();
}
else{
Serial.println("Connection failed");
client.stop();
}
Serial.println("End");
};
Thanks in advance.
Update
I’ve implemented @peekay123 clickButton library and all seems to be working. I think it was blinking red because I was doing too much in the interrupt method. I’ve also posted it on GitHub.
/*
Title: openremote-spark-button
Description: Update a OpenRemote slider using a Spark Core with a pushbutton
Author: Nicholas Wilde <ncwilde43@gmail.com>
Date: 06/11/2014
Note: This example is based off of pkourany's clickButton demo
<https://github.com/pkourany/clickButton/blob/master/firmware/examples/clickButtonDemo.cpp>
*/
// This #include statement was automatically added by the Spark IDE.
#include "clickButton/clickButton.h"
// Values
const unsigned int sliderVal = 50; // The value we want to set the slider to.
const unsigned int sliderId = 195; // The ID of the OpenRemote slider.
// the Button
const int buttonPin1 = 0;
ClickButton button1(buttonPin1);
// Button results
int function = 0;
// TCPClient stuff.
TCPClient client;
// This routine runs only once upon reset
void setup()
{
Serial.begin(9600);
pinMode(D0, INPUT);
// Setup button timers (all in milliseconds / ms)
// (These are default if not set, but changeable for convenience)
button1.debounceTime = 20; // Debounce timer in ms
button1.multiclickTime = 250; // Time limit for multi clicks
button1.longClickTime = 1000; // time until "held-down clicks" register
}
// This routine loops forever
void loop()
{
// Update button state
button1.Update();
// Save click codes in LEDfunction, as click codes are reset at next Update()
if (button1.clicks != 0) function = button1.clicks;
if(function == -1) //Long click
{
Serial.println("SINGLE LONG click");
sendData();
}
function = 0;
delay(5);
}
// Send a GET request using TCPClient
void sendData()
{
delay(1000);
Serial.println("Connecting to server ...");
if (client.connect("192.168.1.150", 8080)) // IP & port of local OpenRemote server
{
Serial.println("Connection successful");
String restString = buildString(sliderId, sliderVal);
client.println(restString);
client.println("Host: 192.168.1.150");
client.println("Content-Length: 0");
client.println();
}
else Serial.println("Connection failed");
delay(2000); // Delay needed to allow the GET command to be received.
client.stop();
}
// Build the GET string with the given parameters
String buildString(int id, int val)
{
String s = "GET /controller/rest/control/";
s += id;
s += "/";
s += val;
s += " HTTP/1.1";
Serial.println(s);
return s;
}