Learn how to control LEDs connected to a Raspberry Pi through the machine learning program Wekinator!

In this article, part 9 of a larger machine learning series, you are going to learn to control the Raspberry Pi using machine learning through the Wekinator platform. Wekinator is a platform for doing machine learning projects. We can train Wekinator by giving it inputs and it will give us output accordingly.

We will give the input to the Wekinator through processing, and after training, it will give us the output through which we will control the LED connected to the Raspberry Pi’s GPIO pins.

Catch up on the rest of the Wekinator Machine Learning series here:

Circuit Diagram

First of all, connect the positive leg of the LED to GPIO4 of Raspberry Pi, then connect the negative leg of LED to the ground pin of Raspberry Pi through the 220-ohm resistor.

image1.png

Installing the Software

You need to install the processing IDE on your Raspberry Pi. Download processing for Linux ARMv6hf here.

image3.png

After downloading it, place it into the folder where you want to install processing. Then, right click on it and click “extract here”. Now a folder will be created called “processing”. Right click on this folder and select “Open in terminal”. A terminal window will open up. Type the below command in it and the processing IDE will open up.

./processing

Now you need to install the Wekinator software. So go to the below link and click on “any OS, including Linux”.

image2.png

After downloading, place it where you want to install it, then right click on it and click “extract here”. A new directory will be created. Right click on it and select “Run in terminal”. In terminal, write the following command, and Wekinator will start running.

java -jar WekiMini.jar

Getting Started

Now paste the code given at the end of this post in two separate processing sketches and run these. In the Wekinator window, make the settings as shown below. Set the inputs and outputs to 1 and the type to “All classifiers” with 2 classes.

image5.png

Click on “next” and a new window will open up as shown below.

image4.png

Open the processing window, click on the green triangle, and start the recording for half a second. Click on the red circle and change the class to 2 (in front of output-1). Then start the recording for half a second.

After that, click on “Train” and then click on “Run”. Now when you will click on the green triangle, the LED connected to the Raspberry Pi will light up, and when you will click on the red circle, the LED connected to the Raspberry Pi will turn OFF.

Processing Code (Input to Wekinator)

    // Importing the library which will help us in communicating with the wekinator
import oscP5.*;
import netP5.*;

//creating the instances
OscP5 oscP5;
NetAddress dest;

float bx;

void setup() {
  // Size of output window
  size(200, 50, P3D);
    
 // Starting the communication with wekinator. listen on port 9000, return messages on port 6448
  oscP5 = new OscP5(this,9000);
  dest = new NetAddress("127.0.0.1",6448);
}

void draw() {
  // Creating the boxes in output window
  blocks();
  
  // Send the OSC message to wekinator
  sendOsc();
}

void mousePressed() 
{
  // If mouse is pressed in the first box
  if (mouseX > 0 && mouseX < 50)
  {
    bx=1;    
  }
  
  // If mouse is pressed in the second box
  if (mouseX > 100 && mouseX < 150)
  {
    bx=2;    
  }
  
}

void sendOsc() {
  OscMessage msg = new OscMessage("/wek/inputs");
  msg.add((float)bx); 
  oscP5.send(msg, dest);
}

void blocks()
{
  background(0);
  fill(0, 128, 0);
  ellipse(25, 25, 50, 50);
  fill(255);
  text("ON", 10, 30);
  
  fill(255, 0, 0);
  ellipse(125, 25, 50, 50);
  fill(255);
  text("OFF", 120, 30);
}

Processing Code (Output From Wekinator)

    // Importing the library that will help us in controlling the GPIO pins of raspberry pi
import processing.io.*;

// Importing the library which will help us in communicating with the wekinator
import oscP5.*;  
import netP5.*;

// Creating the instances
OscP5 oscP5;
NetAddress dest;

// Variable to store the output 
public int output;

void setup() 
{
  // Setting the GPIO 4 as output pin
   GPIO.pinMode(4, GPIO.OUTPUT);
  
  
  // Starting the communication with wekinator. listen on port 12000, return messages on port 6448
  oscP5 = new OscP5(this, 12000); 
  dest = new NetAddress("127.0.0.1", 6448);
}


// Recieve OSC messages from Wekinator
void oscEvent(OscMessage theOscMessage) {
  if (theOscMessage.checkAddrPattern("/wek/outputs") == true) {
    // Receiving the output from wekinator
    float value = theOscMessage.get(0).floatValue();  
    
    // Converting the output to int type
      output = int(value);    
  }
}

void draw() 
{
  // Making the led HIGH or LOW depending on the output from the wekinator
  if (output == 1)
  {
    GPIO.digitalWrite(4, GPIO.HIGH);
  }
  else if (output == 2)
  {
    GPIO.digitalWrite(4, GPIO.LOW);    
  }
}
Muhammad Aqib

Maker Pro Logo
Continue to site
Quote of the day

-