Publish and subscribe functions not working

I have an electron set up sending data to a photon to activate a gate. I bench tested this setup in my shop for 3 days before installing it, but once on site, it doesn’t seem to work. The electron is publishing when it’s supposed to, and I can add an additional publish to the photon, and it will return the data it receives. Also, the photon is online and can be pinged from the dashboard successfully.

So the only difference is the Wi-Fi equipment on site, is there a chance the correct port is open on the router? Or would that stop the photon from connecting completely?

There isn’t really enough data and too many variables involved to answer this properly.

A good practice to test the accesibility of devices is to have some Particle.function() to call and check the return value.
Also to check whether subscribe handlers are called properly exposing some Particle.variable() that reflects the successful trigger can help debugging.

1 Like

I’ve added an additional line in my function to publish the data the device receives. This works successfully as when device 1 publishes the data, device 2 is subscribing successfully and re-publishing the data to the cloud where I can see it in the console. But my code, that worked last week for 3 days straight, suddenly isn’t working. Once again, I know you’ve seen it before, but here it is:

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


void subscriptionHandler(const char *event, const char *data); // forward declaration
void senderInterrupt(); // forward declaration

const int WIEGAND_D0_PIN = D2; // Green
const int WIEGAND_D1_PIN = D3; // White
int relay = D7;

const int TIMER_PERIOD_US = 100;
const int LOW_PERIOD = 1;
const int HIGH_PERIOD = 10;

IntervalTimer timer;
int sendBitsLeft = 0;
int sendPeriod = 0;
unsigned long sendValue;

void setup()
{
    pinMode(relay, INPUT_PULLDOWN);
	Serial.begin(9600);
    
	pinMode(WIEGAND_D0_PIN, OUTPUT);
	digitalWrite(WIEGAND_D0_PIN, HIGH);

	pinMode(WIEGAND_D1_PIN, OUTPUT);
	digitalWrite(WIEGAND_D1_PIN, HIGH);

	timer.begin(senderInterrupt, TIMER_PERIOD_US, uSec);
    
	Particle.subscribe("Janus2103b", subscriptionHandler, MY_DEVICES);
	
	
}

void loop() {

if (digitalRead(relay)==HIGH) {
        Particle.publish("Janus2103a","open", PRIVATE);
        delay(10);
        digitalWrite(relay, LOW);
        delay(5000);
    }
}
void subscriptionHandler(const char *event, const char *data) {
    Particle.publish("Janus2103c", data, PRIVATE);
	unsigned long value = strtoul(data, NULL, 0);
	Serial.printlnf("received value=0x%x", value);
	sendPeriod = 0;
	sendValue = value;
	sendBitsLeft = 26;
}

void senderInterrupt() {
	if (sendBitsLeft > 0) {
		if (sendPeriod == HIGH_PERIOD) {
			sendPeriod = 0;
		}

		if (sendPeriod == 0) {
			// Write out the data bit
			if ((sendValue >> (sendBitsLeft - 1)) & 1) {
				// 1 bit
				digitalWrite(WIEGAND_D1_PIN, LOW);
			}
			else {
				// 0 bit
				digitalWrite(WIEGAND_D0_PIN, LOW);
			}
		}
		else
		if (sendPeriod == LOW_PERIOD) {
			// Restore high
			digitalWrite(WIEGAND_D0_PIN, HIGH);
			digitalWrite(WIEGAND_D1_PIN, HIGH);
			sendBitsLeft--;
		}

		sendPeriod++;
	}
}

System firmware on the photon is 0.6.3, and because I thought there may be an internet problem, I set up an electron with the same code, and is running system firmware 0.6.4.

Guys, I’ve got a pretty serious problem here. It seems my publish is getting lost somewhere and never being picked up by the other end, but only on this site. I can use the exact same equipment in my shop and everything works fine. Electron running 0.6.4, publishing data to the cloud, I can see this happen in the console, Photon running 0.6.3 is the subscribe end, and never receives the data. I have double checked, and had someone else double check the code, everything is right.

void subscriptionHandler(const char *event, const char *data) {
    Particle.publish("Janus2103c", data, PRIVATE);
	unsigned long value = strtoul(data, NULL, 0);
	Serial.printlnf("received value=0x%x", value);
	sendPeriod = 0;
	sendValue = value;
	sendBitsLeft = 26;
}

from the docs

NOTE 2: Particle.publish() and the Particle.subscribe() handler(s) share the same buffer. As such, calling Particle.publish() within a Particle.subscribe() handler will wipe the subscribe buffer! In these cases, copying the subscribe buffer’s content to a separate char buffer prior to calling Particle.publish() is recommended.

2 Likes

That should only be an issue if I’m attempting to subscribe and publish at the same time, or rather, use the data at the same time. This publishes data, then a few seconds later gets the authentication. Once the wiegand card number has been published, it can be wiped.

The buffer sharing between publish and subscribe isn’t that “clean”, so you should at least swap the first two lines of your subscriptoin handler round.
First use data for your strtoul() and after publish the event.

1 Like

I removed the publish from that function, it was only there to send data back, if it was received, as a troubleshooting measure. No change in behavior. Ended up installing a competitors product to get the customer taken care of, but I’m still curious as to what’s causing this. It hasn’t been an issue since product creation, and I haven’t changed code, or system firmware.