2016-11-18 45 views
0

我在GitHub上找到一个小项目,我有一个Arduino Mega 2560,我试图在检测到运动时收到短信,但我不断收到当我尝试运行服务器时,Python中出现错误。Python(Arduino运动传感器,带短信通知)

下面的代码:

#!/usr/bin/env python 

import serial 
import requests 
from datetime import datetime, timedelta 

# Set the serial port to the same serial port you uploaded the arduino sketch to 
# In the Arduino IDE, click "Tools > Serial Port" 
# SERIAL_PORT = "/dev/tty.usbserial-A70064Mh" 
SERIAL_PORT = "/dev/cu.usbmodem1421" 
SERIAL_BAUD = 115200 

# Don"t send more than one message every 30 minutes 
SENSOR_INTERVAL = timedelta(minutes=30) 

SMS_FROM = "" # Make sure this is a number on telapi.com or you'll get charged extra for spoofing 
SMS_TO = "" 
SMS_BODY = "ALERT! Your Arduino just detected motion!" 
TELAPI_ACCOUNT_SID = "" 
TELAPI_TOKEN = "" 

# Try to import TELAPI_ACCOUNT_SID and such from settings_local.py, if it exists 
try: 
    from settings_local import * 
except ImportError: 
    pass 

TELAPI_SMS_URL = "https://api.zang.io/v2/Accounts/{AccountSid}/SMS/Messages" % TELAPI_ACCOUNT_SID 

# Start the server 
if __name__ == "__main__": 
    print "Starting SMS motion detector server at", datetime.now() 
    last_sent_time = None 

    # Open a serial connection to the Arduino 
    with serial.Serial(SERIAL_PORT, SERIAL_BAUD) as arduino: 
     while True: 
      print "Polling Arduino..." 

      # Listen for the Arduino to send a byte 
      byte_received = arduino.read() 

      print "Received byte:", byte_received 

      # Motion was detected 
      if byte_received == "1": 
       print "Motion detected at", datetime.now() 

       # If we haven"t sent an SMS in the last 30 minutes, send one now 
       if not last_sent_time or (datetime.now() - last_sent_time) > SENSOR_INTERVAL: 
        last_sent_time = datetime.now() 
        print "Sending SMS..." 

        # Send request to TelAPI to send SMS 
        try: 
         data = { 
          "From": SMS_FROM, 
          "To": SMS_TO, 
          "Body": SMS_BODY, 
         } 
         requests.post(TELAPI_SMS_URL, data=data, auth=(TELAPI_ACCOUNT_SID, TELAPI_TOKEN)) 

         print "** SMS Sent! **" 

        except Exception as e: 
         print "Some error occurred while sending SMS:", e 

所以当我运行“蟒蛇server.py”它给了我这个错误

Traceback (most recent call last): 
    File "server.py", line 28, in <module> 
    TELAPI_SMS_URL = "https://api.zang.io/v2/Accounts/{AccountSid}/SMS/Messages" % TELAPI_ACCOUNT_SID 
TypeError: not all arguments converted during string formatting 

现在看起来TELAPI是没有了,但臧采取这是地方,任何帮助将不胜感激,非常感谢你!

回答

0

我通过更换藏API,具有以下

TELAPI_SMS_URL = "https://api.telapi.com/2011-07-01/Accounts/%s/SMS/Messages" % TELAPI_ACCOUNT_SID 

那么当然运行server.py固定此,Arduino的能够检测运动并发送SMS。