Slack Button

Background

I have an automated measuring system in the lab. I wanted to start the measurement in style. Thus, I needed a 10cm red button capable of launching ballistic missiles. The button had to be wireless (naturally), so the rig could be started from anywhere in the room. I aquired a LinkItONE, with a battery and bluetooth module. Normally one would connect the arduino directly to wifi, but our friend eduroam prohibits this (as it is very difficult to install the required certificates on arduino). My solution is to connect the button to a low energy bluetooth chat system, which communicates with an android phone. The phone then recieves bluetooth messages from the arduino and communicates this to the cloud, in this case, posting to Slack. But generally, the potentials for remote connectivity are two way, and limitless.



Equipment and Prep

Android phone, Arduino or similiar - LinkItONE (with battery and bluetooth module), button, wires and 3D printer. The button I used was this one , and my 3D print to contain it can be found from the repository on Git. The link it one requires some installation into the arduino desktop app, but is easily done online .

Arduino Code

Make sure you have installed and include the LinkItONE server and battery header files. Make sure you have connected the bluetooth antenna. This means you can set everything up very easily. I haven't hooked up the LED in the final version, but I included it for testing.

       #include <LBT.h>
       #include <LBTServer.h>
       #include <LBattery.h>

       char buff[256];
       int ledPin = 13;  //Pin the LED is connected to on the LinkIt ONE
       int buttonPin = 2;
       String command;

       int buttonState = 0;
       int connectedblue = 0;


       void setup() {
         // put your setup code here, to run once:
       Serial.begin(9600);
       LBTServer.begin((uint8_t*)"My_BTServer");
       pinMode(ledPin,OUTPUT);
       pinMode(buttonPin, INPUT);
       digitalWrite(ledPin,LOW);
       }

       void loop() {
         // put your main code here, to run repeatedly:

       if(LBTServer.connected())
       {
         // Serial.println("Connected");  //If bluetooth is connected print "Connected" in serial port
         connectedblue = 1;
         }
       else
       {
         Serial.println("Retrying");   //Else retry
         LBTServer.accept(5);
         }



       if(LBattery.level()<=33){
           Serial.println("Low Battery ");
             LBTServer.write("Low Battery - CHARGE ME");

        }
         else{
           Serial.println("Fine Battery");
         }


       buttonState = digitalRead(buttonPin);

       if (buttonState == LOW){
         digitalWrite(ledPin,LOW);   //If command is "On", switch LED on
         Serial.println("LED is on");
         if (connectedblue==1){
         LBTServer.write("LED is on");
         }
         delay(1000);
         digitalWrite(ledPin,LOW);
         }
       else if (buttonState == HIGH){
         digitalWrite(ledPin,HIGH);   //If command is "Off", switch LED off
         Serial.println("LED is off");
       //   if (connectedblue==1){
       //  LBTServer.write("LED is off000");
       //  }

         }
       }


     

Android Code


There is a whole lot going in the bluetooth communication. Best to work from what people have done before, so I just modified the existing Bluetooth chat example . You can then just modify the code where the app recieves information from bluetooth. In fact, to make it easier and not worry about decoding messages, decisions are made depending on the number of bytes are sent. Anyway, install android studio, link up your chosen android device (this will not work in the emulator). Put the two following functions in the BluetoothChatService.java file. Then create a slack app here , get your webhook url, and put it in the code where I have written ***INSERT URL HERE *** .

Talking to slack function

  public class HTTPRequestTask extends AsyncTask {

         @Override
         protected Boolean doInBackground(Void... params) {
             android.util.Log.w("myApp", "I am here");

             try {
               try {


                String json = "{\"text\":\"LED is ON \"}";


                URL url = new URL("***INSERT URL HERE ***");
                HttpsURLConnection httpsURLConnection = (HttpsURLConnection) url.openConnection();
                httpsURLConnection.setRequestMethod("POST");
                httpsURLConnection.setDoOutput(true);
                httpsURLConnection.setDoInput(true);

                httpsURLConnection.setRequestProperty("Content-Type", "application/json");
                httpsURLConnection.setRequestProperty("Accept", "application/json");
                httpsURLConnection.connect();
                android.util.Log.w("myApp", "I have connected");

                byte[] outputBytes = json.getBytes("UTF-8");
                OutputStream os = httpsURLConnection.getOutputStream();
                os.write(outputBytes);
                os.close();

                // read the response

                InputStream in = new BufferedInputStream(httpsURLConnection.getInputStream());
                BufferedReader reader = new BufferedReader(new InputStreamReader(in));
                StringBuilder result = new StringBuilder();
                String line;
                while((line = reader.readLine()) != null) {
                    result.append(line);
                }
                System.out.println(result.toString());

                in.close();
                httpsURLConnection.disconnect();


            } catch (MalformedURLException e) {
                android.util.Log.w("myApp", "I have failed 1");
                e.printStackTrace();

            } catch (IOException e) {
                android.util.Log.w("myApp", "I have failed 2");
                e.printStackTrace();

            }

            return true;
        }
    }




Recieving Messages

public void run() {
        Log.i(TAG, "BEGIN mConnectedThread");
        byte[] buffer = new byte[1024];
        int bytes;

        // Keep listening to the InputStream while connected
        while (mState == STATE_CONNECTED) {
            try {
                // Read from the InputStream
                bytes = mmInStream.read(buffer);


                Log.i("woo, I got a message",bytes+""+bytes);

                if(bytes==9) {
                  Log.i("Tomi", "True");
                  HTTPRequestTask testTask =new HTTPRequestTask();
                  testTask.execute();
                }

                if(bytes==10) {
                    Log.i("woo, I got a message2", "True");
                    HTTPRequestTask2 testTask =new HTTPRequestTask2();
                    testTask.execute();
                }


// Send the obtained bytes to the UI Activity
                mHandler.obtainMessage(Constants.MESSAGE_READ, bytes, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "disconnected", e);
                connectionLost();
                break;
            }
        }
    }