Successful Publish, Fail in Subscribing

I am publishing this…

|windowHandler | Temperature 66.537498F | spare_and_working|4/4/23 at 10:10:34 am|
|windowHandler | Open Windows|spare_and_working | 4/4/23 at 10:10:34 am|

But my subscriber doesn’t appear to be working. Here is my Subscribe program.
Any ideas? I am new to this.

int relay1 = D5; // set pin for relay1
int relay2 = D6; // set pin for relay2

void setup() {

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
}
// Function to handle the particle event
void windowHandler(const char *event, const char *data) {
  Serial.print("Received event: ");
  Serial.println(event);
  Serial.print("Received data: ");
  Serial.println(data);
  
  If the event is "Open Window", set D5 and D6 to HIGH
  if (strcmp(data, "Open Windows") == 0) {
   Serial.println("Opening windows");
    digitalWrite(relay1, HIGH);
    digitalWrite(relay2, HIGH);
  }
  // If the event is "Close Window", set D5 and D6 to LOW
  else if (strcmp(data, "Close Windows") == 0) {
   Serial.println("Closing windows");
    digitalWrite(relay1, LOW);
    digitalWrite(relay2, LOW);
  }
}

Hi Brent-

The Subscribe function will help you out here. Check out these docs and let me know if you have any questions.

Colleen

Thank you Colleen,
After looking a the documents you mentioned,
I made some changes, and here are the resulting programs.

–Publish–

// This #include statement was automatically added by the Particle IDE.
#include "lib1.h"

// This #include statement was automatically added by the Particle IDE.
#include <spark-dallas-temperature.h>

#include <OneWire.h>
//#include <DallasTemperature.h>

// Data wire is plugged into pin D7 on Particle Photon
#define ONE_WIRE_BUS D6

// Setup a oneWire instance to communicate with any OneWire devices
OneWire oneWire(ONE_WIRE_BUS);

// Pass our oneWire reference to Dallas Temperature sensor
DallasTemperature sensors(&oneWire);

void setup() {
// handleOpenWindows
// Start serial communication
Serial.begin(9600);

// Initialize the DS18B20 sensor
sensors.begin();
}

void loop() {
// Request temperature readings from the DS18B20 sensor
sensors.requestTemperatures();

// Get the temperature value in Fahrenheit
float temperature = sensors.getTempFByIndex(0);

if (temperature > 74) {
// Close window code here
Particle.publish("handleCloseWindows", "Close Windows", PRIVATE);

} else if (temperature >= 57 && temperature <= 67){ //62
// Open window code here
Particle.publish("handleOpenWindows", "Open Windows", PRIVATE);
}

{
Particle.publish("windowHandler", "Temperature " + String(temperature) + "F", PRIVATE);
}

{
// Wait for a few seconds before requesting another reading
delay(60000); //900,000 = 15 munites/ 1,200,000 = 20 minutes
}
}

This is what it’s putting out:

windowHandlerTemperature 58.549999Fspare_and_working4/4/23 at 5:08:40 pmhandleOpenWindowsOpen Windowsspare_and_working4/4/23 at 5:08:40 pmwindowHandlerTemperature 58.325001Fspare_and_working4/4/23 at 5:07:39 pmhandleOpenWindowsOpen Windowsspare_and_working4/4/23 at 5:07:39 pm

Here is the Subscribe Program:

–Subscribe–

int relay1 = D5; // set pin for relay1
int relay2 = D6; // set pin for relay2

void setup() {
Particle.subscribe("Open Windows", handleOpenWindows);
Particle.subscribe("Close Windows", handleCloseWindows);

pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
}

void loop() {
// your main loop code here, if any
}

void handleOpenWindows(const char *event, const char *data) {
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
}

void handleCloseWindows(const char *event, const char *data) {
digitalWrite(relay1, LOW);
digitalWrite(relay2, LOW);
}

What did I do wrong? As a nube, it looks correct to me.
Any help is greatly appreciated.

You should subscribe to the event name, not to the payload.

So it should actually be

  Particle.subscribe("handleOpenWindows", handleOpenWindows);
  Particle.subscribe("handleCloseWindows", handleCloseWindows);

Since you don't use the payload (Open Windows | Close Windows) in your event handlers anyhow, you can also omit it in the publish statement.

As for coding style, proper indentation would help readability - particularly with more elaborate projects.
There is also little benefit in using anonymous code blocks like here

BTW, there is no PUBLIC / PRIVATE distinction for events anymore, so you can just omit the last parameter.

Noted, and thank you

This topic was automatically closed 182 days after the last reply. New replies are no longer allowed.