Hi! @Maicoh! Welcome to the community forum
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)