Ahora toca trabajar con imágenes, y lo vamos a aplicar a la barra de notificaciones de nuestro proyecto.
Ya hemos visto lo básico y como conectar y queda por cerrar este tema y lo comparto con ustedes en el siguiente video:
Veamos el código:
#include <SPI.h>
#include <LoRa.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
String inString = ""; // string para guardar los caracteres entrantes
String MiMensaje = ""; // String para guardar el mensaje completo MiMensaje
String inRssi = "";
String inSnr = "";
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
#define OLED_RESET -1 // Reset pin # (or -1 if sharing Arduino reset pin)
#define SCREEN_ADDRESS 0x3C ///< See datasheet for Address; 0x3D for 128x64, 0x3C for 128x32
#define FACTOR_ATENUACION 2 // Valores entre 2 y 4
#define MEDIDARSSI -60 //
static const unsigned char PROGMEM antena[] =
{0x48, 0xb4, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x30, 0x78};
static const unsigned char PROGMEM linea1[] =
{0xc0, 0x80};
static const unsigned char PROGMEM linea2[] =
{0xc0, 0x80, 0x80};
static const unsigned char PROGMEM linea3[] =
{0xc0, 0x80, 0x80, 0x80, 0x80};
static const unsigned char PROGMEM linea4[] =
{0xc0, 0x80, 0x80, 0x80, 0x80, 0x80, 0x80};
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
void setup() {
Serial.begin(9600);
while (!Serial);
Serial.println("Receptor LoRa");
if (!LoRa.begin(433E6)) {
Serial.println("Inicializacion LoRa fallida!");
while (1);
}
if(!display.begin(SSD1306_SWITCHCAPVCC, SCREEN_ADDRESS)) {
Serial.println(F("SSD1306 allocation failed"));
for(;;); // Don't proceed, loop forever
}
}
void loop() {
int packetSize = LoRa.parsePacket();
if (packetSize) {
// lectura del paquete
while (LoRa.available())
{
int inChar = LoRa.read();
inString += (char)inChar;
MiMensaje = inString;
}
inString = "";
}
int rssi = LoRa.packetRssi();//Devuelve el RSSI promedio del último paquete recibido (dBm).
float snr = LoRa.packetSnr();//Devuelve la relación señal/ruido (SNR) estimada del paquete recibido en dB.
inRssi = (String)rssi;
inSnr = (String)snr;
int distancia = pow(10.0 , (MEDIDARSSI - rssi) / (10.0 * FACTOR_ATENUACION));
// return Math.pow(10.0, ((Math.abs(this) - Math.abs(txPower)) / (10 * 2)).toDouble())
display.clearDisplay();
display.setTextSize(1);
display.setTextColor(WHITE);
display.setCursor(0,0);
display.println(distancia);
display.setTextSize(2);
display.setCursor(0,16);
display.println(inRssi + "(dBm)");
display.setTextSize(2);
display.setCursor(0,35);
display.println(inSnr + "(dB)");
display.drawBitmap( 45 , 0 , antena , 6 , 10 , 1);
display.drawBitmap( 52 , 8 , linea1 , 1 , 2 , 1);
display.drawBitmap( 54 , 6 , linea2 , 1 , 4 , 1);
display.drawBitmap( 56 , 4 , linea3 , 1 , 6 , 1);
display.drawBitmap( 58 , 2 , linea4 , 1 , 8 , 1);
display.display();;
Serial.println(MiMensaje);
}