I recently purchased a few particles in part because they are little devices that are named after elements that allow you to connect sensors to the internet; what about these items does not scream BoBtheChemist?
I’m currently running through some tutorials and am sure to forget a thing or three, so I’m going to chronicle my experience in hopes to (a) have a central repository for my work, (b) show that my website does have some activity (c) possibly serve as a resource for the 1-2 bots that frequent my website.
I have an Argon and two Xenon particles at the moment. The Argon is my connection to the interweb so I’m focusing on that one for the moment. I’ve got this code running:
int led1 = D0;
int led2 = D7;
void setup()
{
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
Particle.function("led",ledToggle);
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);
}
void loop()
{
// Nothing to do here
}
int ledToggle(String command) {
if (command=="on") {
digitalWrite(led1,HIGH);
digitalWrite(led2,HIGH);
return 1;
}
else if (command=="off") {
digitalWrite(led1,LOW);
digitalWrite(led2,LOW);
return 0;
}
else {
return -1;
}
}
That’s simply the web-connected LED demo code from the particle tutorial. It allows me to create a special URL that will toggle LEDs on my Argon. The new part was to control this LED via Mathematica, which requires the following code:
r[arg_String] := HTTPRequest[<|
"Method" -> "POST",
"Scheme" -> "https",
"Domain" -> "api.particle.io",
"Path" -> {"v1", "devices", device, "led"},
"Query" -> "access_token" -> token,
"Body" -> {"arg" -> arg}
|>];
Button["on", URLExecute@r@"on"]
Button["off", URLExecute@r@"off"]
The device and access_token symbols are defined earlier but aren’t presented here so that all those bots visiting my website cannot take over my Argon and start making it blink uncontrollably.
Needless to say, this simple demonstrate works well, and within about 15 minutes, I am now controlling an Argon LED over the internet with Mathematica. Yeah.