Raspberry Pi Electron, Master/Slave Communication

Hi Guys,
So I have been able to get my Pi to send messages successful over and over to the Electron.
Pi = Master
Electron = Slave
that is the way it is setup at this time.

When I try to make a request to the Electron to send me a queued message I am struggling

    include <Wire.h>
    define MyAddress 0x40

    byte DataToBeSend[1];
    bool OutgoingMessageNeedsProcessing = false;
    bool IncommingMessageNeedsProcessing = false;
    bool IncommingMessageRequestHelp = false;
    bool GPSMessageNeedsProcessing = false;
    bool MessageStart = false;
    String aString;
    String aStringToSend;
    String LastTrailHelp;
    void setup()
    {
        /* Initialize I2C Slave & assign call-back function 'onReceive' on 'I2CReceived'*/
        Wire.begin(MyAddress);
        Wire.onReceive(I2CReceived);
        Wire.onRequest(I2CRequest);
       // Particle.publish("GPSData", "Start Up Applicationv5", PRIVATE);
        Particle.subscribe("Help", HelpHandler);


    }

    void HelpHandler(const char *event, const char *data)
    {
        LastTrailHelp = String(data);
    }

    void loop()
    {
        
        if(OutgoingMessageNeedsProcessing)
            {
              if(GPSMessageNeedsProcessing)
                {
                    GPSMessageNeedsProcessing=false;
                    aStringToSend = aStringToSend.replace("~ENDGPS~","").replace("~STARTGPS~","");
                    Particle.publish("GPSData", aStringToSend, PRIVATE);
                    aStringToSend="";
                    delay(1000);
                }
                else
                {
                  
                }
                OutgoingMessageNeedsProcessing=false;
                    
            }
          
        delay(1000);
    }


    /* This function will automatically be called when RPi2 sends data to this I2C slave */
    void I2CReceived(int NumberOfBytes)
    {
      //  Serial1.write("Data Recieved");
        /* WinIoT have sent data byte; read it */

     while(0 < Wire.available()) { // loop through all
     
          String wire = ConvertToAscii(Wire.read());
          aStringToSend = aStringToSend + wire;
        }
        DataToBeSend[0] =2;
        if(strstr(aStringToSend,"~ENDGPS~"))
        {
            MessageStart=false;
            OutgoingMessageNeedsProcessing=true;
            GPSMessageNeedsProcessing=true;
            DataToBeSend[0] =1;
            Wire.write(DataToBeSend,1);
        }
        else if(strstr(aStringToSend,"~RM:HELP~"))
        {
    //Would send data to the PI here
            Wire.write("~START~");   
            Wire.write("NO DATA");
            Wire.write("~END~");
        }

          

      

    }

    void SendRequestData()
    {


        
               
        
    }

    /* This function will automatically be called when RPi2 requests for data from this I2C slave */
    void I2CRequest()
    {
        /*Send data to WinIoT */
        Wire.write(DataToBeSend,1);
        
    }


    String ConvertToAscii(char c)
    {
        switch (c)
        {
           case 32:
            return " ";
            break;
           case 33:
            return "!";
            break;
           case 34:
            return "\"";
            break;        
            case 35:
            return "#";
            break;        
            case 36:
            return "$";
            break;        
            case 37:
            return "%";
            break;        
            case 38:
            return "&";
            break;        
            case 39:
            return "'";
            break;        
            case 40:
            return "(";
            break;        
            case 41:
            return ")";
            break;        
            case 42:
            return "*";
            break;        
            case 43:
            return "+";
            break;        
            case 44:
            return ",";
            break;        
            case 45:
            return "-";
            break;        
            case 46:
            return ".";
            break;        
            case 47:
            return "/";
            break;        
            case 48:
            return "0";
            break;        
            case 49:
            return "1";
            break;        
            case 50:
            return "2";
            break;        
            case 51:
            return "3";
            break;        
            case 52:
            return "4";
            break;        
            case 53:
            return "5";
            break;        
            case 54:
            return "6";
            break;        
            case 55:
            return "7";
            break;        
            case 56:
            return "8";
            break;        
            case 57:
            return "9";
            break;        
            case 58:
            return ":";
            break;        
            case 59:
            return ";";
            break;        
            case 60:
            return "<";
            break;        
            case 61:
            return "=";
            break;        
            case 62:
            return ">";
            break;        
            case 63:
            return "?";
            break;        
            case 64:
            return "@";
            break;        
            case 65:
            return "A";
            break;        
            case 66:
            return "B";
            break;        
            case 67:
            return "C";
            break;        
            case 68:
            return "D";
            break;        
            case 69:
            return "E";
            break;        
            case 70:
            return "F";
            break;        
            case 71:
            return "G";
            break;        
            case 72:
            return "H";
            break;        
            case 73:
            return "I";
            break;        
            case 74:
            return "J";
            break;        
            case 75:
            return "K";
            break;        
            case 76:
            return "L";
            break;        
            case 77:
            return "M";
            break;        
            case 78:
            return "N";
            break;        
            case 79:
            return "O";
            break;        
            case 80:
            return "P";
            break;        
            case 81:
            return "Q";
            break;        
            case 82:
            return "R";
            break;        
            case 83:
            return "S";
            break;        
            case 84:
            return "T";
            break;        
            case 85:
            return "U";
            break;        
            case 86:
            return "V";
            break;        
            case 87:
            return "W";
            break;        
            case 88:
            return "X";
            break;        
            case 89:
            return "Y";
            break;        
            case 90:
            return "Z";
            break;        
            case 91:
            return "[";
            break;        
            case 92:
            return "\\";
            break;        
            case 93:
            return "]";
            break;        
            case 94:
            return "^";
            break;        
            case 95:
            return "_";
            break;        
            case 96:
            return "'";
            break;        
            case 97:
            return "a";
            break;   
            case 98:
            return "b";
            break;   
            case 99:
            return "c";
            break;   
            case 100:
            return "d";
            break;   
            case 101:
            return "e";
            break;   
            case 102:
            return "f";
            break;   
            case 103:
            return "g";
            break;   
            case 104:
            return "h";
            break;   
            case 105:
            return "i";
            break;   
            case 106:
            return "j";
            break;   
            case 107:
            return "k";
            break;   
            case 108:
            return "l";
            break;   
            case 109:
            return "m";
            break;   
            case 110:
            return "n";
            break;   
            case 111:
            return "o";
            break;   
            case 112:
            return "p";
            break;   
            case 113:
            return "q";
            break;   
            case 114:
            return "r";
            break;   
            case 115:
            return "s";
            break;   
            case 116:
            return "t";
            break;   
            case 117:
            return "u";
            break;   
            case 118:
            return "v";
            break;   
            case 119:
            return "w";
            break;   
            case 120:
            return "x";
            break;   
            case 121:
            return "y";
            break;   
            case 122:
            return "z";
            break;   
            case 123:
            return "{";
            break;   
            case 124:
            return "|";
            break;   
            case 125:
            return "}";
            break;   
            case 126:
            return "~";
            break;   
            default:
                return "~~~";
                break;

        }


    }

