Montana Mendy Montana Mendy - 10 months ago 73
Python Question

Python (Arduino motion sensor, with SMS notification)

I'm doing a small project I found on GitHub, I have an Arduino Mega 2560, and I'm trying to get an SMS when it detects motion, but I keep getting an error in Python, when I try running the server.

Here's the code:

#!/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 or you'll get charged extra for spoofing
SMS_TO = ""
SMS_BODY = "ALERT! Your Arduino just detected motion!"

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


# Start the server
if __name__ == "__main__":
print "Starting SMS motion detector server at",
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 =

print "Received byte:", byte_received

# Motion was detected
if byte_received == "1":
print "Motion detected at",

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

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

print "** SMS Sent! **"

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

So when I run "python" it gives me this error

Traceback (most recent call last):
File "", line 28, in <module>
TypeError: not all arguments converted during string formatting

Now it looks like TELAPI is no more, but Zang has taken it's place, any help would be appreciated, thank you so much!

Answer Source

I fixed this by replacing the Zang API, with the following


Then of course running, Arduino was able to detect motion and send SMS.