Ok, I am trying to use two photon’s to talk to each other for a school project. Basically working towards a garage door status monitor and I am stuck.
My goal is to have one photon verify open/close status with a reed switch and publish it. I’ve only managed to get it to publish the open status and not both. However, when I do this I see 4 events pop up on the Particle Console for each time I move the magnet away.
Here is the publishing code:
int inputPin = D0; //pin for the reed switch
int reedState = HIGH; //Initial State of the switch
int status; //variabe for reading the pin
void setup() {
pinMode(D7,OUTPUT);
pinMode(inputPin,INPUT);
}
void loop()
{
readTheSensor(); // get the data from the sensor
reportTheData(); // report it out, if the state has changed
}
void setLED( int state )
{
digitalWrite(D7, state);
}
void readTheSensor() {
status = digitalRead(inputPin);
}
void reportTheData() {
if (status == LOW) {
if (reedState == HIGH) {
Particle.publish("open","open");
reedState = LOW;
setLED(reedState);
}
} else {
if (reedState == LOW) {
reedState = HIGH;
setLED( reedState );
}
}
}
The subscribing code I haven’t been able to work right. Here is what I have so far, after I try to explain the logic I want:
int led = D7;
int state = 0;
void setup()
{
pinMode(led,OUTPUT);
digitalWrite(led,LOW);
Particle.subscribe("open",myHandler,"using my device ID");
}
void myHandler (const char *event, const char *data)
{
if (strcmp(data,"open")==0)
{
digitalWrite(led,HIGH);
delay(100);
digitalWrite(led,LOW);
}
}
void loop()
{
}
What I want is a void loop that will continuously check for the published event, and it the reed switch is open it turns on the D7 led, and if the reed switch is closed it turns off the led.
This is my first real exposure to many of these concepts and coding in general, and I am at a loss. Too many hours of googling and testing other things and here we are.
Thank you in advance for your help/advice on this.
-Alchaun