Electron to photon communication

Hi

I currently own an electron and a photon, what i am trying to do is have the photon read a level in the tank via a digital input then when the input is HIGH turn on an output on the electron. so i have been trying to write my code for the photon and this is what i have.

// project 2 getting two units to talk to each one unit will be at the tank (master) and when space is available in the tanks will tell the unit at the well to pump (slave)

char publishString[40];//the varible which holds the string for the publish
int floatinput = D4; // Instead of writing D0 over and over again, we'll write floatinput

boolean storagetankavailable = false; // declare a true or false 


void setup()
{
    pinMode(floatinput, INPUT); // declare that floatinput is an input and when it has an input will mean space in tank is available
   
}

void loop() {
  
    if (floatinput == HIGH) // if the input is on
    {
        storagetankavailable == true; // assign the value true to variable
    }
    else
    {
        storagetankavailable == false;
    }

   sprintf(publishString,"%s\n",storagetankavaiable); 
    Spark.publish("oktopump",publishString);

  // We'll wait for 10 second...
  delay(10000);


}

there seems to be an error with the above code but i can not seem to find what it is (could you please point me in the right direction)
so then i will need to write code for the electron which will subscribe to this event and then turn its output on depending on another if else statement

Thanks for any advice

change it to this now but still no joy

// project 2 getting two units to talk to each one unit will be at the tank (master) and when space is available in the tanks will tell the unit at the well to pump (slave)

char publishString[40];//the varible which holds the string for the publish
int floatinput = D4; // Instead of writing D0 over and over again, we'll write floatinput

boolean storagetankavailable = false; // declare a true or false 
int floatinputstate = 0;

void setup()
{
    pinMode(floatinput, INPUT); // declare that floatinput is an input and when it has an input will mean space in tank is available
   
}

void loop() {
    
    floatinputstate = digitalRead(floatinput);
    if (floatinputstate == HIGH)
    {
        storagetankavailable == true;
    }
    else
    {
        storagetankavailable == false;
    }

   sprintf(publishString,"%s\n",storagetankavaiable); 
    Spark.publish("oktopump",publishString);

  // We'll leave it on for 10 second...
  delay(10000);


}

You got 1 of 3 problems. One other problem is that you’re using storagetankavailable == true but you want an assignment, not a test for equality.

And the last is the storagetankavaiable is missing a letter, and is also a boolean, not a string (%s). Something like this should work:

    if (digitalRead(floatinput) == HIGH) // if the input is on
    {
        storagetankavailable = true; // assign the value true to variable
    }
    else
    {
        storagetankavailable = false;
    }

   sprintf(publishString,"%s", storagetankavailable ? "true" : "false"); 
    Spark.publish("oktopump",publishString);

  // We'll wait for 10 second...
  delay(10000);

However, you probably want to rethink your publish/subscribe methodology here, because if you subscribe to your oktopump event on an Electron, you’ll use a lot of data, getting that message every 10 seconds!

thanks for your help

could you please explain this line for me as i do not fully understand it

sprintf(publishString,"%s", storagetankavailable ? "true" : "false"); 



and also thanks what is the “%s”

Cheers Ben

so what i need to do to save data is check to see if the state has changed from last time and if it has not then do not publish if it has publish and this will save data on the subscribe side?

also can we monitor if each photon or electron is online and then if not turn off the output??

Thanks again

This sprintf() and its relative printf() are standard C functions that help format strings, but a good description how these work is here

http://www.cplusplus.com/reference/cstdio/sprintf/
http://www.cplusplus.com/reference/cstdio/printf/

Sure. First of all, you could have written this more easily as

    if (digitalRead(floatinput) == HIGH) // if the input is on
    {
        publishString = "true"; // assign the value true to variable
    }
    else
    {
        publishString = "false";
    }

and gotten rid of the sprintf entirely. But since you asked…

The %s means output as a c-style string.

The storagetankavailable ? "true" : "false" expression means if storagetankavailable is true, then return the string "true" and if false return "false".

Here’s some more documentation:
https://www.codingunit.com/printf-format-specifiers-format-conversions-and-formatted-output

http://www.tutorialspoint.com/c_standard_library/c_function_printf.htm

And, yes, publishing only when the value changes would definitely help!