Devices & Components
Arduino Mega 2560 Rev3
Ethernet Shield (Wiznet W5100) for Arduino
Mini Solar Panel
LDR, 5 Mohm
SG90 Micro-servo motor
DHT22 Temperature Sensor
Through Hole Resistor, 10 ohm
Resistor 330 ohm
5 mm LED: Red
Resistor 220 ohm
Software & Tools
Arduino IDE
Cayenne
Project description
Code
The embedded code of the IoT-based solar tracker system
arduino
1/*************************************************************** 2 PROJECT: IoT based solar tracker system / the embedded software 3 Aboubakr El Hammoumi/ aboubakr.elhammoumi@usmba.ac.ma 4***************************************************************/ 5 6#define CAYENNE_PRINT Serial 7#include <CayenneMQTTEthernet.h> //CayenneMQTT library 8#include <Servo.h> //Servo motor library 9#include <DHT.h> //DHT library 10#define DHTTYPE DHT22 11#define DHTPIN 2 12DHT dht(DHTPIN,DHTTYPE); 13 14//MQTT credentials 15char username[]="498d2d00-afe2-11ea-883c-638d8ce4c23d"; 16char password[]="ab4a8f92d94033c01f6e18ce1d8a84d8c304c9c4"; 17char clientID[]="17798a40-b968-11ea-93bf-d33a96695544"; 18 19Servo servo_x; //up-down servomotor 20int servoh = 0; 21int servohLimitHigh = 170; 22int servohLimitLow = 10; 23 24Servo servo_z; //left-right servomotor 25int servov = 0; 26int servovLimitHigh = 170; 27int servovLimitLow = 10; 28 29int topl,topr,botl,botr; 30int threshold_value=10; 31float Vout; 32 33void setup() 34{ Serial.begin(9600); 35 Cayenne.begin(username, password, clientID); 36 servo_x.attach(5); 37 servo_z.attach(6); 38 dht.begin(); 39 pinMode(3,OUTPUT); 40 digitalWrite(3,LOW); 41} 42 43void loop() 44{ topr= analogRead(A2); 45 topl= analogRead(A3); 46 botl= analogRead(A4); 47 botr= analogRead(A5); 48 Vout=(analogRead(A1) * 5.0) / 1023; 49 Serial.println(" Manual-mode"); 50 Cayenne.loop(); 51 52 if (digitalRead(3)==HIGH){ 53 Serial.println(" Automatic-mode"); 54 servoh = servo_x.read(); 55 servov = servo_z.read(); 56 int avgtop = (topr + topl) / 2; 57 int avgbot = (botr + botl) / 2; 58 int avgright = (topr + botr) / 2; 59 int avgleft = (topl + botl) / 2; 60 int diffhori= avgtop - avgbot; 61 int diffverti= avgleft - avgright; 62 63 /*tracking according to horizontal axis*/ 64 if (abs(diffhori) <= threshold_value) 65 { 66 servo_x.write(servoh); //stop the servo up-down 67 }else { 68 if (diffhori > threshold_value) 69 { Serial.println(" x - 2 "); 70 servo_x.write(servoh -2); //Clockwise rotation CW 71 if (servoh > servohLimitHigh) 72 { 73 servoh = servohLimitHigh; 74 } 75 delay(10); 76 }else { 77 servo_x.write(servoh +2); //CCW 78 if (servoh < servohLimitLow) 79 { 80 servoh = servohLimitLow; 81 } 82 delay(10); 83 } 84 } 85 /*tracking according to vertical axis*/ 86 if (abs(diffverti) <= threshold_value) 87 { 88 servo_z.write(servov); //stop the servo left-right 89 }else{ 90 if (diffverti > threshold_value) 91 { 92 servo_z.write(servov -2); //CW 93 if (servov > servovLimitHigh) 94 { 95 servov = servovLimitHigh; 96 } 97 delay(10); 98 }else{ 99 servo_z.write(servov +2); //CCW 100 if (servov < servovLimitLow) 101 { 102 servov = servovLimitLow; 103 } 104 delay(10); 105 } 106 } 107 } 108} 109// Cayenne Functions 110CAYENNE_IN(8){ 111 int value = getValue.asInt(); 112 CAYENNE_LOG("Channel %d, pin %d, value %d", 8, 3, value); 113 digitalWrite(3,value); 114} 115CAYENNE_IN(7){ //up-down servo motor 116 if (digitalRead(3)==HIGH){ //Automatic_mode 117 } 118 else{ //Manual_mode 119 servo_x.write(getValue.asDouble() * 180); 120 } 121} 122CAYENNE_IN(6){ //left-right servo motor 123 if (digitalRead(3)==HIGH){ 124 } 125 else{ 126 servo_z.write(getValue.asDouble() * 180); 127 } 128} 129 130CAYENNE_OUT(0) { //Current 131 float current = Vout/10; 132 Cayenne.virtualWrite(0, current); 133 Serial.print("Current: "); 134 Serial.println(current); 135} 136CAYENNE_OUT(1) { //Voltage 137 float voltage = Vout * 2; 138 Cayenne.virtualWrite(1, voltage); 139 Serial.print("Voltage: "); 140 Serial.println(voltage); 141} 142CAYENNE_OUT(2){ //LDR Top-right 143 Cayenne.virtualWrite(2, topr); 144} 145CAYENNE_OUT(3){ //LDR Top-left 146 Cayenne.virtualWrite(3,topl); 147} 148CAYENNE_OUT(4){ //LDR Bot-left 149 Cayenne.virtualWrite(4,botl); 150} 151CAYENNE_OUT(5){ //LDR Bot-right 152 Cayenne.virtualWrite(5,botr); 153} 154CAYENNE_OUT(10) { //Power 155 float power = (Vout * 2 * Vout)/10 ; 156 Cayenne.virtualWrite(10, power); 157 Serial.print("Power: "); 158 Serial.println(power); 159} 160CAYENNE_OUT(11){ //Temperature 161 float t = dht.readTemperature(); 162 //int chk = dht.read(DHT11PIN); 163 Cayenne.virtualWrite(11, t, TYPE_TEMPERATURE, UNIT_CELSIUS); 164 Serial.print("temperature: "); 165 Serial.println(t); 166} 167CAYENNE_OUT(12){ //Huidity 168 float h = dht.readHumidity(); 169 //int chk = dht.read(DHT11PIN); 170 Cayenne.virtualWrite(12, h); 171 Serial.print(" humidity: "); 172 Serial.println(h); 173} 174 175
Downloadable files
The electronic circuit of the IoT-based solar tracker
The electronic circuit of the IoT-based solar tracker

The electronic circuit of the IoT-based solar tracker
The electronic circuit of the IoT-based solar tracker

Comments
Only logged in users can leave comments