Things used in this project

Code

the_arduino_code_for_arduinopi.cC/C++
the_arduino_code_for_arduinopi.c
int ledstate = LOW;
String cmd;
bool cmdRec = false;
void setup()
{
    //Start the connection with the Raspberry Pi
    Serial1.begin(115200);
    // Start the connection with the Laptop
    Serial.begin(115200);
    for(int i=2; i <= 10; i++) {
        pinMode(i, OUTPUT);
    }
}

void loop()
{
    handleCmd();
}

void serialEvent1() {
    while(Serial1.available() > 0 && Serial1.writeable()) {
        char inByte = (char)Serial1.read();
        if(inByte == ':') {
            cmdRec = true;
            return;
        } else if(inByte == '@') {
            cmd = "";
            cmdRec = false;
            return;
        } else {
            cmd += inByte;
            return;
        }
    }
}

void handleCmd() {
    if(!cmdRec) return;

    int data[4];
    int numArgs = 0;

    int beginIdx = 0;
    int idx = cmd.indexOf(",");

    String arg;
    char charBuffer[16];

    while (idx != -1) {
        arg = cmd.substring(beginIdx, idx);
        arg.toCharArray(charBuffer, 16);

        // add error handling for atoi:
        data[numArgs++] = atoi(charBuffer);
        beginIdx = idx + 1;
        idx = cmd.indexOf(",", beginIdx);
    }
    // And also fetch the last command
    arg = cmd.substring(beginIdx);
    arg.toCharArray(charBuffer, 16);
    data[numArgs++] = atoi(charBuffer);

    // We just want to switch a port so lets change the values
    if(data[0] < 100) {
        execCmd(data);
    } else {
        execCmds(data);
    }
    cmdRec = false;
}

// Select just one port and enable it
void execCmd(int* data) {
    analogWrite(data[0], data[1]);
}

// For advanced function like switch all the leds in RGB
void execCmds(int* data) {
    switch(data[0]) {
    case 101:
        // first the red part of the RGB
        // for me 4,7,10
        analogWrite(4, data[1]);
        analogWrite(7, data[1]);
        analogWrite(10, data[1]);
        // green: 3, 6, 9
        analogWrite(3, data[2]);
        analogWrite(6, data[2]);
        analogWrite(9, data[2]);
        // blue: 2, 5, 8
        analogWrite(2, data[3]);
        analogWrite(5, data[3]);
        analogWrite(8, data[3]);
        break;

    case 102:
        // request analog readout!
        int sensor = analogRead(data[1]);
        Serial1.println(sensor);
    }
}
hover_example.c
hover_example.c
$(".hover-light > button").hover(function () {
        var port = $(this).val();
        $.post("php-ajax/cmd.php?mode=hover", {port:port, value:255});
    }, function () {
        var port = $(this).val();
        $.post("php-ajax/cmd.php?mode=hover", {port:port, value:0});
color_picker.c
color_picker.c
$("#swatch").click(function () {
    var red = $("#red").slider("value");
    var green = $("#green").slider("value");
    var blue = $("#blue").slider("value");
    $.post("php-ajax/cmd.php?mode=picker", {red:red, green:green, blue:blue});
live_sensor_data.c
live_sensor_data.c
root@raspberrypi:/home/pi# crontab -e
* * * * * php /opt/www/php-ajax/cron.php

Credits

Replications

Did you replicate this project? Share it!

I made one

Love this project? Think it could be improved? Tell us what you think!

Give feedback

Comments

Similar projects you might like

Unlock your door with a knock
  • 15,320
  • 49

Full instructions

Open your door with a special knock using Arduino, Android Phone and 1Sheeld

DIY Relay Outlet Arduino
  • 20,623
  • 24

Full instructions

Control a wall outlet using an Arduino using this DIY solid state relay outlet.

Zolertia IPv6/6LoWPAN Ubidots client
  • 4,276
  • 13

Full instructions

A Zolertia Z1 6LoWPAN/IPv6 ultra low-power mesh network sending POST requests to Ubidots

Occupied
  • 6,577
  • 55

Full instructions

A toilet queueing app for serious poopers. Uses resin.io, firebase and twilio to notify people when the toilet is open - runs on the RPI.

Uber Home Automation
  • 25,289
  • 112

Work in progress

Wireless Arduino sensor nodes and awesome home automation platform called OpenHAB.  Know when your dog barks.

Magoo
  • 5,159
  • 26

Work in progress

An obstacle-detection and GPS-enabled navigation wearable for the visually impaired.

Magoo

4 members

ProjectsCommunitiesTopicsContestsLiveAppsBetaFree StoreBlogAdd projectSign up / Login