Skip to content

Commit

Permalink
added particle code for network connectivity
Browse files Browse the repository at this point in the history
  • Loading branch information
jeff committed Oct 28, 2018
1 parent c19db24 commit 3b1059d
Show file tree
Hide file tree
Showing 2 changed files with 123 additions and 0 deletions.
24 changes: 24 additions & 0 deletions car/photon-control/particle-left.html
@@ -0,0 +1,24 @@
<!--
<form action="https://api.particle.io/v1/devices/device-id-here/led?access_token=access-token-here" method="POST">
-->
<html>
<body>
<center>
<br>
<br>
<br>
<form action="https://api.particle.io/v1/devices/3d0024000c47353136383631/dir?access_token=1fa9ab2ed7aef7b8f5a7d3b954866fabaca14f6b" method="POST">
Tell your device what to do!<br>
<br>
<input type="radio" name="arg" value="left">Turn left.
<br>
<input type="radio" name="arg" value="right">Turn right.
<br>
<input type="radio" name="arg" value="straight">Go straight.
<br>
<br>
<input type="submit" value="Do it!">
</form>
</center>
</body>
</html>
99 changes: 99 additions & 0 deletions car/photon-control/photon_dir.c
@@ -0,0 +1,99 @@
// -----------------------------------
// Controlling LEDs over the Internet
// -----------------------------------

// First, let's create our "shorthand" for the pins
// Same as in the Blink an LED example:
// led1 is D0, led2 is D7

int led1 = D0;
int led2 = D7;
int LEFT_PIN = D5;
int RIGHT_PIN = D6;

// Last time, we only needed to declare pins in the setup function.
// This time, we are also going to register our Particle function

void setup()
{

// Here's the pin configuration, same as last time
pinMode(led1, OUTPUT);
pinMode(led2, OUTPUT);
pinMode(LEFT_PIN, OUTPUT);
pinMode(RIGHT_PIN, OUTPUT);

// We are also going to declare a Particle.function so that we can turn the LED on and off from the cloud.
// This is saying that when we ask the cloud for the function "led", it will employ the function ledToggle() from this app.
Particle.function("led",ledToggle);

// For good measure, let's also make sure both LEDs are off when we start:
digitalWrite(led1, LOW);
digitalWrite(led2, LOW);

Particle.function("dir",setDirection); // "dir" can receive "left", "right" or "straight" (default)

digitalWrite(LEFT_PIN, LOW);
digitalWrite(RIGHT_PIN, LOW);

}


// Last time, we wanted to continously blink the LED on and off
// Since we're waiting for input through the cloud this time,
// we don't actually need to put anything in the loop

void loop()
{
// Nothing to do here
}

// We're going to have a super cool function now that gets called when a matching API request is sent
// This is the ledToggle function we registered to the "led" Particle.function earlier.


int ledToggle(String command) {
/* Particle.functions always take a string as an argument and return an integer.
Since we can pass a string, it means that we can give the program commands on how the function should be used.
In this case, telling the function "on" will turn the LED on and telling it "off" will turn the LED off.
Then, the function returns a value to us to let us know what happened.
In this case, it will return 1 for the LEDs turning on, 0 for the LEDs turning off,
and -1 if we received a totally bogus command that didn't do anything to the LEDs.
*/

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;
}
}


int setDirection(String command) {
/* Particle.functions always take a string as an argument and return an integer.
Allow three values for the string: "left", "right" or "straight" (default)
*/

digitalWrite(LEFT_PIN,LOW);
digitalWrite(RIGHT_PIN,LOW);

if (command=="left") {
digitalWrite(LEFT_PIN,HIGH);
return 1;
}
else if (command=="right") {
digitalWrite(RIGHT_PIN,HIGH);
return 2;
}
else {
return 0;
}
}

0 comments on commit 3b1059d

Please sign in to comment.