Skip to content

Instantly share code, notes, and snippets.

@geoom
Last active October 31, 2022 15:38
Show Gist options
  • Save geoom/99d1407992364c3f9553 to your computer and use it in GitHub Desktop.
Save geoom/99d1407992364c3f9553 to your computer and use it in GitHub Desktop.
Arduino bluetooth connection by python script on Mac OS X (yosemite)
// first make "pairing" with HC-06 devise
// view devises list by running 'ls /dev/tty.* in terminal, you can see /dev/tty.HC-06-DevB on the list
// then go to arduino editor, and choose your devise bluetooth under the 'Tools > Serial port' menu
// Now open the serial monitor (Tools > Serial monitor).
// You should notice that the red led of the bluetooth module has stopped blinking. That means we are connected!
// Now when you send a “1” the led on the pin 13 should turn ON, and if you send a “0” it should turn off.
void setup() {
// initialize serial:
Serial.begin(9600);
// initialize the led pin
pinMode(13, OUTPUT);
}
void loop() {
while (Serial.available()) {
char inChar = (char)Serial.read();
switch(inChar) {
case '1':
digitalWrite(13, HIGH);
Serial.print("pin 13 was turn on");
break;
case '0':
digitalWrite(13, LOW);
Serial.print("pin 13 was turn off");
break;
}
}
}
import serial
import time
# # Serial port parameters
serial_speed = 9600
serial_port = '/dev/tty.HC-06-DevB' # bluetooth shield hc-06
if __name__ == '__main__':
print "conecting to serial port ..."
ser = serial.Serial(serial_port, serial_speed, timeout=1)
print "sending message to turn on PIN 13 ..."
ser.write('1')
print "recieving message from arduino ..."
data = ser.readline()
if (data != ""):
print "arduino says: %s" % data
else:
print "arduino doesnt respond"
time.sleep(4)
print "finish program and close connection!"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment