ループで実行されている Python スクリプトがあります。(.ino スクリプトからリレーをトリガーするために) ブリッジを介して値 2、3、または 4 を渡すたびに、python スクリプトがクラッシュします。
基本的に、リレー値がシリアル モニターに出力されているのを確認した後、「Starting .py script again」が何度も表示されますが、リレーがトリガーされることはありません。
奇妙なのは、PuTTY を使用して Yun に SSH 接続し、Python スクリプトを手動で呼び出すことができ、それが機能することです。arduino から python スクリプトを自動的に起動しようとすると、python スクリプトが機能しません。
Python スクリプト:
from ftplib import FTP
import datetime
import sys
sys.path.insert(0, '/usr/lib/python2.7/bridge')
from bridgeclient import BridgeClient as bridgeclient
from time import sleep
# Initialize vars
ftpIP = 'XXXXXXXXXXXXXX' # Internal IP
# ftpIP = 'XXXXXXXXXXXXXXXXX' #External IP
ftpDirectory = '3990548'
value = bridgeclient()
value.put('relay', '0')
status = 'Disarmed'
# Initialize FTP
ftp = FTP(ftpIP)
ftp.login(XXXXXXXXXXXXXXXXXXX)
ftp.cwd(ftpDirectory)
# Loop
while (1):
#Don't eat all CPU cycles
sleep(0.01)
# Define minute and second, each loop iteration
rawTime = datetime.datetime.now()
fmtTime = rawTime.strftime("%m/%d/%Y %H:%M:%S")
date = datetime.date.today()
hour = rawTime.hour
minute = rawTime.minute
second = rawTime.second
month = date.month
day = date.day
year = date.year
fileDate = rawTime.strftime("%m%d%y")
fileName = '{}.txt'.format(fileDate)
filePath = 'tadtest/{}'.format(fileName)
# Keep FTP alive
if (second == 30):
ftp.voidcmd('NOOP')
value.put('relay', '9')
print value.get('relay')
sleep(1)
value.put('relay', '0')
### Alarm System ###
if (second <= 5 and (minute == 0 or minute == 20 or minute == 40)):
# Write to file
with open(filePath, "a+") as myFile:
myFile.write('SensorAlarm, {}\n'.format(fmtTime))
if (status == 'Disarmed'):
# Trigger Keyfob Relay Arm (3)
value.put('relay', '3')
print value.get('relay')
sleep(5)
value.put('relay', '0')
print "Arm"
status = 'Armed'
elif (status == 'Armed'):
# Trigger Contact Sensor Relay
value.put('relay', '2')
print value.get('relay')
sleep(5)
value.put('relay', '0')
print "Alarm"
status = 'Armed'
else:
print "Error in Alarm (Arm) 'if' statement"
# Write to FTP
ftp.storbinary('STOR {}'.format(fileName), open('/mnt/sda1/{}'.format(filePath), 'rb'))
### Disarm System ###
if (second <= 5 and (minute == 5 or minute == 25 or minute == 45)):
# Write to file
with open(filePath, "a+") as myFile:
myFile.write('OpeningReport, {}\n'.format(fmtTime))
# Trigger Keyfob Relay Disarm (4)
value.put('relay', '4')
print value.get('relay')
sleep(5)
value.put('relay', '0')
print "Disarm"
status = 'Disarmed'
# Write to FTP
ftp.storbinary('STOR {}'.format(fileName), open('/mnt/sda1/{}'.format(filePath), 'rb'))
### Arm System ###
if (second <= 5 and (minute == 8 or minute == 30 or minute == 50)):
# Write to file
with open(filePath, "a+") as myFile:
myFile.write('ClosingReport, {}\n'.format(fmtTime))
# Trigger Keyfob Relay Arm (3)
value.put('relay', '3')
print value.get('relay')
sleep(5)
value.put('relay', '0')
print "Arm"
status = 'Armed'
# Write to FTP
ftp.storbinary('STOR {}'.format(fileName), open('/mnt/sda1/{}'.format(filePath), 'rb'))
Arduino スクリプト: (media() 関数は LED 画面に書き込むだけです)
#include <Bridge.h>
#include <Adafruit_GFX.h>
#include <Adafruit_ST7735.h>
//Define LCD variables
#define TFT_CS 7
#define TFT_RST 8
#define TFT_DC 9
#define TFT_SCLK 10
#define TFT_MOSI 11
Adafruit_ST7735 tft = Adafruit_ST7735(TFT_CS, TFT_DC, TFT_MOSI, TFT_SCLK, TFT_RST);
int crsr=0;
uint16_t mcolor=ST7735_RED;
Process p;
//Here we hold values coming from the bridge
char relayValue[2] = {0};
int previousRelayInt = 100;
void setup() {
Bridge.begin();
Serial.begin(9600);
//Relay pins
pinMode(2, OUTPUT);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(13, OUTPUT);
//Zero out memory used for relay value
memset(relayValue, 0, 2);
//Initialize pins
digitalWrite(2, HIGH);
digitalWrite(3, LOW);
digitalWrite(4, LOW);
digitalWrite(13, LOW);
//Initialize screen to be black
tft.initR(INITR_BLACKTAB);
tft.fillScreen(ST7735_BLACK);
}
void loop() {
if (p.running()==0)
{
p.runShellCommandAsynchronously("python /mnt/sda1/ftp_test.py");
Serial.println("Started .py script again");
}
Bridge.get("relay", relayValue, 2);
int relayInt = atoi(relayValue);
if (relayInt==previousRelayInt)
{
}
else if (relayInt!=previousRelayInt)
{
printTFT(relayValue);
Serial.println(relayValue);
previousRelayInt = relayInt;
}
if (relayInt == 0)
// Do nothing
{
}
else if (relayInt == 2)
//Trigger Contact Sensor - Alarm
{
digitalWrite(2, LOW);
printTFT(relayValue);
delay(4000);
digitalWrite(2, HIGH);
delay(4000);
mediaAlarm();
}
else if (relayInt == 3)
//Trigger Keyfob - Arm
{
digitalWrite(3, HIGH);
printTFT(relayValue);
delay(4000);
digitalWrite(3, LOW);
delay(4000);
mediaArm();
}
else if (relayInt == 4)
//Trigger Keyfob - Disarm
{
digitalWrite(4, HIGH);
printTFT(relayValue);
delay(4000);
digitalWrite(4, LOW);
delay(4000);
mediaDisarm;
}
else if (relayInt == 9)
//Blink LED
{
digitalWrite(13, HIGH);
printTFT(relayValue);
delay(4000);
digitalWrite(13, LOW);
delay(4000);
}
}