Aboubakr_El HammoumiSaad Motahhir
Published © MIT

Internet of things (IoT)-based solar tracker

The proposed IoT solution can be employed in various fields to connect devices or things to the internet.

IntermediateFull instructions provided7,728
Internet of things (IoT)-based solar tracker

Things used in this project

Hardware components

Arduino Mega 2560
Arduino Mega 2560
×1
Ethernet Shield (Wiznet W5100) for Arduino
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×2
LDR, 5 Mohm
LDR, 5 Mohm
×4
DHT22 Temperature Sensor
DHT22 Temperature Sensor
×1
Mini Solar Panel
×1
Resistor 330 ohm
Resistor 330 ohm
×4
Through Hole Resistor, 10 ohm
Through Hole Resistor, 10 ohm
×2
Resistor 220 ohm
Resistor 220 ohm
×1
5 mm LED: Red
5 mm LED: Red
×1

Software apps and online services

Cayenne
myDevices Cayenne
Arduino IDE
Arduino IDE

Story

Read more

Schematics

The electronic circuit of the IoT-based solar tracker

Code

The embedded code of the IoT-based solar tracker system

Arduino
/***************************************************************
 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);
}

Credits

Aboubakr_El Hammoumi

Aboubakr_El Hammoumi

1 project • 25 followers
Saad Motahhir

Saad Motahhir

2 projects • 26 followers

Comments