OLED Display Problem (dots) for lora

Hi. I have a problem with my OLED display for my Arduino code. It shows dots only after I upload the code. I have used lora to transmit the gps data from one another.
Screenshot 2023-01-24 103958

Here is the code:
Trasnmitter:

// Transmitter

#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
#include <TinyGPS++.h>
#include <SPI.h>              // include libraries
#include <LoRa.h>
//--------------------------------------------------------------
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
//-------------------------------
static const int RXPin = 8, TXPin = 9;
static const uint32_t GPSBaud = 9600;
 
// The TinyGPS++ object
TinyGPSPlus gps;
 
// The serial connection to the GPS device
SoftwareSerial ss(RXPin, TXPin); // for gps
/////////////////////////////////////////////////////
String outgoing;              // outgoing message
 
byte msgCount = 0;            // count of outgoing messages
byte destination = 0xFF;     
byte localAddress = 0xBB;
long lastSendTime = 0;        // last send time
int interval = 2000;          // interval between sends
///----------------------
// For Oled display
#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
///-------------------------------
String Mymessage = "";
//--------------------------------------------------------------
// Size of the geo fence (in meters)
const float maxDistance = 30;

//--------------------------------------------------------------
float initialLatitude = 34.014875;
float initialLongitude = 72.163585;

float latitude, longitude;

char buff[10];
String mylong = ""; // for storing the longittude value
String mylati = ""; // for storing the latitude value

//--------------------------------------------------------------

int msgstatus; 

int Sensor1;
int relay = 3;
float distance;

/*****************************************************************************************
 * setup() function
 *****************************************************************************************/
void setup()
{
  //--------------------------------------------------------------
  //Serial.println("Arduino serial initialize");
  Serial.begin(9600);
   display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  display.clearDisplay();
  display.setTextColor(WHITE);
  pinMode(relay, OUTPUT);
  //--------------------------------------------------------------
  //Serial.println("NEO6M serial initialize");
    ss.begin(GPSBaud);
  //--------------------------------------------------------------
  if (!LoRa.begin(433E6)) {       
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                       // if failed, do nothing
  }
 
 Serial.println("LoRa init succeeded.");
}

/*****************************************************************************************
 * loop() function
 *****************************************************************************************/
void loop()
{

  while (ss.available() > 0)
    if  ( gps.encode(ss.read() ) )
    {
     displayInfo();    
  latitude = gps.location.lat(), 6 ;
  longitude = gps.location.lng(), 6 ;
  mylati = dtostrf(latitude, 3, 6, buff);
  mylong = dtostrf(longitude, 3, 6, buff);
  distance = getDistance(latitude, longitude, initialLatitude, initialLongitude);

    }
    
  //--------------------------------------------------------------
  if (millis() - lastSendTime > interval) {
  //displayInfo(); 

 // Serial.print("Latitude= "); Serial.println(latitude, 6);
  //Serial.print("Lngitude= "); Serial.println(longitude, 6);

    if(distance > maxDistance) {
      msgstatus =1;
    }
    
    if(distance < maxDistance) {
      msgstatus =0;
       
    }
      
    // Serial.print("Distance: ");
  //Serial.println(distance);
   Mymessage = Mymessage + mylati +"," + mylong+","+msgstatus + "," +distance ;  
     sendMessage(Mymessage);
     //Serial.println(Mymessage);
    delay(50);
    Mymessage = "";
  

  
      //Serial.println("Sending " + message);
    lastSendTime = millis();            // timestamp the message
   
  }
  // parse for a packet, and call onReceive with the result:
  onReceive(LoRa.parsePacket());


}





