Arduino HTTP Get/Post code running slow

Hi guys,

I'm running in a bit of an issue in my project : I'm using a php webapp in order to make my Arduino MK1010 WiFI an alarm. It is connected to a vibrating motor, and everything is working how intended.

However, the arduino loop is running quite slowly, which is kind of problematic because in order to stop the motor from vibrating, the Arduino parses the value of a JSON payload, meaning that it takes about 15 seconds for the code to finish looping and then setting the motor off.

I would appreciate any kind of help in order to optimize/understand why it is so slow to loop !

Here's my sketch :

// ArduinoJson - Version: 6.18.5
#include <ArduinoJson.h>
#include <ArduinoJson.hpp>




#include <SPI.h>
#include <WiFiNINA.h>
#include <WiFiUdp.h>
#include <ArduinoHttpClient.h>


int status = WL_IDLE_STATUS;
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID;        // your network SSID (name)
char pass[] = SECRET_PASS;    // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0;            // your network key Index number (needed only for WEP)
int setTime = 1;
int myID = 5;
int motorPin = 3;

char serverAddress[] = "http://mydomainname.com";  
int port = 80;
WiFiClient wifi;
HttpClient client= HttpClient(wifi, serverAddress, port);

unsigned int localPort = 2390;      // local port to listen for UDP packets

IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server

const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP

WiFiUDP Udp;
WiFiServer server(80);

void setup() {

  // Open serial communications and wait for port to open:

  Serial.begin(9600);
  pinMode(motorPin, OUTPUT);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for native USB port only

  }

  // check for the WiFi module:

  if (WiFi.status() == WL_NO_MODULE) {

    Serial.println("Communication with WiFi module failed!");

    // don't continue

    while (true);

  }

  String fv = WiFi.firmwareVersion();

  if (fv < WIFI_FIRMWARE_LATEST_VERSION) {

    Serial.println("Please upgrade the firmware");

  }

  // attempt to connect to Wifi network:

  while (status != WL_CONNECTED) {

    Serial.print("Attempting to connect to SSID: ");

    Serial.println(ssid);

    // Connect to WPA/WPA2 network. Change this line if using open or WEP network:

    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:

    delay(10000);

  }

  Serial.println("Connected to wifi");

  printWifiStatus();

  Serial.println("\nStarting connection to server...");

  Udp.begin(localPort);
}

void loop() {

  sendNTPpacket(timeServer); // send an NTP packet to a time server

  // wait to see if a reply is available

  delay(5000);
  Serial.println("poggers");
  if (Udp.parsePacket()) {

    Serial.println("packet received");

    // We've received a packet, read the data from it

    Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

    //the timestamp starts at byte 40 of the received packet and is four bytes,

    // or two words, long. First, esxtract the two words:

    unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);

    unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);

    // combine the four bytes (two words) into a long integer

    // this is NTP time (seconds since Jan 1 1900):

    unsigned long secsSince1900 = highWord << 16 | lowWord;

    Serial.print("Seconds since Jan 1 1900 = ");

    Serial.println(secsSince1900);

    // now convert NTP time into everyday time:

    Serial.print("Unix time = ");

    // Unix time starts on Jan 1 1970. In seconds, that's 2208988800:

    const unsigned long seventyYears = 2208988800UL;

    // subtract seventy years:

    unsigned long epoch = secsSince1900 - seventyYears;

    // print Unix time:

    Serial.println(epoch);

    // print the hour, minute and second:

    Serial.print("The UTC time is ");       // UTC is the time at Greenwich Meridian (GMT)

    Serial.print((epoch  % 86400L) / 3600); // print the hour (86400 equals secs per day)

    Serial.print(':');

    if (((epoch % 3600) / 60) < 10) {

      // In the first 10 minutes of each hour, we'll want a leading '0'

      Serial.print('0');

    }

    Serial.println((epoch  % 3600) / 60); // print the minute (3600 equals secs per minute)
    
    WiFiClient clientListener = server.available();   // listen for incoming clients
    epoch = epoch - (epoch % 60);
    Serial.println("arduino epoch = ");
    Serial.println(epoch);
    setTime = fgetData();
    
    if (setTime > 0)
    {
      if (epoch >= setTime)
      {
        if (setTime == 1)
        {
            digitalWrite(motorPin, LOW);  // deactivate the motor
        }
        else
        {
          digitalWrite(motorPin, HIGH);  // activate the motor
          fpostData();
        }
      }
    }
  }

  // wait 20 seconds before asking for the SSID time and the webapp set time again

  delay(10000);

}

