nbiot device receives data slowly or doesn't receive answers from the server

NB-IOT Cannot receive response information from the server at real times
I do not understand. ask for advice.

#include <Wire.h>
#include <SoftwareSerial.h>
#include "SSD1306Ascii.h"
#include "SSD1306AsciiWire.h"
#include <PZEM004T.h>
#include <Arduino.h>
#include <ArduinoJson.h>
#include "BC95Udp.h"
#include <Dns.h>
#include <TaskScheduler.h>

void t1Callback();
void t2Callback();
void t3Callback();
void read_s();
void volt();
void watt();
void amp();
void com();
float calBill(float Unit, float ft, bool over_150_Unit_per_month = false) ;
// Config time
int timezone = 7;
// Config bill
#define FIX_FT -15.90

int dst = 0;
unsigned long lastUpdateEnergy = 0, lastUpdateFirebase = 0;
float Power1,Power2,Power3,Power4,Power5;
unsigned long Energy = 0;
//PZEM004T pzem(51, 52); // (RX,TX) connect to TX,RX of PZEM
IPAddress ip(192, 168, 1, 1);

#define I2C_ADDRESS 0x3C
#define RST_PIN -1
SSD1306AsciiWire oled;
#if defined(__AVR_ATmega328P__) || defined(__AVR_ATmega168__)
  #include <AltSoftSerial.h>
  AltSoftSerial bc95serial;
#elif defined(__AVR_ATmega1280__) || (__AVR_ATmega2560__)
#define bc95serial Serial1
#endif


BC95UDP udpclient;

DNSClient dns;
IPAddress remoteip;
IPAddress remoteip2;
IPAddress remoteip_old;

int x;
int dot = 10;
int ledPin1 = 31;
int ledPin2 = 33;
int ledPin3 = 35;
int ledPin4 = 37;


PZEM004T pzem1(50,51); // (RX,TX) conhhhh6nect to TX,RX of PZEM
PZEM004T pzem2(52,53); // (RX,TX) connect to TX,RX of PZEM
PZEM004T pzem3(10,11); // (RX,TX) connect to TX,RX of PZEM
PZEM004T pzem4(12,13); // (RX,TX) connect to TX,RX of PZEM
PZEM004T pzem5(62,63); // (RX,TX) connect to TX,RX of PZEM

Task t1(3000, TASK_FOREVER, &t1Callback); 
Task t3(500, TASK_FOREVER, &t3Callback);
Task t4(100, TASK_FOREVER, &read_s);

Task t2(1000, TASK_FOREVER, &volt);
Task t5(1000, TASK_FOREVER, &watt);
Task t6(1000, TASK_FOREVER, &amp);

//Task t7(100, TASK_FOREVER, &com);
Scheduler runner;

float p1,p2,p3,p4,p5,i1,i2,i3,i4,i5,w1,w2,w3,w4,w5;
String str;
String stringOne;
uint8_t buff[64];
String head;
String body;
//******************************************************************//
void reboot(){
  oled.clear();
  oled.setFont(Adafruit5x7);
  Serial.print(F("Restrat...."));
  oled.setCursor (0, 4);
  oled.print("Restart...UDP Setting New IP Address");
  setup();
}
void printHEX(uint8_t *buff, size_t len) {
    Serial.println("Request...");
    for (int i=0; i<len; i++) {
        head = buff[11];
        body = buff[15];
        Serial.print(buff[i]);
    }
    Serial.println("");
        if(head == "49"){
          if(body == "49"){
            digitalWrite(ledPin1, LOW);
          }else{
            digitalWrite(ledPin1, HIGH);
          }
        }
        if(head == "50"){
          if(body == "49"){
            digitalWrite(ledPin2, LOW);
          }else{
            digitalWrite(ledPin2, HIGH);
          }
        }
        if(head == "51"){
          if(body == "49"){
            digitalWrite(ledPin3, LOW);
          }else{
            digitalWrite(ledPin3, HIGH);
          }
        }
        if(head == "52"){
          if(body == "49"){
            digitalWrite(ledPin4, LOW);
          }else{
            digitalWrite(ledPin4, HIGH);
          }
        }
    
}

