树莓派和arduino的恩恩怨怨

一、负责与树莓派互动的putty的会话经常断,不爽:

参考:【SSH】SSH自动断开连接的原因和解决办法|SSH保持长连接方法_ssh连上几秒就断开了_bandaoyu的博客-CSDN博客

用nano

1、修改 /etc/profile中改MOUT的值:export TMOUT=0

echo $TMOUT

如果显示空白,表示没有设置, 等于使用默认值0, 一般情况下应该是不超时. 如果大于0, 可以在如/etc/profile之类文件中设置它为0.

Definition: TMOUT: If set to a value greater than zero, the value is interpreted as the number of seconds to wait for input after issuing the primary prompt. Bash terminates after waiting for that number of seconds if input does not arrive

2、修改/etc/ssh/sshd_config文件,将 ClientAliveInterval 0和ClientAliveCountMax 3的注释符号去掉,将ClientAliveInterval对应的0改成60

ClientAliveInterval指定了服务器端向客户端请求消息 的时间间隔, 默认是0, 不发送.而ClientAliveInterval 60表示每分钟发送一次, 然后客户端响应, 这样就保持长连接了.ClientAliveCountMax, 使用默认值3即可.ClientAliveCountMax表示服务器发出请求后客户端没有响应的次数达到一定值, 就自动断开. 正常情况下, 客户端不会不响应.

3、重启sudo ervice sshd restart

最后记得执行/etc/init.d/sshd restart   service sshd restart哦,否则刚才的修改是不会生效的。

二、树莓派安装串口模块,可不能直接pip install

在linux系统上安装pyserial
1,首先先下载软件包
https://pypi.org/project/pyserial/ —下载的地址


2,在linux系统上安装pyserial
在linux系统上安装又两种方式,一中和windows上一样,另一种就是通过软件包去安装。
如果linux上没有安装pip,需要通过sudo apt install python-pip去安装pip工具。

(1), tar xvf pyserial-3.4.tar.gz
(2), cd pyserial-3.4
(3), sudo python setup.py install
原文链接:https://blog.csdn.net/u014100559/article/details/105700875

三、树莓派与Arduino通过USB进行通信

在树莓派中利用Python3与Arduino实现串口通信 - 知乎

1 将树莓派与arduino通过usb线进行连接

2 在树莓派终端输入 ls /dev/tty*查看两者连接端口的名字。查看有没有ttyACM0 这个文件(注只有在两个硬件USB互连的情况下才会有这个。如果两者没有连接是不会有的)最新的系统一般都会自动生成。看到ttyACM0就说明二者可以通讯了 接下来上测试代码

3 编写树莓派与Arduino通信代码

void setup() 
{
  Serial.begin(9600); // 9600 bps
}
void loop()
{
  if ( Serial.available())
    {
      if('s' == Serial.read())
        Serial.println("Hello Raspberry,I am Arduino.");
     }
}

把上面代码通过python IDE下载到arduino中,然后再与树莓派usb连接。

树莓派与Arduino通信 - 知乎 (zhihu.com)

四、写一段树莓派端的代码去喊话arduino

import serial

Port = "/dev/ttyUSB0"  # serial port
baudRate = 9600  #
ser = serial.Serial(Port, baudRate, timeout=1)

while True:
    send = 's'  # the data send to arduio
    ser.write(send.encode())
    str = ser.readline().decode()  # receive data from arduino
    if(str != ""):
        print(str)
        if(str == 'ok'): # quit after send
            print('Got it!')
            break

ser.close()

arduino呢?

int incomedate = 0;
void setup() {
  Serial.begin(9600); //设置串口波特率9600
}

void loop() {
  while (Serial.available() > 0)//串口接收到数据
  {
    incomedate = Serial.read();//获取串口接收到的数据
    if (incomedate == '1')
    {
      //若接收到1则执行
      pinMode(13, OUTPUT);
      digitalWrite(13, HIGH); //亮灯    
      Serial.println("ok"); //向树莓派发送信息
    }
    delay(5);
  }
}

还要回到arduino IDE?不,就在树莓派上操作它:(将Arduino连接到树莓派上)

首先在树莓派上下载Arduino

sudo apt-get install arduino -y

后面的看:树莓派连接Arduino_树莓派和arduino结合-CSDN博客

五、见神杀神,putty为何每次输密码?!!

https://blog.csdn.net/weixin_36274355/article/details/116612225y

又看上了服务器的桌面背景,考下来:C:\Program Files\PuTTY>pscp [email protected]:/usr/share/rpd-wallpaper/*.jpg I:/wallpaper

待续……

评论区域大家也可以发电踩坑记录哈

猜你喜欢

转载自blog.csdn.net/xgocn/article/details/133856263