int fgetData() {
  
  String line = "";
  if (client.connect("mydomainname.com",80)) {
    Serial.println("COLLECTconnected");
    client.println("GET /get/json/json_payload.php HTTP/1.1");
    client.println("Host: mydomainname.com");
    client.println("Connection: close");
    client.println();
  } 
  else {
    Serial.println("connection failed");
  }
  Serial.println("[Response:]");
  while (client.connected() || client.available())
  {
    if (client.available())
    {
      String line = client.readStringUntil('\n');
      if (line.indexOf("time") > 0)
      {
        Serial.println("\n line = \n");
        Serial.println(line);
        Serial.println("\n end of read line \n");
        StaticJsonDocument<200> doc;
        int lineLength = line.length();
        char json[lineLength];
        line.toCharArray(json, lineLength);

        DeserializationError error = deserializeJson(doc, json);

      // Test if parsing succeeds.
        if (error) {
          Serial.print(F("deserializeJson() failed: "));
          Serial.println(error.f_str());
          return (-1);
        }

        int i = 0;
        int id = 0;
        int time = 0;
        while (i <= doc.size())
        {
          id = doc[i]["id"];
          time = doc[i]["time"];
          if (id == myID)
          {
            Serial.println("id = ");
            Serial.println(id);
            Serial.println("time = ");
            Serial.println(time);
            return (time);
          }
          i++;
        }
      }
    }
  }
  client.stop();
  Serial.println("\n[Disconnected]");
  return (-1);
}

void fpostData() {
  
String postData = "time=0&id=";
postData = String(postData+myID);

  if (client.connect("mydomainname.com", 80)) {
    client.println("POST /post/request.php HTTP/1.1");
    client.println("Host: www.mydomainname.com");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(postData.length());
    client.println();
    client.print(postData);
    int statusCode = client.responseStatusCode();
    Serial.print("Status code: ");
    Serial.println(statusCode);
  }
  if (client.connected()) {
    client.stop();
  }
  Serial.println("postData :\n");
  Serial.println(postData);
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) {

  //Serial.println("1");

  // set all bytes in the buffer to 0

  memset(packetBuffer, 0, NTP_PACKET_SIZE);

  // Initialize values needed to form NTP request

  // (see URL above for details on the packets)

  //Serial.println("2");

  packetBuffer[0] = 0b11100011;   // LI, Version, Mode

  packetBuffer[1] = 0;     // Stratum, or type of clock

  packetBuffer[2] = 6;     // Polling Interval

  packetBuffer[3] = 0xEC;  // Peer Clock Precision

  // 8 bytes of zero for Root Delay & Root Dispersion

  packetBuffer[12]  = 49;

  packetBuffer[13]  = 0x4E;

  packetBuffer[14]  = 49;

  packetBuffer[15]  = 52;

  //Serial.println("3");

  // all NTP fields have been given values, now

  // you can send a packet requesting a timestamp:

  Udp.beginPacket(address, 123); //NTP requests are to port 123

  //Serial.println("4");

  Udp.write(packetBuffer, NTP_PACKET_SIZE);

  //Serial.println("5");

  Udp.endPacket();

  //Serial.println("6");
}

void printWifiStatus() {

  // print the SSID of the network you're attached to:

  Serial.print("SSID: ");

  Serial.println(WiFi.SSID());

  // print your board's IP address:

  IPAddress ip = WiFi.localIP();

  Serial.print("IP Address: ");

  Serial.println(ip);

  // print the received signal strength:

  long rssi = WiFi.RSSI();

  Serial.print("signal strength (RSSI):");

  Serial.print(rssi);

  Serial.println(" dBm");
}

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]) : "";
}

Thanks in advance !

Remove or reduce those delays inside your main loop.

Speed of the loop depends on the speed of the board you are using, speed of your network, the time it takes to connect to all the services that you are connecting to, and to do everything that the code has to do inside the loop.

As you can see you are doing a lot of things inside your loop. You could try and optimize the cycle time a little bit. However inevitably it's gonna take an x amount of time that's gonna vary time to time in order to establish those connections and to do all the tasks.

Good Luck!

You spend a long time doing nothing...

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