void setup() {
    pinMode(ledPin1, OUTPUT);
    pinMode(ledPin2, OUTPUT);
    pinMode(ledPin3, OUTPUT);
    pinMode(ledPin4, OUTPUT);
    
    digitalWrite(ledPin1, HIGH);
    digitalWrite(ledPin2, HIGH);
    digitalWrite(ledPin3, HIGH);
    digitalWrite(ledPin4, HIGH);
    
    Wire.begin();
    bc95serial.begin(9600);
    BC95.begin(bc95serial);
    BC95.reset();
    
    Serial.begin(9600);

    Wire.begin();
    Wire.setClock(400000L);
  
    #if RST_PIN >= 0
      oled.begin(&Adafruit128x64, I2C_ADDRESS, RST_PIN);
    #else // RST_PIN >= 0
      oled.begin(&Adafruit128x64, I2C_ADDRESS);
    #endif // RST_PIN >= 0
    
    Serial.print(F("Starting"));

    while (!BC95.attachNetwork()) {
          oled.setFont(Adafruit5x7);
          oled.setCursor (0, 4);
          oled.print("Start");
          oled.setCursor (dot*3, 4);
          oled.print(".");
          
        Serial.print(".");
        delay(1000);
        dot++;
    }

    dns.begin();
    dns.getHostByName("myhost", remoteip);
    remoteip_old = remoteip;
    Serial.print(remoteip);
    Serial.print(F("NB-IOT module IP address : "));
    Serial.println(BC95.getIPAddress());
    udpclient.begin(8053);
    udpclient.beginPacket(remoteip, 41239);

    oled.setFont(Adafruit5x7);
    oled.setCursor (0, 4);
    oled.print("Connection Success");

    oled.setFont(Adafruit5x7);    
    runner.init();
    runner.addTask(t1);
    runner.addTask(t2);
    runner.addTask(t3);
    runner.addTask(t4);
    runner.addTask(t5);
    runner.addTask(t6);
    //runner.addTask(t7);
    t1.enable();
    t2.enable();
    t3.enable();
    t4.enable();
    t5.enable();
    t6.enable();

    //t7.enable();
}

void loop() {
  runner.execute();
}

void t1Callback() {
    dns.getHostByName("myhost", remoteip2);
    Serial.print(remoteip_old);
    Serial.print("|");
    Serial.print(remoteip2);
    Serial.println();
    if(remoteip2 != remoteip_old){
       reboot();
    }else{
      Serial.print("False");
      Serial.println();
    }
}
void t3Callback(){
            oled.clear();
            oled.setCursor (37, 0);
            oled.print("Sensor 1");
            
            oled.setCursor (10, 2);
            oled.print("Watt :");
            oled.setCursor (50, 2);
            oled.print(w1);
            oled.setCursor (100, 2);
            oled.print("W");

            oled.setCursor (10, 3);
            oled.print("Volt :");
            oled.setCursor (50, 3);
            oled.print(p1);
            oled.setCursor (100, 3);
            oled.print("V");

            oled.setCursor (16, 4);
            oled.print("Amp :");
            oled.setCursor (50, 4);
            oled.print(i1);
            oled.setCursor (100, 4);
            oled.print("A");
            
            oled.setCursor (0, 6);
            oled.print("IP :");
            oled.setCursor (30, 6);
            oled.print(remoteip_old);
        //delay(2000);
        //read_s();
          oled.clear();
            oled.setCursor (37, 0);
            oled.print("Sensor 2");
            
            oled.setCursor (10, 2);
            oled.print("Watt :");
            oled.setCursor (50, 2);
            oled.print(w2);
            oled.setCursor (100, 2);
            oled.print("W");

            oled.setCursor (10, 3);
            oled.print("Volt :");
            oled.setCursor (50, 3);
            oled.print(p2);
            oled.setCursor (100, 3);
            oled.print("V");

            oled.setCursor (16, 4);
            oled.print("Amp :");
            oled.setCursor (50, 4);
            oled.print(i2);
            oled.setCursor (100, 4);
            oled.print("A");
            
            oled.setCursor (0, 6);
            oled.print("IP :");
            oled.setCursor (30, 6);
            oled.print(remoteip_old);
      //delay(2000);
      //read_s();
            oled.clear();

            oled.setCursor (37, 0);
            oled.print("Sensor 3");
            
            oled.setCursor (10, 2);
            oled.print("Watt :");
            oled.setCursor (50, 2);
            oled.print(w3);
            oled.setCursor (100, 2);
            oled.print("W");

            oled.setCursor (10, 3);
            oled.print("Volt :");
            oled.setCursor (50, 3);
            oled.print(p3);
            oled.setCursor (100, 3);
            oled.print("V");

            oled.setCursor (16, 4);
            oled.print("Amp :");
            oled.setCursor (50, 4);
            oled.print(i3);
            oled.setCursor (100, 4);
            oled.print("A");
            
            oled.setCursor (0, 6);
            oled.print("IP :");
            oled.setCursor (30, 6);
            oled.print(remoteip_old);
      //delay(2000);
      //read_s();
            oled.clear();
            oled.setCursor (37, 0);
            oled.print("Sensor 4");
            
            oled.setCursor (10, 2);
            oled.print("Watt :");
            oled.setCursor (50, 2);
            oled.print(w4);
            oled.setCursor (100, 2);
            oled.print("W");

            oled.setCursor (10, 3);
            oled.print("Volt :");
            oled.setCursor (50, 3);
            oled.print(p4);
            oled.setCursor (100, 3);
            oled.print("V");

            oled.setCursor (16, 4);
            oled.print("Amp :");
            oled.setCursor (50, 4);
            oled.print(i4);
            oled.setCursor (100, 4);
            oled.print("A");
            
            oled.setCursor (0, 6);
            oled.print("IP :");
            oled.setCursor (30, 6);
            oled.print(remoteip_old);
      //delay(2000);
      //read_s();
            oled.clear();
            oled.setCursor (37, 0);
            oled.print("Sensor 5");
            
            oled.setCursor (10, 2);
            oled.print("Watt :");
            oled.setCursor (50, 2);
            oled.print(w5);
            oled.setCursor (100, 2);
            oled.print("W");

            oled.setCursor (10, 3);
            oled.print("Volt :");
            oled.setCursor (50, 3);
            oled.print(p5);
            oled.setCursor (100, 3);
            oled.print("V");

            oled.setCursor (16, 4);
            oled.print("Amp :");
            oled.setCursor (50, 4);
            oled.print(i5);
            oled.setCursor (100, 4);
            oled.print("A");
            
            oled.setCursor (0, 6);
            oled.print("IP :");
            oled.setCursor (30, 6);
            oled.print(remoteip_old);
}
void read_s(){
   size_t len = udpclient.read(buff, 64);
   printHEX(buff, len);
   udpclient.endPacket();
   udpclient.parsePacket();
}
void watt(){
    str = "";
    stringOne = "";
    str = "|w1|"+String(w1)+"|w2|"+String(w2)+"|w3|"+String(w3)+"|w4|"+String(w4)+"|w5|"+String(w5)+"|"; 
    int str_len = str.length() + 1; 
    char char_array[str_len];
    str.toCharArray(char_array, str_len);
    for(int i=0;i<=str_len;i++){
      stringOne +=  String(char_array[i], HEX);
    }     

    Serial.println(stringOne);
    
    udpclient.write(char_array, str_len);
    udpclient.endPacket();
    udpclient.parsePacket();

}
void amp(){
    str = "";
    stringOne = "";
    str = "|i1|"+String(i1)+"|i2|"+String(i2)+"|i3|"+String(i3)+"|i4|"+String(i4)+"|i5|"+String(i5)+"|"; 
    int str_len = str.length() + 1; 
    char char_array[str_len];
    str.toCharArray(char_array, str_len);
    for(int i=0;i<=str_len;i++){
      stringOne +=  String(char_array[i], HEX);
    }
    Serial.println(stringOne);

    udpclient.write(char_array, str_len);
    udpclient.endPacket();
    udpclient.parsePacket();

}
void volt(){
    str = "";
    stringOne = "";
    str = "|v1|"+String(p1)+"|v2|"+String(p2)+"|v3|"+String(p3)+"|v4|"+String(p4)+"|v5|"+String(p5)+"|";  
    int str_len = str.length() + 1; 
    char char_array[str_len];
    str.toCharArray(char_array, str_len);
    for(int i=0;i<=str_len;i++){
      stringOne +=  String(char_array[i], HEX);
    }

    Serial.println(stringOne);

    udpclient.write(char_array, str_len);
    udpclient.endPacket();
    udpclient.parsePacket();

}
void compu(){
    p1 = pzem1.voltage(ip);
    p2 = pzem2.voltage(ip);
    p3 = pzem3.voltage(ip);
    p4 = pzem4.voltage(ip);
    p5 = pzem5.voltage(ip);
    i1 = pzem1.current(ip);
    i2 = pzem2.current(ip);
    i3 = pzem3.current(ip);
    i4 = pzem4.current(ip);
    i5 = pzem5.current(ip);
    w1 = pzem1.power(ip);
    w2 = pzem2.power(ip);
    w3 = pzem3.power(ip);
    w4 = pzem4.power(ip);
    w5 = pzem5.power(ip);
}

