简体   繁体   中英

Arduino not reacting from rasperry pi serial communication

I'm really confused.

I have a Raspberry Pi 3B+ that needs to communicate to an Arduino Nano by using its GPIO pins RX & TX this is working fine except for a logic issue that I cannot figure out. I have a simple java web interface with 2 buttons, namely on and off, this interacts with a python script that needs to issue commands to the Arduino and it needs to either turn on or turn off a relay depending on the command it receives. By printing the state the arduino receives does reflect what the arduino should do (On button sends on command and Off button sends off command)

But, when I click a button the list below happens,

  1. Click the On button - nothing happens
  2. Click the Off button - relay switches on
  3. Click the Off button - nothing happens
  4. Click the On button - nothing happens
  5. Click the Off button - relay switches off

I printed the state of the pin to which the relay is connected and its state does weird stuff,

  1. On - state = 0 (No switching)
  2. Off - state = 1 (Switching)
  3. Off - state = 0 (No Switching)
  4. On - state = 1 (No switching)
  5. Off - state = 0 (Switches)
It makes no sense, here is what's happening from the python script
#define relayPin 8
String command;
void setup() {
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
}

void loop() {
  if(Serial.available()){
    command = Serial.readStringUntil('\n');
    command.trim();
    if(command == "on"){
      turnOn();
    }

    if(command == "off"){
      turnOff();
    } 
  }
}

void turnOn(){
  print();
  if (digitalRead(relayPin) == 0) {
    digitalWrite(relayPin, HIGH);
  }  
}

void turnOff(){
  print();
  if (digitalRead(relayPin) == 1) {
    digitalWrite(relayPin, LOW);
  }
}

void print(){
   Serial.println("Pin State: ");
   Serial.print(digitalRead(relayPin)); 
   Serial.println();
}

For the Arduino I have the following

#define relayPin 8
String command;

void setup() {
  Serial.begin(9600);
  pinMode(relayPin, OUTPUT);
}

void loop() {
  if(Serial.available()){
    command = Serial.readStringUntil('\n');
    command.trim();
    if (command.equals("on")) {
      digitalWrite(relayPin, HIGH);
    }
    else if (command.equals("off")) {
      digitalWrite(relayPin, LOW);
    }
  }
}

Initially I had this, and used a USB cable for communication and it worked perfectly

#define relayPin 8 String command; void setup() { Serial.begin(9600); pinMode(relayPin, OUTPUT); } void loop() { if(Serial.available()){ command = Serial.readStringUntil('\\n'); command.trim(); if (command.equals("on")) { digitalWrite(relayPin, HIGH); } else if (command.equals("off")) { digitalWrite(relayPin, LOW); } } }

I used this tutorial to configure the raspberry to allow serial communication by means of GPIO

https://github.com/Arijit1080/Raspberry-Pi---Arduino-Serial-Communication[tutorial][1]

and in this I did everything that the instructions.txt specified, namely

1. sudo nano /boot/config.txt dtparam=spi=on dtoverlay=pi3-disable-bt core_freq=250 enable_uart=1 force_turbo=1 2. sudo cp /boot/cmdline.txt /boot/cmdline_backup.txt 3. sudo nano /boot/cmdline.txt dwc_otg.lpm_enable=0 console=tty1 root=/dev/mmcblk0p2 rootfstype=ext4 elevator=deadline fsck.repair=yes rootwait quiet splash plymouth.ignore-serial-consoles 4. sudo reboot 5. ls -l /dev sudo systemctl stop serial-getty@ttyAMA0.service sudo systemctl disable serial-getty@ttyAMA0.service 6. sudo apt-get install minicom 7. minicom -b 9600 -o -D /dev/ttyAMA0

And this is how everything is wired up

在此处输入图片说明

What could the problem be?

It turns out that the relay is the main problem, when I give it a HIGH signal, the coil does not stay energized until it receives a LOW signal as my previous relay does, instead, I need to send a pulse signal if you will, when I do the following it seems to work as expected

#define r 8
String command;
void setup() {
  Serial.begin(9600);
  pinMode(r, OUTPUT);
}

void loop() {
  command = Serial.readStringUntil("/n");
  command.trim();
  if (action.equals("switch")) {
    digitalWrite(r, HIGH);
    delay(100);
    digitalWrite(r, LOW);
  }
}

When I send a command switch it energizes the coil and releases it again, this causes the relay to switch, when I send the same command, it then switches back. As mentioned, the coil doesn't stay energized so that I can see what state it's in.

I only need to figure out a way to get some sort of status back to see what the relay is doing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM