Internet of things (IoT)-based solar tracker

Greetings Gents,

I faced a problem when I try to perform the below project. I developed the dashboard but I can not controller. It goes offline once I try to change the servo moter speed and there are no live parameters in the gauges.

/***************************************************************
PROJECT: IoT based solar tracker system / the embedded software
Aboubakr El Hammoumi/ aboubakr.elhammoumi@usmba.ac.ma
***************************************************************/

#define CAYENNE_PRINT Serial
#include <CayenneMQTTEthernet.h>    //CayenneMQTT library 
#include <Servo.h>                  //Servo motor library 
#include <DHT.h>                    //DHT library 
#define DHTTYPE DHT22
#define DHTPIN 2
DHT dht(DHTPIN,DHTTYPE);

//MQTT credentials   
char username[]="498d2d00-afe2-11ea-883c-638d8ce4c23d";
char password[]="ab4a8f92d94033c01f6e18ce1d8a84d8c304c9c4";
char clientID[]="17798a40-b968-11ea-93bf-d33a96695544";

Servo servo_x;                   //up-down servomotor  
int servoh = 0;
int servohLimitHigh = 170;     
int servohLimitLow = 10;       

Servo servo_z;                   //left-right servomotor 
int servov = 0; 
int servovLimitHigh = 170;
int servovLimitLow = 10;

int topl,topr,botl,botr;
int threshold_value=10;        
float Vout;

void setup()
{ Serial.begin(9600);
  Cayenne.begin(username, password, clientID);
  servo_x.attach(5);
  servo_z.attach(6);
  dht.begin();
  pinMode(3,OUTPUT);
  digitalWrite(3,LOW); 
}

void loop()
{ topr= analogRead(A2);       
  topl= analogRead(A3);         
  botl= analogRead(A4);         
  botr= analogRead(A5);        
  Vout=(analogRead(A1) * 5.0) / 1023;
  Serial.println(" Manual-mode");
  Cayenne.loop();
  
  if (digitalRead(3)==HIGH){
    Serial.println(" Automatic-mode");
    servoh = servo_x.read();
    servov = servo_z.read();
    int avgtop = (topr + topl) / 2;     
    int avgbot = (botr + botl) / 2;   
    int avgright = (topr + botr) / 2;   
    int avgleft = (topl + botl) / 2;    
    int diffhori= avgtop - avgbot;      
    int diffverti= avgleft - avgright;    
    
    /*tracking according to horizontal axis*/ 
    if (abs(diffhori) <= threshold_value)
    {
     servo_x.write(servoh);            //stop the servo up-down
    }else {
       if (diffhori > threshold_value)
          { Serial.println(" x - 2 ");
          servo_x.write(servoh -2);    //Clockwise rotation CW
          if (servoh > servohLimitHigh)
          {
           servoh = servohLimitHigh;
          }
          delay(10);
          }else {
           servo_x.write(servoh +2);   //CCW
           if (servoh < servohLimitLow)
           {
           servoh = servohLimitLow;
           }
           delay(10);
           }
      }      
    /*tracking according to vertical axis*/ 
    if (abs(diffverti) <= threshold_value)
    {     
     servo_z.write(servov);       //stop the servo left-right
    }else{
       if (diffverti > threshold_value)
       { 
       servo_z.write(servov -2);  //CW
       if (servov > servovLimitHigh) 
       { 
       servov = servovLimitHigh;
       }
       delay(10);
       }else{ 
        servo_z.write(servov +2);  //CCW
        if (servov < servovLimitLow) 
        {
        servov = servovLimitLow;
        }
        delay(10);
        }
     }
  }
}
// Cayenne Functions
CAYENNE_IN(8){
  int value = getValue.asInt();
  CAYENNE_LOG("Channel %d, pin %d, value %d", 8, 3, value);
  digitalWrite(3,value);
}
CAYENNE_IN(7){ //up-down servo motor
  if (digitalRead(3)==HIGH){ //Automatic_mode
  }
  else{ //Manual_mode
  servo_x.write(getValue.asDouble() * 180);
  }
}
CAYENNE_IN(6){ //left-right servo motor
  if (digitalRead(3)==HIGH){
  }  
  else{
  servo_z.write(getValue.asDouble() * 180);
  }
}

