Matthew Aggari
Published

Lane Tech HS Sunset Tracker

My project is a neat piece of art that uses a servo and a sunset api to show whether the sun will set early or late each morning.

BeginnerFull instructions provided223
Lane Tech HS Sunset Tracker

Things used in this project

Hardware components

Argon
Particle Argon
×1
SG90 Micro-servo motor
SG90 Micro-servo motor
×1
Male/Male Jumper Wires
×1

Software apps and online services

sunset api
Particle Build Web IDE
Particle Build Web IDE

Hand tools and fabrication machines

Hot glue gun (generic)
Hot glue gun (generic)

Story

Read more

Custom parts and enclosures

Custom Fritzing Part for the SG90 servo

Schematics

Servo routing.

Power connects to 3v3 (red/yellow), ground (brown/green) connects to ground and data is sent through D2 (orange/orange)

Code

Manual code

C/C++
This is the code that is meant to be flashed to the argon. If you plan to have your project setup to run automatically and be plugged in all the time, uncomment lines 73-76 and comment line 79, If you wish to test times and monitor variables uncomment lines 56 and 62-70 respectively.
#include <ArduinoJson.h>

StaticJsonDocument<512> doc;
// create servo object to control a servo
Servo myservo;  
// variable to store the servo position
int pos = 0;    

void setup()
{
    // Subscribe to the webhook response event
    Particle.subscribe("hook-response/sunset", myHandler, MY_DEVICES);
    myservo.attach(D2);  // attaches the servo on the D2 pin to the servo object
    Serial.begin(9600);
    
}

void loop() {
  // Get some data
  String data = String(180);
  // Trigger the integration
  Particle.publish("sunset", data, PRIVATE);
  // Wait 6 seconds
  delay(6000);
}

void myHandler(const char *event, const char *data)
{
    //retrieving only the hour from the char returned by my webhook and making it into an int
    String colin (":");
    String WordSearch (data);
    int colinPos = WordSearch.indexOf(colin, 0);
    String calcTime = WordSearch.substring(0, colinPos);
    int intCalcTime = calcTime.toInt();
    int finalTime = 0;
    
    // Converting UTC time to Central time
    if( intCalcTime == 10)
    finalTime = 4;
    else if( intCalcTime == 11)
    finalTime = 5;
    else if( intCalcTime == 12)
    finalTime = 6;
    else if( intCalcTime == 1)
    finalTime = 7;
    else if( intCalcTime == 2)
    finalTime = 8;
    else if( intCalcTime == 3)
    finalTime = 9;
    else if( intCalcTime == 4)
    finalTime = 10;
    else if( intCalcTime == 5)
    finalTime = 11;
    
    //uncomment line below to test different times
    //finalTime = 7;
    
    //calculate Servo Angle
    double ServoAngle;
    if (finalTime > 7) 
    ServoAngle= 180;
    else
    ServoAngle = 180-((double)(finalTime-4)/3)*180;
   
   //Uncomment these printlines to troubleshoot any timezone conversion issues and check variables
    Serial.print("Webhook Sunset ");
    Serial.println(data);
    Serial.println("Sunset String " + WordSearch);
    Serial.println("Colin Position " + colinPos);
    Serial.println("Hours String " + calcTime);
    Serial.println(finalTime);
    Serial.print("ServoAngle ");
    Serial.println(ServoAngle);
    Serial.println(Time.hour(Time.now()));

    //uncomment to have code run automatically at the set time everyday
    //if (Time.hour(Time.now())==13)
    //{
    //myservo.write((int)ServoAngle);
    //}
    //
    //comment the line below if intending to run automatically as described above
    myservo.write((int)ServoAngle); //this code actually sets the servo angle
}

Credits

Matthew Aggari
3 projects • 2 followers
Contact

Comments

Please log in or sign up to comment.