Arduino (IoT) Video Serie: Kit de Carro Parte 3

4WD Car Kit Arduino Santiapps Series

Arduino Honduras Santiapps Marcio Valenzuela

Arduino IoT: Video tutorial Kit de Carro Parte 3

Este es el codigo final:

#include  <AFMotor.h>

AF_DCMotor motor1(1, MOTOR12_64KHZ); // create motor #1, 64KHz pwm
AF_DCMotor motor2(2, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor3(3, MOTOR12_64KHZ); // create motor #2, 64KHz pwm
AF_DCMotor motor4(4, MOTOR12_64KHZ); // create motor #2, 64KHz pwm

void setup() {
  Serial.begin(9600);           // set up Serial library at 9600 bps
  Serial.println("Motor test!");
  
  motor1.setSpeed(200);     // set the speed to 200/255
  motor2.setSpeed(200);     // set the speed to 200/255
  motor3.setSpeed(200);     // set the speed to 200/255
  motor4.setSpeed(200);     // set the speed to 200/255
}

void loop() {
  
  motor1.run(FORWARD);      // turn it on going forward
  delay(1000);
  motor1.run(BACKWARD);     // the other way
  delay(1000);
  motor1.run(RELEASE);      // stopped
  delay(5000);

  motor2.run(FORWARD);      // turn it on going forward
  delay(1000);
  motor2.run(BACKWARD);     // the other way
  delay(1000);
  motor2.run(RELEASE);      // stopped
  delay(1000);

  motor3.run(FORWARD);      // turn it on going forward
  delay(1000);
  motor3.run(BACKWARD);     // the other way
  delay(1000);
  motor3.run(RELEASE);      // stopped
  delay(5000);

  motor4.run(FORWARD);      // turn it on going forward
  delay(1000);
  motor4.run(BACKWARD);     // the other way
  delay(1000);
  motor4.run(RELEASE);      // stopped
  delay(1000);
}

void forward(){}
void backward(){}
void brakes(){}

IoT Arduino-Raspberry Pi Hackeando una TSRB430 BLE Relay Board P3

Hacking Electronics Arduino Santiapps
Raspberry Pi 2 Bluetooth Serial Uart HM10 HM12 Relay Board AI by Santiapps Marcio Valenzuela
Raspberry Pi 2 Bluetooth Serial Uart HM10 HM12 Relay Board AI

 

Continuamos haciendo la TSRB430 BLE pero ahora desde una Raspberry Pi 🙂

 

Requisitos

  1. RPi2
  2. Modulo Serial HM10 BLE
  3. TSRB430 Relay Board

Raspberry Pi2 Code

[code]</pre>
<pre>#!/usr/bin/env python

import serial

def convert_hex_to_int(hexChars):
#convert string of hex chars to a list of ints
try:
ints = [ord(char) for char in hexChars]
return ints
except TypeError:
pass
return []
def convert_hex_to_bin_str(hexChars):
#convert hex char into byte string
response = convert_hex_to_int(hexChars)[0]
# convert int to binary string
responseBinary = bin(response)
# first 2 chars of binary string are ‘0b’ so ignore these
return responseBinary[2:]

ser = serial.Serial(
port=’/dev/serial0′,
baudrate=9600,
parity=serial.PARITY_NONE,
stopbits=serial.STOPBITS_ONE,
bytesize=serial.EIGHTBITS,
timeout=1
)

print “Serial esta abierto: ” + str(ser.isOpen())

print “Escribiendo…”
ser.write(“[“)
#ser.write(“AT+CONNL”)
print “Escrito! Ahora leamos”
x = ser.readline()
print “got ‘” + x + “‘”

responseBits = convert_hex_to_bin_str (x)
# binary conversion drops values until a 1 is encountered
# assume missing values are 0 and pad to give a value for all relays
responseBits = responseBits.zfill(4)
# reverse chars so that relay 1 is first
responseBits = list(responseBits)
responseBits.reverse()
# create dictionary of relay states
relayStates = {}
relay = 1
for bit in responseBits:
relayStates[relay] = int(bit)
relay += 1
print relayStates

ser.close()
</pre>
<pre>[/code]

Con esto ocupamos un poco mas de codigo para interpretar la respuesta del TSRB430.

Ahora podemos escribir y leer a los relays remotamente.

pi@raspberrypi:~/Documents/python $ python sendat5.py

Serial is open: True

Now Writing

Did write, now read

got ”

{1: 0, 2: 0, 3: 0, 4: 0}

pi@raspberrypi:~/Documents/python $

En el proximo tutorial veremos como usar estos códigos para crear un asistente personal que controla nuestra casa.