Figuring Out Thinger Platform To Control Raspberry Pi LED

Hello,

for a college project we are using thinger.io for the first time and we’ve been studying manuals and resources about the possibilities with the platform.

We’ve only had some experience with solely working with a Raspberry Pi and coding with Python. We have followed the Guide on how to establish a connection from the Raspberry Pi with the Thinger dashboard and this works 100%.

Our project idea is to control a LED (on/off) from the platform and eventually expand towards the possibility of adjusting the intensity of the LED.

We have trouble getting started on this type of action but we continue experimenting and do our research. Most of the guides that I find are based off Arduino devices or other type of devices which makes it a little confusing.

From my understanding we have to run a python script that fires off a LED while the main.cpp is configured to send that LED data to the interface. We just don’t understand how we would have to configure that main.cpp on a Raspberry Pi.

We would be very thankful for some type of provision or guide that would be compatible with the Raspberry Pi so we could progress with our project.

In the meantime we will be researching more this amazing interface.

Hi! @Maicoh! Welcome to the community forum :slight_smile:

I am glad you are using the platform for a college project. Turning on and off a led with the Raspberry Pi is quite similar to the Arduino code. Yo have different options here to integrate the pin control from a C++ program. You can issue a system call to enable/disable the pin, like this example, or you can integrate a code that modifies the microprocessor registers directly, which is much more efficient and fast, like this other example..

In this case, in this example I will use mmapGpio, but there are other alternatives like WiringPi. So feel free to use any alternative.

The reference code for using mmapGpio for driving a led (in this case in GPIO_17) from a Raspberry Pi, can be something like the following code (I explain more below and provide a example project):

#include "thinger/thinger.h"
#include "mmapGpio.h"

#define USER_ID             "YOUR_USER_ID"
#define DEVICE_ID           "YOUR_DEVICE_ID"
#define DEVICE_CREDENTIAL   "YOUR_DEVICE_CREDENTIAL"

#define LED_PIN 17

// instantiate an instance of the mmapGpio class
mmapGpio rpiGpio;

int main(int argc, char *argv[])
{
    // initialize thinger.io device
    thinger_device thing(USER_ID, DEVICE_ID, DEVICE_CREDENTIAL);

    // set GPIO17 to output
    rpiGpio.setPinDir(LED_PIN, mmapGpio::OUTPUT); 

    // define a led resource so we can toggle it
    thing["led"] << [](pson& in){
    	// if the input is empty means that we are reading its current value, so fill the input with the current state of the pin
        if(in.is_empty()){
        	in = rpiGpio.readPin(LED_PIN) ? true : false;
        // if the input is not empty, means that we have a command to change the state
        }else{
        	// "in" can be of any type, but it will be of type boolean as we are returning a true/false while reading its state
        	if(in){
        		rpiGpio.writePinHigh(LED_PIN);
        	}else{
        		rpiGpio.writePinLow(LED_PIN);
        	}
        }
    };

    thing.start();
    return 0;
}

Basically you must modify the stock linux example project structure by copying the mmapGpio.h and mmapGpio.cpp to the src folder, and then add to the CMakeLists.txt the file mmapGpio.pp for its compilation. Then it is required to modify the main.cpp file to add the code for turning on/off leds.

Notice that depending on your Raspberry Pi version, you must modify the mmapGpio.h to comment the #define RASPBERRYPI2 if you are using version 1, or leave as it comes if you are using a version 2. Not sure what happens if you are using a Rpi 3, but I think it will work as the version 2.

Here you have the source project for testing (you can compile in the same way as the stock Raspberry example, but remember to modify your user/device credentials). I have not tested the project yet, so I am not sure if it works or compile. Will try soon with my raspberry. In the meanwhile we can fix any problem if you cannot get it working.

Raspberry-Pi-Led-Example.zip (43.0 KB)

3 Likes

Waw!! Thank you a lot for this information @alvarolb ! I will play with it when I get home.

1 Like

@alvarolb We’ve finally been able to spend time to apply these settings. I applied your exact method but something appears to be wrong. I get this error message:

I’ve also tried to replace my own source project with yours but I receive the exact same of error >_<
Maybe I am using a wrong input on my RaspberryPi?

Hello, i’m trying to execute the same code, but i’m getting the same error.


Would you @alvarolb please help!

Regards

We are still experimenting now and tried to include wiringPi instead of mmapGpio by installing it in the “thinger_client” folder and programming a blinking LED function again inside the src/main.cpp file. It seems like wiringPi isn’t compatible since we get this error. I don’t know what could be missing to fix it. Just thought I’d share it here.

Hi @Maicoh!

I have updated the code I posted the other day. Check the new version and try again. I think this one will compile :slight_smile:

Regarding your version with WiringPi, you are probably missing linking the WiringPi library in the compile process (this should be done through the CMakeList.txt). This way I selected the other approach that does not require any external libraries. But looking at the WiringPi interface, I think I should make RaspberryPi version example for this library, as it seems to be quite easy!

1 Like

I tried it and it worked. figured it out yesterday.

So… can you turn on/off the led using the Raspberry Pi?

Yes I did it works just fine. I been searching a lot and then finally found it. You just have to compile it with wiringPi.

1 Like
1 Like

Great! Good work @thaisah.vanvugt!! it works!! :blush: would you share your project in the community?

You can even try to add to your dashboard a on/off button to turn on and off the led! It is very easy!

I tried but with me it didn’t work. How did you do this?

Great! Would you plz share your project in the community/github?

Hello
I am having a compilation error in my main.cpp program. Trying to compile using wiringPi. Did thaisah.vanvugt posted her code? i would help a lot :slight_smile:
Thanks in advance

Hi @jose990115, share your code/error here, so we can fix it, for sure! :slight_smile:

Is the video still available? Video seems to be private

I am trying to control an LED with a Raspberry Pi. I get the same error in Thaisah’s screen shot “Undefined reference to ‘wiringPiSetup’”. Thaisah said:

but I’m not sure what exactly the change was I have “-lwiringPi” added to my CMakeLists.txt file as suggested.
I am able to use wiringPi to make an LED blink locally on the Pi, so I think I have wiringPi correctly installed, but somehow thinger isn’t finding it. Any help would be appreciated.