Create a "GG Button" With an IOT Button on Discord

I was having some fun with my internet button (found here: https://store.particle.io/collections/shields-and-kits), and also working on some bot coding.

So I decided to bring the two together; with the goal of pressing a button to summon a bot to my channel to play a sound effect(s). This would be similar to the famous https://www.ggbutton.com/ button, but would play a sound to everyone I am in a Discord channel with.

Since I was already working with Darkside's SoundBoard bot for Discord; I decided to utilize the API. That way I'd be creating a simple web hook that could later be used for other purposes; like having Jenkins run a build/deploy.

You can see the completed work in action:


This comes from this source code:

1:  // This #include statement was automatically added by the Particle IDE.  
2:  #include <HttpClient.h>  
3:    
4:  // This #include statement was automatically added by the Particle IDE.  
5:  #include <InternetButton.h>  
6:    
7:  /**  
8:   * George Frick  
9:   *   
10:   * This application will play a certain sound effect in a voice channel  
11:   * when the button is pressed.  
12:   *   
13:   * A simple http post when a button is pressed.  
14:   * You don't have to go through the particle API.  
15:   *   
16:   * In this example, we post to a Discord sound effect bot.   
17:   * @see https://github.com/Darkside138/DiscordSoundboard  
18:   *   
19:   * You also need the above two libraries, HttpClient and InternetButton  
20:   * https://github.com/spark/InternetButton  
21:   * https://github.com/nmattisson/HttpClient  
22:   *  
23:   * To use the Serial output, have your internet button connected via usb  
24:   * and type 'particle serial monitor'  
25:   */  
26:    
27:  HttpClient http;  
28:  InternetButton b = InternetButton();  
29:  bool mashBlocker = false;  
30:    
31:  // Headers currently need to be set at init, useful for API keys etc.  
32:  http_header_t headers[] = {  
33:    { "Content-Type", "application/x-www-form-urlencoded" },  
34:    // { "Accept" , "application/json" },  
35:    { "Accept" , "*/*"},  
36:    { NULL, NULL } // NOTE: Always terminate headers will NULL  
37:  };  
38:    
39:  http_request_t request;  
40:  http_response_t response;  
41:    
42:  void setup() {  
43:    b.begin(); // pass 1 for older buttons (no shield)  
44:    b.setBrightness(50); // limit overall brightness.  
45:    Serial.begin(9600);  
46:  }  
47:    
48:  void loop(){  
49:    
50:    if(b.buttonOn(2) && mashBlocker == false) {  
51:      b.ledOn(3, 255, 255, 255);  
52:      mashBlocker = true;  
53:        
54:      Serial.println();  
55:      Serial.println("Making web request...");  
56:    
57:      request.hostname = "192.168.1.193";  
58:      request.port = 8080;  
59:      request.path = "/soundsApi/playFile";  
60:      request.body = "soundFileId=sms1&username=gfrick-test";  
61:      
62:      // Post request, it blocks. (blocking i/o)  
63:      http.post(request, response, headers);  
64:    
65:      Serial.print("Application>\tResponse status: ");  
66:      Serial.println(response.status);  
67:      
68:      Serial.print("Application>\tHTTP Response Body: ");  
69:      Serial.println(response.body);  
70:    
71:      b.ledOff(3);  
72:      mashBlocker = false;  
73:    }  
74:    
75:  }  

This was a quick and fun little project, and it gets me started on doing IOT based dev ops and more!

Comments

Popular posts from this blog

Arduino Robot 1 - The Basic Tank

Fixing a dead Sansa MP3 player (Versions e200 e260 e280)

Arduino Robot 2 - Wandering Tank