// Calculate distance between two points
float getDistance(float flat1, float flon1, float flat2, float flon2) {

  // Variables
  float dist_calc=0;
  float dist_calc2=0;
  float diflat=0;
  float diflon=0;

  // Calculations
  diflat  = radians(flat2-flat1);
  flat1 = radians(flat1);
  flat2 = radians(flat2);
  diflon = radians((flon2)-(flon1));

  dist_calc = (sin(diflat/2.0)*sin(diflat/2.0));
  dist_calc2 = cos(flat1);
  dist_calc2*=cos(flat2);
  dist_calc2*=sin(diflon/2.0);
  dist_calc2*=sin(diflon/2.0);
  dist_calc +=dist_calc2;

  dist_calc=(2*atan2(sqrt(dist_calc),sqrt(1.0-dist_calc)));
  
  dist_calc*=6371000.0; //Converting to meters

  return dist_calc;
}
void sendMessage(String outgoing) {
  LoRa.beginPacket();                   // start packet
  LoRa.write(destination);              // add destination address
  LoRa.write(localAddress);             // add sender address
  LoRa.write(msgCount);                 // add message ID
  LoRa.write(outgoing.length());        // add payload length
  LoRa.print(outgoing);                 // add payload
  LoRa.endPacket();                     // finish packet and send it
  msgCount++;                           // increment message ID
}
void onReceive(int packetSize) {
  if (packetSize == 0) return;          // if there's no packet, return

  // read packet header bytes:
  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address
  byte incomingMsgId = LoRa.read();     // incoming msg ID
  byte incomingLength = LoRa.read();    // incoming msg length

  String incoming = "";

  while (LoRa.available()) {
    incoming += (char)LoRa.read();
  }

  if (incomingLength != incoming.length()) {   // check length for error
    //Serial.println("error: message length does not match length");
    ;
    return;                             // skip rest of function
  }

  // if the recipient isn't this device or broadcast,
  if (recipient != localAddress && recipient != 0xFF) {
   // Serial.println("This message is not for me.");
    ;
    return;                             // skip rest of function
  }

  // if message is for this device, or broadcast, print details:
  //Serial.println("Received from: 0x" + String(sender, HEX));
  //Serial.println("Sent to: 0x" + String(recipient, HEX));
  //Serial.println("Message ID: " + String(incomingMsgId));
 // Serial.println("Message length: " + String(incomingLength));
 // Serial.println("Message: " + incoming);
//  Serial.println("RSSI: " + String(LoRa.packetRssi()));
//  Serial.println("Snr: " + String(LoRa.packetSnr()));
//  Serial.println();

 String q = getValue(incoming, ',', 0); // Latitude
 Serial.println(q);
 Sensor1 = q.toInt();  // latitude

 if ( Sensor1 == 1 )
 {
  digitalWrite(relay, HIGH);
 }
  if ( Sensor1 == 0 )
 {
  digitalWrite(relay, LOW);
 }
}

