Components and supplies
Push Button
Arduino uno wifi
16x2 LCD display with I²C interface
10kOhm potentiometer
Apps and platforms
Arduino IDE 2.0 (beta)
Project description
Code
ISS Tracker
cpp
Here is the full code for the project
1#include <ESP8266WiFi.h> 2#include <LiquidCrystal.h> 3#include <ArduinoJson.h> 4#include <ESP8266HTTPClient.h> 5#include <WiFiClient.h> 6#include <ESP8266HTTPClient.h> 7HTTPClient http; 8 9const char* ssid = "Your name"; 10const char* password = "Your code"; 11 12const int rs = D5; 13const int en = D3; 14const int d4 = D2; 15const int d5 = D1; 16const int d6 = D0; 17const int d7 = D4; 18LiquidCrystal lcd(rs, en, d4, d5, d6, d7); 19 20bool isConnected = false; 21 22float targetLat = Your cordinates; 23float targetLon = Your cordinates; 24float distanceThreshold = 10; 25 26unsigned long lastDetectionTime = 0; 27unsigned long notificationInterval = 30000; 28unsigned long displayAstronautsTime = 7000; 29 30float lat = 0.0; 31float lon = 0.0; 32 33bool isSwitchOn = false; 34bool wasSwitchOn = false; 35bool displayAstronauts = false; 36unsigned long displayStartTime = 0; 37 38void setup() { 39 Serial.begin(115200); 40 lcd.begin(16, 2); 41 lcd.setCursor(2, 0); 42 lcd.print("ISS tracker"); 43 connectToWiFi(); 44 pinMode(D6, INPUT_PULLUP); 45} 46 47void loop() { 48 if (WiFi.status() == WL_CONNECTED) { 49 if (!isConnected) { 50 lcd.clear(); 51 lcd.print("Connected!"); 52 isConnected = true; 53 } 54 getISSPosition(); 55 checkIfNearTarget(); 56 handleSwitch(); 57 handleDisplayAstronauts(); 58 } else { 59 if (isConnected) { 60 lcd.clear(); 61 lcd.print("Connection Lost"); 62 isConnected = false; 63 } 64 } 65 delay(1000); 66} 67 68void connectToWiFi() { 69 WiFi.begin(ssid, password); 70 while (WiFi.status() != WL_CONNECTED) { 71 delay(1000); 72 Serial.print("."); 73 } 74 Serial.println(); 75 Serial.println("Connected to WiFi"); 76} 77 78void getISSPosition() { 79 if (!isSwitchOn) { // Ενημερώνουμε τις συντεταγμένες μόνο όταν το switch είναι ανενεργό 80 WiFiClient client; 81 http.begin(client, "http://api.open-notify.org/iss-now.json"); 82 int httpCode = http.GET(); 83 if (httpCode == HTTP_CODE_OK) { 84 String payload = http.getString(); 85 deserializeISSPayload(payload); 86 } else { 87 Serial.println("HTTP request error"); 88 } 89 http.end(); 90 } 91} 92 93void deserializeISSPayload(String payload) { 94 StaticJsonDocument<256> doc; 95 DeserializationError error = deserializeJson(doc, payload); 96 if (error) { 97 Serial.println("JSON deserialization error"); 98 return; 99 } 100 lat = doc["iss_position"]["latitude"]; 101 lon = doc["iss_position"]["longitude"]; 102 lcd.clear(); 103 lcd.setCursor(0, 0); 104 lcd.print("Lat: " + String(lat, 6)); 105 lcd.setCursor(0, 1); 106 lcd.print("Lon: " + String(lon, 6)); 107 lcd.setCursor(11, 1); 108 lcd.print(" "); 109 lcd.setCursor(11, 0); 110 lcd.print(" "); 111} 112 113void checkIfNearTarget() { 114 float latDiff = abs(targetLat - lat); 115 float lonDiff = abs(targetLon - lon); 116 117 if (latDiff <= distanceThreshold && lonDiff <= distanceThreshold) { 118 if (millis() - lastDetectionTime > notificationInterval) { 119 lcd.setCursor(0, 0); 120 lcd.print(" ISS detect "); 121 lcd.setCursor(0, 1); 122 lcd.print(" "); 123 lastDetectionTime = millis(); 124 delay(3000); 125 } 126 } else { 127 lastDetectionTime = 0; 128 } 129} 130 131void handleSwitch() { 132 wasSwitchOn = isSwitchOn; 133 isSwitchOn = digitalRead(D6) == LOW; 134 135 if (isSwitchOn && !wasSwitchOn) { 136 displayAstronauts = true; 137 displayStartTime = millis(); 138 } 139} 140 141void handleDisplayAstronauts() { 142 if (displayAstronauts && (millis() - displayStartTime <= displayAstronautsTime)) { 143 displayAstronautCount(); 144 } else { 145 displayAstronauts = false; 146 } 147} 148 149void displayAstronautCount() { 150 WiFiClient client; 151 http.begin(client, "http://api.open-notify.org/astros.json"); 152 int httpCode = http.GET(); 153 if (httpCode == HTTP_CODE_OK) { 154 String payload = http.getString(); 155 DynamicJsonDocument doc(1024); 156 DeserializationError error = deserializeJson(doc, payload); 157 if (!error) { 158 int astronautCount = doc["number"]; 159 lcd.clear(); 160 lcd.setCursor(0, 0); 161 lcd.print("Astronauts: " + String(astronautCount)); 162 lcd.setCursor(0, 1); 163 lcd.print("Speed: 7.706m/s"); 164 } 165 } 166 http.end(); 167}
Comments
Only logged in users can leave comments
petros_mpla
0 Followers
•0 Projects
0