CAYENNE_OUT(0) { //Current
  float current = Vout/10;
  Cayenne.virtualWrite(0, current);
  Serial.print("Current: ");
  Serial.println(current);
}
CAYENNE_OUT(1) { //Voltage
  float voltage = Vout * 2;
  Cayenne.virtualWrite(1, voltage);
  Serial.print("Voltage: ");
  Serial.println(voltage);
}
CAYENNE_OUT(2){ //LDR Top-right
  Cayenne.virtualWrite(2, topr);
}
CAYENNE_OUT(3){ //LDR Top-left
  Cayenne.virtualWrite(3,topl);
}
CAYENNE_OUT(4){ //LDR Bot-left
  Cayenne.virtualWrite(4,botl);
}
CAYENNE_OUT(5){ //LDR Bot-right
  Cayenne.virtualWrite(5,botr);
}
CAYENNE_OUT(10) { //Power
  float power = (Vout * 2 * Vout)/10 ;
  Cayenne.virtualWrite(10, power);
  Serial.print("Power: ");
  Serial.println(power);
}
CAYENNE_OUT(11){ //Temperature
  float t = dht.readTemperature();
  //int chk = dht.read(DHT11PIN);
  Cayenne.virtualWrite(11, t, TYPE_TEMPERATURE, UNIT_CELSIUS);
  Serial.print("temperature: ");
  Serial.println(t);
}
CAYENNE_OUT(12){ //Huidity
  float h = dht.readHumidity();
  //int chk = dht.read(DHT11PIN);
  Cayenne.virtualWrite(12, h);
  Serial.print("  humidity: ");
  Serial.println(h);
}

i am guessing the mega is restarting when the servos are turned ON because of the power consumption.
Can you check the serial monitor while the process, to find out what is exactly happening.

How i can fix it ?

First, confirm it in the serial monitor.

[0] MAC: FE-39-EB-EB-xx-xx
[20736] IP: xxxx
mqtt.mydevices.com:1883
[21236] Connected
Manual-mode
Manual-mode
Current: 0.40
Voltage: 7.95
Power: 3.16
temperature: 24.40
humidity: 70.20
Manual-mode
Manual-mode
Manual-mode
Ma

what does it print when you turn ON the servo motor from the dashboard?

it goes offline when i try to move the servo motor

yes, so what does the serial monitor output?

[0] MAC: FE
[16722] IP:
[16723] Connecting to mqtt.mydevices.com:1883
[17177] Connected
Manual-mode
Manual-mode
Current: 0.02
Voltage: 0.37
Power: 0.01
temperature: 25.30
humidity: 76.90
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode

----- this is what i found

Add #define CAYENNE_DEBUG and check the serial monitor.

[0] MAC:
[20734] IP:
[20734] Connecting to mqtt.mydevices.com:1883
[21256] Connected
[21450] Publish: topic 4, channel 65534, value Arduino Mega, subkey , key
[21464] Publish: topic 6, channel 65534, value ATmega2560, subkey , key
[21540] Publish: topic 7, channel 65534, value 16000000, subkey , key
[21615] Publish: topic 5, channel 65534, value 1.3.0, subkey , key
[21687] Publish: topic 8, channel 65534, value W5100, subkey , key
Manual-mode
Manual-mode
[23788] Publish: topic 1, channel 0, value 0.021, subkey , key
Current: 0.02
[23804] Publish: topic 1, channel 1, value 0.420, subkey , key
Voltage: 0.42
[23888] Publish: topic 1, channel 2, value 64, subkey , key
[23954] Publish: topic 1, channel 3, value 51, subkey , key
[24017] Publish: topic 1, channel 4, value 0, subkey , key
[24081] Publish: topic 1, channel 5, value 91, subkey , key
[24146] Publish: topic 1, channel 10, value 0.009, subkey , key
Power: 0.01
[24229] Publish: topic 1, channel 11, value 23.700, subkey c, key temp
temperature: 23.70
[24322] Publish: topic 1, channel 12, value 80.500, subkey , key
humidity: 80.50
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
[33503] Connection ok
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
Manual-mode
[39556] Publish: topic 1, channel 0, value 0.021, subkey , key
Current: 0.02
[39572] Publish: topic 1, channel 1, value 0.420, subkey , key
Voltage: 0.42
[39655] Publish: topic 1, channel 2, value 64, subkey , key
[39720] Publish: topic 1, channel 3, value 51, subkey , key
[39785] Publish: topic 1, channel 4, value 818, subkey , key
[39851] Publish: topic 1, channel 5, value 714, subkey , key
[39916] Publish: topic 1, channel 10, value 0.009, subkey , key
Power: 0.01
[40000] Publish: topic 1, channel 11, value 23.800, subkey c, key temp
temperature: 23.80
[40092] Publish: topic 1, channel 12, value 81.200, subkey , key
humidity: 81.20
Manual-mode
Manual-mode
Manual-mode
Manual-mode
[44229] Connection ok
Manual-mode
[45102] Message received: topic 2, channel 6
[45102] In: value 69, channel 6
[45117] publishState: topic 1 channel 6
[45159] Publish: topic 1, channel 6, value 69, subkey , key
[45224] Send response: 89C4r9swGLqF2qw
Manual-mode

what do you get after this?

Nothing much

Can anyone help me please …,

How are you powering your servo motor? Is it from the arduino power pins? If yes, try powering them with a separate power supply and common ground.