What I want to do is take a subscription message and send it to the PI however i think I need to and queue it and the PI will need to query and pull the data…

Does anyone know a better way for 2 way communication to where the electron could init a message to be sent to the pi vs the pi pulling it.

I’m not quite sure what that massive switch() statement is supposed to do.
What’s the advantage of this over

String ConvertToAscii(char c) {
  if (' '  <= c && c <= '~') return String(c);
  else return "~~~";
}  

Since all your cases aren’t really doing anything else then

  if ( c == 'A' ) return "A";  // 'A' = 65 = 0x41
1 Like

Ya not the best with C have not used it for almost 20 years… Now that you point that out a very DOH moment lol… I will making a change to that

Any thoughts on the master/slave… Slave init communicate over i2c to the master? is it possible

1 Like

What language are you using on the Pi?

I am in a similar situation using the python mraa library on a beaglebone black. After a lot of searching, it turns out this library will only take the role of a master in python. I really could use it as a slave as well. The one thing we can do is sacrifice another digital or analog pin. The Pi/BBB could listen for the pin to transition state and then the master could then poll the slave for data it wants to transmit. Because the Electron and Pi/BBB operate at different reference voltages, you will want to place a level shifter between the two pins.

Otherwise, for now I am in a polling situation.

AFAIK on an I2C bus only a master can initiate a communication but you can have multiple masters on a bus and role switching is allowed too.

Food for thought…there’s an EasyTransfer library that makes serial communications pretty simple. I had a need for a Photon to send data locally to an Electron which subsequently would publish it to the cloud. EasyTransfer, as the name implies, made this pretty easy.