void displayInfo()
{
  Serial.print(F("Location: ")); 
  if (gps.location.isValid())
  {
    Serial.print(gps.location.lat(), 6);
    Serial.print(F(","));
    Serial.print(gps.location.lng(), 6);
    Serial.print(" ");
    Serial.print(F("Speed:"));
    Serial.print(gps.speed.kmph());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
 
  Serial.print(F("  Date/Time: "));
  if (gps.date.isValid())
  {
    Serial.print(gps.date.month());
    Serial.print(F("/"));
    Serial.print(gps.date.day());
    Serial.print(F("/"));
    Serial.print(gps.date.year());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
 
  Serial.print(F(" "));
  if (gps.time.isValid())
  {
    if (gps.time.hour() < 10) Serial.print(F("0"));
    Serial.print(gps.time.hour());
    Serial.print(F(":"));
    if (gps.time.minute() < 10) Serial.print(F("0"));
    Serial.print(gps.time.minute());
    Serial.print(F(":"));
    if (gps.time.second() < 10) Serial.print(F("0"));
    Serial.print(gps.time.second());
    Serial.print(F("."));
    if (gps.time.centisecond() < 10) Serial.print(F("0"));
    Serial.print(gps.time.centisecond());
  }
  else
  {
    Serial.print(F("INVALID"));
  }
 
  Serial.println();
 
}


String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;
 
    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}
Receiver:

/*
  //Receiver
  
 
*/
#include <SPI.h>              // include libraries
#include <LoRa.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

#define SCREEN_WIDTH 128 // OLED display width, in pixels
#define SCREEN_HEIGHT 64 // OLED display height, in pixels
// Declaration for an SSD1306 display connected to I2C (SDA, SCL pins)
#define OLED_RESET     -1 // Reset pin # (or -1 if sharing Arduino reset pin)
Adafruit_SSD1306 display(SCREEN_WIDTH, SCREEN_HEIGHT, &Wire, OLED_RESET);
 int Buzzer=5;
 int button=4;
 int buttonState;
 int Distance;
 boolean Bflag = false;
 
String SenderNode = "";
String outgoing;              // outgoing message

byte msgCount = 0;            // count of outgoing messages
byte localAddress = 0xFF;     // address of this device
byte destination = 0xBB;      // destination to send to
long lastSendTime = 0;        // last send time
int interval = 2000;          // interval between sends
 
String incoming = "";
 String statusmessage = "";

int Sensor1 = 0; // Latitude
int Sensor2 = 0; // Longitude
int Sensor3 = 0; // Status
 
String DayNight = "";
 
void setup() {
  Serial.begin(9600);                   // initialize serial
  pinMode(Buzzer,OUTPUT);
   pinMode(button,INPUT_PULLUP);
  display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
  delay(500);
  display.clearDisplay();
  display.setTextColor(WHITE);
 
  if (!LoRa.begin(433E6)) {             // initialize ratio at 915 MHz
    Serial.println("LoRa init failed. Check your connections.");
    while (true);                       // if failed, do nothing
  }
 
 // Serial.println("LoRa init succeeded.");
}
 
void loop() {
 
  // parse for a packet, and call onReceive with the result:
  if (millis() - lastSendTime > interval) {

    if ( (digitalRead(button) == LOW) && ( Bflag == false ) )
    {
      buttonState = 1;
      Bflag = true;
    }

       if ( (digitalRead(button) == HIGH )&& (Bflag == true))
    {
      buttonState = 0;
      Bflag = false;
    }
  
statusmessage = statusmessage + buttonState + "," ;
        sendMessage(statusmessage);
        delay(50);
        statusmessage = "";

     lastSendTime = millis();  
  }
   
  onReceive(LoRa.parsePacket());
    
  }
 
 
void onReceive(int packetSize) {
  if (packetSize == 0) return;          // if there's no packet, return
 
  // read packet header bytes:
  int recipient = LoRa.read();          // recipient address
  byte sender = LoRa.read();            // sender address
  if( sender == 0XBB )
  SenderNode = "Node1:";
  byte incomingMsgId = LoRa.read();     // incoming msg ID
  byte incomingLength = LoRa.read();    // incoming msg length
 
 
  while (LoRa.available()) {
    incoming += (char)LoRa.read();
  }
 
  if (incomingLength != incoming.length()) {   // check length for error
    //Serial.println("error: message length does not match length");
    ;
    return;                             // skip rest of function
  }
 
//  // if the recipient isn't this device or broadcast,
//  if (recipient != Node1 && recipient != MasterNode) {
//   // Serial.println("This message is not for me.");
//    ;
//    return;                             // skip rest of function
//  }
 
  // if message is for this device, or broadcast, print details:
  //Serial.println("Received from: 0x" + String(sender, HEX));
  //Serial.println("Sent to: 0x" + String(recipient, HEX));
  //Serial.println("Message ID: " + String(incomingMsgId));
 // Serial.println("Message length: " + String(incomingLength));
 // Serial.println("Message: " + incoming);
  //Serial.println("RSSI: " + String(LoRa.packetRssi()));
 // Serial.println("Snr: " + String(LoRa.packetSnr()));
 // Serial.println();
 
//Serial.println("received value");
 
String q = getValue(incoming, ',', 0); // Latitude
String r = getValue(incoming, ',', 1); // Longitude
String s = getValue(incoming, ',', 2); // Status
String t = getValue(incoming, ',', 3); // Status

Sensor1 = q.toDouble();  // latitude
Sensor2 = r.toDouble(); // longitude
Sensor3 = s.toInt(); // status
Distance = t.toInt();
//Serial.println(q);
//Serial.println(r);
//Serial.println(s);
 
if (Sensor3==1)
{
  digitalWrite(Buzzer,HIGH);
  }
  else 
{
  digitalWrite(Buzzer,LOW);
  }
incoming = "";

 
    //clear display
  display.clearDisplay();
 

 
  display.setTextSize(2);
  display.setCursor(0, 10);
  display.print(q);        // Latitude
   
    display.setTextSize(2);
  display.setCursor(0, 30);
  display.print(r);       // Longitude
 
  display.setTextSize(2);
  display.setCursor(0, 50);
  display.print("S:" + s +" D:"+Distance); // Status and distance


 
display.display(); 
}
 
String getValue(String data, char separator, int index)
{
    int found = 0;
    int strIndex[] = { 0, -1 };
    int maxIndex = data.length() - 1;
 
    for (int i = 0; i <= maxIndex && found <= index; i++) {
        if (data.charAt(i) == separator || i == maxIndex) {
            found++;
            strIndex[0] = strIndex[1] + 1;
            strIndex[1] = (i == maxIndex) ? i+1 : i;
        }
    }
    return found > index ? data.substring(strIndex[0], strIndex[1]) : "";
}

void sendMessage(String outgoing) {
  LoRa.beginPacket();                   // start packet
  LoRa.write(destination);              // add destination address
  LoRa.write(localAddress);             // add sender address
  LoRa.write(msgCount);                 // add message ID
  LoRa.write(outgoing.length());        // add payload length
  LoRa.print(outgoing);                 // add payload
  LoRa.endPacket();                     // finish packet and send it
  msgCount++;                           // increment message ID
}

Here is the code. Please help meee

Hello sattttt12
Run some tutorials for the hardware selected.
If you are happy with the results of the tutorials you can merge these to your project.
Have a nice day and enjoy coding in C++.

Start at the begining.

Write a program that just prints a test message on the display, does that work OK ?

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.