Golang and Arduino

Small arduino and golang programs

Control relay with Raspberry PI in python in web

Control relay via web on raspberry Pi

First you need to install some dependencies

apt-get install python-rpi.gpio python3-rpi.gpio python-flask python3-flask

after that, create file: app.py and paste following code to it. edit MYRELAY to your pin ( in my example i am using 8 physical pin ) 


from flask import Flask
import RPi.GPIO as GPIO

MYRELAY = 8

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
GPIO.setup(MYRELAY, GPIO.OUT, initial=GPIO.LOW)

app = Flask(__name__)

@app.route("/on")
def turnon():
    GPIO.output(MYRELAY, GPIO.HIGH)
    return "OK"

@app.route("/off")
def turnoff():
    GPIO.output(MYRELAY, GPIO.LOW)
    return "OK"

if __name__ == "__main__":
    app.run(host="0.0.0.0",port='8080')

run

python3 app.py

now, ping your Raspberry Pi via http://ip:8080/on to turn Relay ON and http://ip:8080/off to turn relay off

Make a simple scheduler for arduino

After some googling, i decided to try to write and arduino library to simplify use of "millis" trick to schedule jobs with 'clean api'.

Here is some code of blink:


#include "SimpleScheduler.h"

void ledOn();
void ledOff();

void ledOn() {
  digitalWrite(LED_BUILTIN, HIGH);
  schedule(500, ledOff);
}
void ledOff() {
  digitalWrite(LED_BUILTIN, LOW);
  schedule(500, ledOn);
}

void setup() {
  pinMode(LED_BUILTIN, OUTPUT);
  ledOn();
}

void loop() {
  scheduler_run();
}

you can find library at github arduino SimpleScheduler

arduino parse hex colors to numbers


void setup() {
    Serial.begin(9600);

    // get data from Serial.readStringUntil('\n'); to memory
    String data="FFFFFFFF 00FFFFFF 0000FFFF 000000FF";
    // store length of data
    int iend = data.length() - 1; 
    // current begin index
    int index=0;
    // next index
    int next_index;
    do{
        next_index=data.indexOf(' ',index);

        String hexcode=data.substring(index, next_index);

        index=next_index+1;

        unsigned long bigNumber = (unsigned long)strtoul(hexcode.c_str(), 0, 16);

        // add number to array

        Serial.println(bigNumber);

     }while((next_index!=-1)&&(next_index<iend));
}

void loop() {

}

python evdev or how to read usb keyboard input in python

from evdev import InputDevice, categorize, ecodes

def ky(num):
    keys = {
        71: "7",
        72: "8",
        73: "9",
        75: "4",
        76: "5",
        77: "6",
        79: "1",
        80: "2",
        81: "3",
        82: "0",
        96: "enter"
    }
    return keys.get(num)

device = InputDevice("/dev/input/by-path/platform-fd500000.pcie-pci-0000:01:00.0-usb-0:1.1:1.0-event-kbd") # my keyboard
for event in device.read_loop():
    if event.type == ecodes.EV_KEY:
        if (event.value == 1):
            print(ky(event.code))

all you need is to find right path for your InputDevice 

ansi C unions on arduino

After some time of googling, i found interesting solution, how to get and set pins as bits in arduino


union myunion {
    struct {
        unsigned int b7: 1;
        unsigned int b6: 1;
        unsigned int b5: 1;
        unsigned int b4: 1;
        unsigned int b3: 1;
        unsigned int b2: 1;
        unsigned int b1: 1;
        unsigned int b0: 1;
    };
    unsigned int num;
} mu;

with this union with struct, we can read pins to 1 big number invidual bits and display it on for example - led's in binary.


mu.b0 = digitalRead(2);
mu.b1 = digitalRead(3);
mu.b2 = digitalRead(4);
mu.b3 = digitalRead(5);
mu.b4 = digitalRead(6);
mu.b5 = digitalRead(7);
mu.b6 = digitalRead(8);
mu.b7 = digitalRead(9);

and vuola, now we can get number with "mu.num" depending on HIGH pins 


Serial.println(mu.num);

and in reverse, we can set "mu.num = 127" and write individual bits to pins.


digitalWrite(mu.b0);
digitalWrite(mu.b1);
digitalWrite(mu.b2);
digitalWrite(mu.b3);
digitalWrite(mu.b4);
digitalWrite(mu.b5);
digitalWrite(mu.b6);
digitalWrite(mu.b7);

whole program:


// parallel to serial
// serial to parallel
union mu1 {
    struct {
        unsigned int b7: 1;
        unsigned int b6: 1;
        unsigned int b5: 1;
        unsigned int b4: 1;
        unsigned int b3: 1;
        unsigned int b2: 1;
        unsigned int b1: 1;
        unsigned int b0: 1;
    };
    unsigned int num;
} mu;

void setup() {
  Serial.begin(9600);
}

void loop() {
  mu.b0 = digitalRead(2); // switch 1
  mu.b1 = digitalRead(3); // switch 2
  mu.b2 = digitalRead(4); // switch 3
  mu.b3 = digitalRead(5); // switch 4
  mu.b4 = digitalRead(6); // switch 5
  mu.b5 = digitalRead(7); // switch 6
  mu.b6 = digitalRead(8); // switch 7
  mu.b7 = digitalRead(9); // switch 8

  Serial.println(mu.num);
  delay(5000);
}

thats it. union acts as "shift in" and "shift out" register, if you set mu.num than you can read individual bits(pins) if you set pins you can read mu.num and it works then like shift in register.