Hello everyone,
I’m trying to write a program in Python, that takes the data from the sensors (temperature and luminosity) and write in a .txt file.
But I’m having a huge problem with Python (I’m using PySerial) and a MacBook Pro running OS X 10.11. because Python says that there is an error and as I’m not a pro in Python, and don’t know what I am doing wrong, because I think that the problem is the serial port
Can someone help me?
The Arduino Code is this one:
const int LDR = 0;
const int LM35 = 1;
int ValorTemp = 0;
float temperatura = 0;
int luminosidade = 0;
void setup() {
Serial.begin(9600);
analogReference(INTERNAL);
}
void loop() {
luminosidade = analogRead(LDR);
ValorTemp = analogRead(LM35);
temperatura = ValorTemp*0.1075268817204301;
Serial.print(temperatura);
Serial.print(" , ");
Serial.print(luminosidade);
Serial.println();
delay(1000);
}
And the Python code is this one (not completed yet):
#arduino project for texting data
import serial
serial_speed = 9600
serial_port = '/dev/cu.usbmodem1421'
ser = serial.Serial(serial_port, serial_speed, timeout = 1)
while True:
data = ser.readline()
if data:
print data
When I try to run the program in the Python, it gives me the following error:
Traceback (most recent call last):
File “/Users/***/Desktop/Arduino/Project Sensor/ProgramPython.py”, line 7, in
ser = serial.Serial(serial_port, serial_speed, timeout = 1)
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialutil.py”, line 180, in init
self.open()
File “/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/serial/serialposix.py”, line 294, in open
raise SerialException(msg.errno, “could not open port %s: %s” % (self._port, msg))
SerialException: [Errno 16] could not open port /dev/cu.usbmodem1421: [Errno 16] Resource busy: ‘/dev/cu.usbmodem1421’