Skill (or a skill template) that communicates back and forth with an Arduino's serial


#1

I was thinking of making my own enclosure with a corresponding skill, however, what held me back is how the Arduino Uno and the Pi 3B+ can communicate to each other via USB (serial).

The Mark 1 example on Github is kinda confusing since it uses the Pi’s GPIO instead of the Pi’s USB port.

Searching for a pyserial example led me to something that still uses Python 2 instead of 3.

Any ideas on how this is possible?


#2

This pyserial is Python3 compatible, there is also a pypi-package available… and here is a article on instructables.com


#3

just mentioning – you do not have to use python if you like – for my TFT screen interface for mycroft - I used basic shell scripting and perl. as I do not really like python that much… for my TFT andrino/ESP interface it can talk either by serial or by network interface…


#4

I’ve been considering something like this also. Let me know how it goes.


#5

I edited a sample code for Arduino, to ensure that the serial prints a message that should send to the Pi.
Comparing to the Mark 1 skill repo, there is no serial option that listens for any button commands.
Here is the Arduino code:

// constants won't change. They're used here to set pin numbers:
const int stopButtonPin = 2;    // the number of the stopbutton pin
const int ledPin = 13;      // the number of the LED pin

// Variables will change:
int ledState = HIGH;         // the current state of the output pin
int buttonState;             // the current reading from the input pin
int lastButtonState = LOW;   // the previous reading from the input pin

// the following variables are unsigned longs because the time, measured in
// milliseconds, will quickly become a bigger number than can be stored in an int.
unsigned long lastDebounceTime = 0;  // the last time the output pin was toggled
unsigned long debounceDelay = 50;    // the debounce time; increase if the output flickers

void setup() {
  Serial.begin(9600);
  
  pinMode(stopButtonPin, INPUT);
  pinMode(ledPin, OUTPUT);

  // set initial LED state
  digitalWrite(ledPin, ledState);
}

void loop() {
  // read the state of the switch into a local variable:
  int reading = digitalRead(stopButtonPin);

  // check to see if you just pressed the button
  // (i.e. the input went from LOW to HIGH), and you've waited long enough
  // since the last press to ignore any noise:

  // If the switch changed, due to noise or pressing:
  if (reading != lastButtonState) {
    // reset the debouncing timer
    lastDebounceTime = millis();
  }

  if ((millis() - lastDebounceTime) > debounceDelay) {
    // whatever the reading is at, it's been there for longer than the debounce
    // delay, so take it as the actual current state:

    // if the button state has changed:
    if (reading != buttonState) {
      buttonState = reading;

      // only toggle the LED if the new button state is HIGH
      if (buttonState == HIGH) {
        ledState = !ledState;
        Serial.println(F("mycroft.stop"));
      }
    }
  }

  // set the LED:
  digitalWrite(ledPin, ledState);

  // save the reading. Next time through the loop, it'll be the lastButtonState:
  lastButtonState = reading;

  // prototype - will allow the serial to receive something, such as from a raspberry pi
  while(Serial.available() > 0 ){
    String str = Serial.readString();
    if(str.substring(0) == "send\r\n"){
      Serial.println("identified");
    }
    if(str.substring(0) == "light toggle\r\n"){
      Serial.println("change light status");
      ledState = !ledState;
    }
  }
}

I wonder what I did wrong in this case.


#6

Now I know what happened.
It seems that I need to design my own enclosure code first, which require some files to be made on the mycroft-core folder as shown in this part of the repository.

EDIT: I forgot, the mycroft.conf file needs to be edited. Not sure if it’s the system or the user level.


#7

You can set the conf at whatever level makes most sense, they get loaded in order so user level will always override the rest.


#8

I might also want to look into this: https://pinout.xyz/pinout/uart

However, I got mixed messages on the Internet if I should use a logic level converter between the arduino and pi, or not


#9

I decided to use a logic level converter.
However, I also tried to create my own enclosure and it gave me an error. Is this normal?
The error can be found in this image (click to enlarge):
Google Photos


#10

Hi rekkitcwts, this shouldn’t affect any operations, MSM just throws this error if it doesn’t recognise the enclosure.

You could comment it out or change the log level here if you wanted to silence it