Monday, April 20, 2015

Pre-Summer III. First 433Mhz Implementation

Ha pasado ya un ratito desde el verano pasado y ya va siendo hora de que vaya preparando el proximo verano.

Para empezar ya tengo nuevo material para la estructura que soportara la electronica. Uno de los objetivos es poder hacer una mini placa con un micro, un modulo de recepcion un led y un servo.
Es decir, sacar esto de aqui ...


Para ello, vamos a tener que pasar por KiCAD y para ello, tenemos una magnfica charla de diseño de PCBs con KiCAD, a ver si hay suerte y podemos ir.

Mientras tanto he ido prototipando en Arduino el módulo de recepción y de emisión a 433Mhz, nada del otro mundo, especialmente después de haber asistido al curso de comunicaciones inalámbricas que nuestro gurú Jorge de SinDormir.net  nos iluminara con sus conocimientos.

Así que estos son mis prototipos:

Modulo de emisión, tambien llamado TX o modulo de Tierra ya que será el encargado de abrír desde Tierra la compuerta del paracaidas:


Poco podeis apreciar aqui, a parte del hecho de que el boton ROJO de arriba, envia una señal al módulo de recepción. Codigo aqui: 


// RF
#include <SPI.h>
#include <RH_ASK.h>
#define MAX_MSG_LEN 59
// LCD
#include <LCD5110_Graph.h>
#include <AVR/pgmspace.h>
LCD5110 myGLCD (9,10, 11,12, 13);
extern uint8_t SmallFont[];
/// \param[in] speed The desired bit rate in bits per second
/// \param[in] rxPin The pin that is used to get data from the receiver
/// \param[in] txPin The pin that is used to send data to the transmitter
/// \param[in] pttPin The pin that is connected to the transmitter controller. It will be set HIGH to enable the transmitter (unless pttInverted is true).
/// \param[in] pttInverted true if you desire the pttin to be inverted so that LOW wil enable the transmitter.
RH_ASK fs1000a = RH_ASK(2000, 11, 3, 10, false);
const byte PIN_BUTTON_UP = 2; // Up botton
const byte PIN_BUTTON_DOWN = 4; // Down botton
void setup() {
Serial.begin(9600);
// LCD
/* Init LCD5110_Graph library */
myGLCD.InitLCD();
myGLCD.setFont(SmallFont);
/* Start display */
myGLCD.print("Jagudo", CENTER, 0);
myGLCD.print("Matteo 6.0", CENTER, 20);
myGLCD.print("Earth module", CENTER, 40);
myGLCD.update();
delay(3000);
// Set up Read botom up in joystick
pinMode(PIN_BUTTON_UP, INPUT); //considera o botão como uma entrada
digitalWrite(PIN_BUTTON_UP, HIGH); // atribui nível alto ao botão
// Init RF Module
if (fs1000a.init()) Serial.println("Extremo TX. Init OK");
}
void loop() {
static int i=0;
static char msg[MAX_MSG_LEN+1];
myGLCD.clrScr();
/* Start display */
myGLCD.print("Earth Module", CENTER, 0);
myGLCD.print("Matteo 6.0", CENTER, 20);
if (!digitalRead(PIN_BUTTON_UP))
{
Serial.println("Open parachute!");
msg [0] = 'O';
msg [1] = '\0';
fs1000a.send((uint8_t *)msg, strlen(msg));
fs1000a.waitPacketSent(); // Blocking until all packet has been sent.
myGLCD.print("OPEN", CENTER, 30);
myGLCD.print("parachute", CENTER, 40);
myGLCD.update();
Serial.println("Enviado!");
delay (500);
}
if (!digitalRead(PIN_BUTTON_DOWN))
{
Serial.println("Close parachute!");
msg [0] = 'o';
msg [1] = '\0';
fs1000a.send((uint8_t *)msg, strlen(msg));
fs1000a.waitPacketSent(); // Blocking until all packet has been sent.
Serial.println("Enviado!");
myGLCD.print("CLOSE", CENTER, 30);
myGLCD.print("parachute", CENTER, 40);
myGLCD.update();
delay (500);
}
// if(Serial.available()) {
// if (i > MAX_MSG_LEN){
// Serial.println("Mensaje demasiado largo. Descartando...");
// i=0;
// }
// else{
// msg[i++]=Serial.read();
// }
// }
//
// if (msg[i-1] == '\n'){
// msg[i]='\0';
// i=0;
//
// fs1000a.send((uint8_t *)msg, strlen(msg));
// fs1000a.waitPacketSent(); // Blocking until all packet has been sent.
// Serial.println("Enviado!");
// }
}
view raw gistfile1.cpp hosted with ❤ by GitHub

Si le habeis echado un vistazo al código habreis visto que es más complejo de lo que aparentemente hace, eso es porque he estado friqueando poniendole una pantallita LCD y viendo que efectivamente mandaba la señal, otra vez, nada del otro mundo, esto es:


El módulo de recepción por su parte, recibe la señal emitida por el modulo de tierra y enciende un LED. No he ido mucho más alla porque el objetivo inmediato es portar el codigo Arduino a un make (del cual ya hablaré en otras entradas del blog), así que esta vez el modulo receptor


queda bastante más sencillo y su codigo ya ni digamos:


#include <RH_ASK.h>
#include <SPI.h>
#define MAX_MSG_LEN 59
RH_ASK receptor = RH_ASK(2000, 11, 12, 10, false);
void setup() {
Serial.begin(9600);
pinMode(13, OUTPUT);
if (receptor.init()) Serial.println("Extremo RX. Init OK");
}
void loop() {
char msg[MAX_MSG_LEN];
byte len = sizeof(msg);
if (receptor.recv( (uint8_t*) msg, (uint8_t*) &len)) {
for (byte i = 0; i < len; i++)
{
Serial.print(msg[i]);
if (msg[i] == 'O') digitalWrite(13, HIGH);
if (msg[i] == 'o') digitalWrite(13, LOW);
}
Serial.println();
}
}
view raw gistfile1.cpp hosted with ❤ by GitHub
Total, que el conjunto queda algo así:


Corriendo ahora si, cada uno con baterias independientes y simulando una primera comunicación modulo Tierra, cohete.

El video ...