Need help with swift code for personal project

Hello! This is my first personal project and it’s almost complete, however I’ve gotten kind of stuck on why my code is not working. I’ve designed a simple pcb for the kick of it and it arrived today, I soldered everything together and it’s just about ready except for some code.

I’m trying to write a program to allow someone to turn my coffee machine on by iPhone and know whether it’s on or off. I’m pretty sure that the photon’s code is alright, as well as the hardware as I can call the registered cloud function from Particle Dev and it will work fine.

My problem is that my call to my cloud function is not turning on the machine, even though the cloud function is declared just as it is in the documentation. I also am not receiving any events when I turn my coffee machine on and off.

Here is my code:

Here is the photon’s code:

I receive the print statement saying that I pushed the button but I don’t receive anything indicating that it got to the block after subscribing to the eventstream. Also I am planning on having the code actually do something with the events, but I haven’t figured that out yet.

Bonus: This is what it looks like so far, minus the box and the LED and button behind it:

Looks to me like your “If” to actually turn on or off is outside your function loop. It’s never executed. Include it in the loop or call it from the loop.

where? In which code?

Particle Photon Void Loop

bool success = Particle.function("PRESS", write);

This is saying that when we ask the cloud for the function “PRESS”, it will employ the function write() from this app and return true on its success… I don’t think I need to put it in the void loop. The documentation has an example here where they do not employ anything into the void loop.

Sorry - yes, you’re correct. In my mind “write” is a reserved word/system function. Wasn’t seeing it as your particle function.

Not quite.

bool success = Particle.function("PRESS", write);

the return value here will only tell that the registration of write() as Particle.function called PRESS in the cloud was successful.
But at that time write() will not be executed and hence not report its success.
BTW the return value of write() would be int not bool but Particle.function() returns bool.

1 Like

Okay but it should still write when I make a post request like that, right? I removed the success bit, because I didn’t use it and it was confusing. From what I understand my app is successfully creating a post request but photon doesn’t seem to react to it.

Is it really not executing your function or does it just not do what you expect?

I see this code in your Photon sketch

  if (strcmp(pin,"D7")){
    digitalWrite(D7, HIGH);
    delay(5000);  //debug mode, change back to delay of 5
    digitalWrite(D7, LOW);
    return 0;

This actually checks if the incoming parameter is not D7.

Equal strings would return zero, but you check for non-zero.

Have you checked if you might get the -1 back from your function?

1 Like


Thank you! I’m an idiot, I just expected it to work as I expected it to, probably because of how I used to compare strings in java or some other language. I’m working on the other bugs, I’ll post back how everything else is going tomorrow. Code has changed a lot since then.

1 Like

I just realized I never updated! It’s been working perfectly. Here’s a video of it working.

It was a really great learning experience and I’d recommend this as a good first project. I really only took one programming class before this in Java and played around in a couple other different languages. In terms of the circuitry I also only knew basic circuit analysis. The majority of the design work was done using Pspice and it probably wasn’t even necessary.