Don't you also think we need a bit more information to be able to help you? I suggest you read the sticky post at the top of the forum topic.

I'm sorry :frowning:

I want to receive a response from the server, but my nbiot device is receiving data slowly or doesn't receive a reply Like being addicted to something, can you help me check?

No description what nbiot device is, no wiring diagram, no links to the hardware used, no links to the libraries used. Did you really read the sticky post at the top of the forum topic?

Connecting equipment :

Library :

Thing I want :

I sent the value to my udp server and let the udp server reply to control the leds.

What is the problem :

nb-iot shield receives information from the udp server sometimes or does not receive at all, I think my code is breaking the work, causing the loop to receive the wrong data

:confused:

NB-IOT : Narrowband IoT - Wikipedia

My connection is an ordinary shield. I just want to help look at the error sequence code.
Thank You

I'm still missing many links to used libraries.

One of them I found using Google (I guess this is the one you're using, I hope you can see you're wasting our time letting us search for stuff you had to provide). PZEM004T library is using SoftwareSerial to connect to that energy meter. Have have 5 of these objects but you can have only one SoftwareSerial instance listening at any one moment. Your setup cannot work that way.

The compu routine is never called in your sketch.

You're using the String object. Sooner or later this will kill your sketch (fragments memory, although the Mega has 4 times the RAM of the UNO it's still limited and you cannot waste it with code like the String class.

nb-iot shield receives information from the udp server sometimes or does not receive at all, I think my code is breaking the work, causing the loop to receive the wrong data

The posted code doesn't receive anything, it just sends out data but that data is never updated.