Created
January 30, 2020 01:17
-
-
Save wgaylord/cf30a021a3592b5e7072bc2374c0e6b9 to your computer and use it in GitHub Desktop.
Code used on an ESP32 for green satnogs station.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <HTTPClient.h> | |
#include <ArduinoJson.h> | |
#include <time.h> | |
#include <sys/time.h> | |
#define SSID "" | |
#define PASSWORD "" | |
#define NTPSERVER "pool.ntp.org" | |
#define TOKEN "Token API_KEY" | |
#define STATION_ID 0 | |
#define SHUTDOWN_PIN 4 | |
#define BOOT_PI 15 | |
#define STATUS_LED 2 | |
#define DELAY 1000*180 | |
#define TIME_BEFORE_BOOT 300 | |
void shutdownPi(); | |
DynamicJsonDocument doc(32862/4); //For reading JSON | |
struct tm tempTM; //Temp Time | |
HTTPClient http; //HTTPClient instance | |
time_t EndofObs; //Time for saved end of obs | |
time_t StartofObs; //TIme for saved start of obs | |
bool piStatus = false; //Status of the Pi running or not. | |
time_t getTimeFromStringTime(String str) //Converts the string time from the json to a time_t | |
{ | |
char char_array[str.length() + 1]; | |
str.toCharArray(char_array, str.length() + 1); | |
int Year, Month, Day, Hour, Minute, Second ; | |
sscanf(char_array, "%d-%d-%dT%d:%d:%dZ", &Year, &Month, &Day, &Hour, &Minute, &Second); | |
tempTM.tm_year = Year-1900; | |
tempTM.tm_mon = Month-1; | |
tempTM.tm_mday = Day; | |
tempTM.tm_hour = Hour; | |
tempTM.tm_min = Minute; | |
tempTM.tm_sec = Second; | |
time_t t = mktime(&tempTM); | |
return t; | |
} | |
time_t getTimeTill(time_t t){ //Get the time between now and the passed in time | |
time_t now; | |
time(&now); | |
return t-now; | |
} | |
void setup() { | |
Serial.begin(9600); | |
delay(1000); | |
WiFi.begin(SSID, PASSWORD); //Connect to wifi | |
delay(1000); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(1000); | |
Serial.println("Connecting to WiFi.."); | |
} | |
configTime(0, 0, NTPSERVER); //Configure the time server. | |
Serial.println("Connected to the WiFi network"); | |
EndofObs = -700; | |
StartofObs = 0; | |
//Init pinModes and states | |
pinMode(BOOT_PI, OUTPUT); | |
pinMode(SHUTDOWN_PIN,OUTPUT); | |
pinMode(STATUS_LED,OUTPUT); | |
pinMode(5,OUTPUT); | |
digitalWrite(BOOT_PI,HIGH); | |
digitalWrite(SHUTDOWN_PIN,HIGH); | |
shutdownPi(); | |
} | |
void loop() { | |
digitalWrite(STATUS_LED,HIGH); | |
http.begin("https://network.satnogs.org/api/jobs/?format=json&ground_station="+STATION_ID); //Specify the URL and certificate | |
http.addHeader("Authorization",TOKEN); //Add the header to mark station as online | |
int httpCode = http.GET(); | |
if (httpCode > 0) { //Check for the returning code | |
String payload = http.getString(); | |
deserializeJson(doc,payload); //Get the json from jobs | |
JsonArray array = doc.as<JsonArray>(); //Array of Jobs | |
if(array.size() > 0){ | |
JsonVariant obs = array.getElement(array.size()-1); //Get the first job | |
JsonVariant start = obs["start"]; //Get its start time | |
JsonVariant end = obs["end"]; //Get its end time | |
time_t startTime = getTimeFromStringTime(start.as<String>()); //Convert to time_t | |
time_t endTime = getTimeFromStringTime(end.as<String>()); //Convert to time_t | |
if((getTimeTill(StartofObs) < 0) & (getTimeTill(EndofObs) < -(TIME_BEFORE_BOOT*2))){ //Should update current start and end | |
StartofObs = startTime; | |
EndofObs = endTime; | |
if(((getTimeTill(startTime) > 1800 ) & piStatus)){ // Is time to next obs large? | |
shutdownPi(); //Shutdown | |
piStatus = false; | |
} | |
} | |
if((getTimeTill(startTime) < TIME_BEFORE_BOOT) & (getTimeTill(EndofObs) > 0) & !piStatus){ //Does the pi need to run. | |
piStatus = true; | |
Serial.print("Starting Pi: "); | |
time_t now; | |
time(&now); | |
Serial.println(now); | |
digitalWrite(5,HIGH); | |
digitalWrite(BOOT_PI,LOW); | |
delay(1000); | |
digitalWrite(BOOT_PI,HIGH); | |
} | |
}else{ | |
if((getTimeTill(StartofObs) < 0) & (getTimeTill(EndofObs) < -(TIME_BEFORE_BOOT*2)) & piStatus){ //Shut down if not and obs over | |
shutdownPi(); | |
piStatus = false; | |
} | |
} | |
} | |
else { | |
Serial.print("Error on HTTP request. Error Code: "); | |
Serial.println(httpCode); | |
} | |
Serial.print("."); | |
http.end(); //Free the resources | |
digitalWrite(STATUS_LED,LOW); | |
delay(DELAY); | |
} | |
void shutdownPi(){ | |
Serial.print("Stopping Pi: "); | |
time_t now; | |
time(&now); | |
Serial.println(now); | |
piStatus = false; | |
digitalWrite(SHUTDOWN_PIN,LOW); | |
delay(1000); | |
digitalWrite(SHUTDOWN_PIN,HIGH); | |
digitalWrite(5,LOW); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment