Back to Parent

The Final Code
// this is m side, another is f side
int switchPin = A1; // pin to detect the voltage of at an end of a switch to tell whether it is turn on/off
int switchReading = 0;
//int turnonandoff = 0;
int lightPin = D0; // pin to control the LED light

void setup(){
  pinMode(switchPin, INPUT);
  Particle.variable("switch", &switchReading, INT); // determine the threshod by observing the reading in monitor
  Serial.begin(4800); 

  pinMode(lightPin, OUTPUT);
  Particle.subscribe("intimacy/switchReading/f2m", myHandler); // listen to what the f side is saying

}

void myHandler(const char*event, const char*data){
  String onoroff = String (data);
  if (onoroff == "turnon"){
    digitalWrite (lightPin, HIGH); // turn on hte light is the switch in another side is on
  } else {
    digitalWrite(lightPin, LOW);
  }
}


void loop(){
  switchReading = analogRead(switchPin); // transfer raw analog reading

  Serial.println(switchReading);    // determine the threshod by observing the reading in monitor
  if (switchReading >= 1000){
    Particle.publish("intimacy/switchReading/m2f", "turnon");  // publish on/off based on whether swith is turned on or off
    delay(1000);
  } else {
    Particle.publish("intimacy/switchReading/m2f", "turnoff");
    delay(1000);
  }

}


//____________________________________________________________________________________________
//____________________________________________________________________________________________
//____________________________________________________________________________________________

// this is f side
int switchPin = A1; // pin to detect the voltage of at an end of a switch to tell whether it is turn on/off
int switchReading = 0;
//int turnonandoff = 0;
int lightPin = D0; // pin to control the LED light

void setup(){
  pinMode(switchPin, INPUT);
  Particle.variable("switch", &switchReading, INT); // determine the threshod by observing the reading in monitor
  Serial.begin(4800); 

  pinMode(lightPin, OUTPUT);
  Particle.subscribe("intimacy/switchReading/m2f", myHandler); // listen to what the f side is saying

}

void myHandler(const char*event, const char*data){
  String onoroff = String (data);
  if (onoroff == "turnon"){
    digitalWrite (lightPin, HIGH); // turn on hte light is the switch in another side is on
  } else {
    digitalWrite(lightPin, LOW);
  }
}


void loop(){
  switchReading = analogRead(switchPin); // transfer raw analog reading

  Serial.println(switchReading);    // determine the threshod by observing the reading in monitor
  if (switchReading >= 1000){
    Particle.publish("intimacy/switchReading/f2m", "turnon");  // publish on/off based on whether swith is turned on or off
    delay(1000);
  } else {
    Particle.publish("intimacy/switchReading/f2m", "turnoff");
    delay(1000);
  }

}
Click to Expand

Content Rating

Is this a good/useful/informative piece of content to include in the project? Have your say!

0