En este laboratorio hemos utilizado dos Xbee en modo API, para poder enviar datos desde un ordenador con un programa python al Arduino mediante las Xbee.
En nuestro proyecto tenemos conectado al Arduino:
- Tres LEDs: Rojo, Amarillo y Azul
- Un buzzer
El programa en python lo que hace es enviar al Arduino qué LED tiene que encender y cuantas veces tiene que parpadear. En nuestro caso tenemos tres LEDs y tres números que corresponden a cuantas veces parpadeará el LED.
- LED: Red, Blue, Yellow
- Times: 1, 2, 3
Por ejemplo si escribimos LED: 'Red' y Times: 2 -> el LED rojo parpadeará dos veces.
Cualquier otra combinación que no sea una de las posibles combinaciones mencionadas anteriormente, será una combinación errónea y el programa python té avisará y el Arduino hará sonar el buzzer indicando que te has equivocado.
El material utilizado para este laboratorio:
El esquema del circuito:
El código Arduino:
| #include <XBee.h> int ledBlue = 10; int ledRed = 11; int ledYellow = 12; int bell = 5; XBee xbee = XBee(); XBeeResponse response = XBeeResponse(); // create reusable response objects for responses we expect to handle ZBRxResponse rx = ZBRxResponse(); void flashLed(int pin, int times, int wait) { for (int i = 0; i < times; i++) { digitalWrite(pin, HIGH); delay(wait); digitalWrite(pin, LOW); if (i + 1 < times) { delay(wait); } } } void setup() { Serial.begin(9600); xbee.begin(Serial); pinMode(ledBlue, OUTPUT); pinMode(ledRed, OUTPUT); pinMode(ledYellow, OUTPUT); pinMode(bell, OUTPUT); } void loop() { delay(1000); // 1. This will read any data that is available: xbee.readPacket(); if (xbee.getResponse().isAvailable()) { if (xbee.getResponse().getApiId() == ZB_RX_RESPONSE) { xbee.getResponse().getZBRxResponse(rx); int data = rx.getData(0); if ( data == 'B'){ int times = rx.getData(1); flashLed(ledBlue, times, 200); } else if (data == 'R'){ int times = rx.getData(1); flashLed(ledRed, times, 200); } else if( data == 'Y'){ int times = rx.getData(1); flashLed(ledYellow, times, 200); } else{ tone(bell, 1000, 250); } } } } |
El código Python:
| #! /usr/bin/python from xbee import ZigBee import serial import time from datetime import datetime ser = serial.Serial('/dev/ttyUSB0', 9600) xbee = ZigBee(ser, escaped=True) while True: try: print('----------------------') print('LED: Red, Blue, Yellow') print('Times: 1, 2, 3') led = str(input('LED: ')) times = str(input('Times: ')) if led+times == 'Red1': value = '\x52\x01' print('Correct combination') elif led+times == 'Red2': value = '\x52\x02' print('Correct combination') elif led+times == 'Red3': value = '\x52\x03' print('Correct combination') elif led+times == 'Blue1': value = '\x42\x01' print('Correct combination') elif led+times == 'Blue2': value = '\x42\x02' print('Correct combination') elif led+times == 'Blue3': value = '\x42\x03' print('Correct combination') elif led+times == 'Yellow1': value = '\x59\x01' print('Correct combination') elif led+times == 'Yellow2': value = '\x59\x02' print('Correct combination') elif led+times == 'Yellow3': value = '\x59\x03' print('Correct combination') else: value = '\x66\x05' print('Wrong combination. Try again') tstart = datetime.now() xbee.send('tx', frame_id='A', dest_addr_long='\x00\x13\xA2\x00\x40\x8B\x3B\xEB', dest_addr='\xFF\xFE', data= value) response = xbee.wait_read_frame(); tend = datetime.now() print tend-tstart time.sleep(3); except KeyboardInterrupt: break ser.close() |
Vídeo con la demostración del resultado al ejecutar el código:


No hay comentarios:
Publicar un comentario