Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

P10 showing ramdom number #22

Open
frinks opened this issue Aug 26, 2023 · 0 comments
Open

P10 showing ramdom number #22

frinks opened this issue Aug 26, 2023 · 0 comments

Comments

@frinks
Copy link

frinks commented Aug 26, 2023

I am using the following code to display characters on P10 LED but what is happening is sometimes the code hangs or sometimes it shows random characters. I have with serial monitor that the socket is being received perfectly.

#include <SPI.h>
#include <DMD.h>
#include <TimerOne.h>
#include <Ethernet.h>
#include "SystemFont5x7.h"
#include "Arial_black_16.h"
#include <Arduino.h>

// Fire up the DMD library as dmd
#define BELT_ID "TLM-1"
IPAddress ip(192, 168, 69, 170); // Static IP address
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xAA}; // MAC address of your Arduino

#define DISPLAYS_ACROSS 2
#define DISPLAYS_DOWN 2
#define MAX_CHAR_ARRAY_SIZE 100

DMD dmd(DISPLAYS_ACROSS, DISPLAYS_DOWN);

String belt_ids;
String belt_id_1;
String belt_id_2;
String count;
String bag_limit;
String vehicle_desctiption;
String receivedData;
String resultant;

char delimiter = '/';
char response[5];
char resultantChar[MAX_CHAR_ARRAY_SIZE];
char vehicleDesctiptionChar[MAX_CHAR_ARRAY_SIZE];
char receivedDataChar[MAX_CHAR_ARRAY_SIZE];

bool is_screen_value_updated = false;
bool is_screen_cleared = false;

int serverPort = 1234; // Port number of the server
int index_of_delimiter;
int resultantCharSize = 0;
int vehicleDesctiptionCharSize = 0;
unsigned long previousPingTime = 0;
const unsigned long pingInterval = 5000; // 5 seconds in milliseconds
unsigned long currentMillis;

int bytesRead = 0;

IPAddress serverIP(192, 168, 69, 150); // IP address of the server
IPAddress gateway(192, 168, 69, 1); // Gateway IP address
IPAddress subnet(255, 255, 255, 0); // Subnet mask

EthernetClient client;

/--------------------------------------------------------------------------------------
Interrupt handler for Timer1 (TimerOne) driven DMD refresh scanning, this gets
called at the period set in Timer1.initialize();
--------------------------------------------------------------------------------------
/
void ScanDMD()
{
dmd.scanDisplayBySPI();
}

int getFreeMemory() {
extern int __heap_start, *__brkval;
int v;
return (int)&v - (__brkval == 0 ? (int)&__heap_start : (int)__brkval);
}

void setup()
{
Serial.begin(9600);

// Initialize Ethernet library
// Ethernet.begin(mac);
Ethernet.begin(mac, ip, gateway, gateway, subnet);

// Wait for Ethernet to be initialized
delay(1000);

Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());

Serial.println("Connecting to server...");
// Connect to the server
connectToServer();

// initialize TimerOne's interrupt/CPU usage used to scan and refresh the display
Timer1.initialize(5000); // period in microseconds to call ScanDMD. Anything longer than 5000 (5ms) and you can see flicker.
Timer1.attachInterrupt(ScanDMD); // attach the Timer1 interrupt to ScanDMD which goes to dmd.scanDisplayBySPI()

// clear/init the DMD pixels held in RAM
dmd.clearScreen(false);
delay(5000);
dmd.clearScreen(true); // true is normal (all pixels off), false is negative (all pixels on)

dmd.drawBox(0, 0, (32 * DISPLAYS_ACROSS) - 1, (16 * DISPLAYS_DOWN) - 1, GRAPHICS_NORMAL);
dmd.selectFont(SystemFont5x7);
}

void loop()
{
int freeMem = getFreeMemory();

Serial.print("Free Memory: ");
Serial.print(freeMem);
Serial.println(" bytes");

if (is_screen_value_updated)
{
dmd.selectFont(SystemFont5x7);
resultant = count + "/" + bag_limit;
resultantCharSize = resultant.length() + 1;
resultant.toCharArray(resultantChar, resultantCharSize);
dmd.drawString(5, 2, resultantChar, resultantCharSize, GRAPHICS_NORMAL);

vehicleDesctiptionCharSize = vehicle_desctiption.length() + 1;
vehicle_desctiption.toCharArray(vehicleDesctiptionChar, vehicleDesctiptionCharSize);
dmd.drawString(2, 1 + 16, vehicleDesctiptionChar, vehicleDesctiptionCharSize, GRAPHICS_NORMAL);
is_screen_value_updated = false;

}
if(is_screen_cleared){
dmd.clearScreen(true); // true is normal (all pixels off), false is negative (all pixels on)

dmd.drawBox(0, 0, (32 * DISPLAYS_ACROSS) - 1, (16 * DISPLAYS_DOWN) - 1, GRAPHICS_NORMAL);
dmd.selectFont(SystemFont5x7);
is_screen_cleared = false;

}
currentMillis = millis();
if (client.connected())
{
// Client is connected
if (client.available())
{
previousPingTime = currentMillis; // Update the last ping time
// Read the data from the server until a . character is encountered
receivedData = client.readStringUntil('.');
receivedData.toCharArray(receivedDataChar, receivedData.length() + 1);

  // Split the string
  char *token = strtok(receivedDataChar, &delimiter);
  while (token != NULL)
  {
    if (token[0] == 'b')
    {
      belt_ids = String(token).substring(2);
      belt_ids.toUpperCase();
      index_of_delimiter = belt_ids.indexOf(',');
      belt_id_1=belt_ids.substring(0, index_of_delimiter);
      belt_id_2=belt_ids.substring(index_of_delimiter+1);
      if(belt_id_1!=BELT_ID && belt_id_2!=BELT_ID){
        break;
      }
    }
    if (token[0] == 'c' && strncmp(token, "clear", sizeof("clear") - 1) == 0){
      is_screen_cleared = true;
    }
    if (token[0] == 'c')
    {
      is_screen_value_updated = true;
      count = String(token).substring(2);
    }

    else if (token[0] == 'l')
    {
      is_screen_value_updated = true;
      bag_limit = String(token).substring(2);
    }

    else
    {
      is_screen_value_updated = true;
      vehicle_desctiption = String(token).substring(2);
      vehicle_desctiption.toUpperCase();
    }

    // Get the next token
    token = strtok(NULL, &delimiter);
  }
}
// Check for server responsiveness every pingInterval milliseconds
if (currentMillis - previousPingTime >= pingInterval) {
  previousPingTime = currentMillis; // Update the last ping time
  if (!pingServer()) {
    // Server not responsive, handle disconnection
    client.stop(); // Disconnect the client
  }
}

}
else
{
// Client is disconnected
Serial.println("Server disconnected!");

// Attempt to reconnect to the server
connectToServer();

}
delay(100);
}

void connectToServer()
{
Serial.println("Connecting to server...");
// Connect to the server
if (client.connect(serverIP, serverPort))
{
Serial.println("Connected!");
}
else
{
Serial.println("Connection failed!");
// Retry connection after a delay
delay(1000);
}
}

bool pingServer() {
client.print("PING\n"); // Send a ping request
delay(100); // Wait for a response

bytesRead = 0;

while (client.available() && bytesRead < sizeof(response)) {
response[bytesRead++] = client.read();
}
response[bytesRead] = '\0'; // Null-terminate the response array
return strcmp(response, "PONG") == 0; // Check if response is as expected
}

Can you please enlighten me on the issue

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant