1+ using System ;
2+ using System . Net ;
3+ using System . Text . Json ;
4+ using System . IO ;
5+
6+ using Microsoft . Extensions . Configuration ;
7+
8+ class Program
9+ {
10+ static void Main ( )
11+ {
12+ var config = new ConfigurationBuilder ( )
13+ . AddUserSecrets < Program > ( )
14+ . Build ( ) ;
15+
16+ var apiKey = config [ "OpenWeatherMap:ApiKey" ] ;
17+ var thingSpeakApiKey = config [ "ThingSpeak:ApiKey" ] ; // ThingSpeak Write API Key
18+ string city = "Freiberg" ;
19+
20+ WebClient client = new WebClient ( ) ;
21+ string city_url = $ "http://api.openweathermap.org/geo/1.0/direct?q={ city } &limit=1&appid={ apiKey } ";
22+ string city_json = client . DownloadString ( city_url ) ;
23+ Console . WriteLine ( city_json ) ;
24+
25+ JsonDocument cityDoc = JsonDocument . Parse ( city_json ) ;
26+ JsonElement cityRoot = cityDoc . RootElement ;
27+ double lat = cityRoot [ 0 ] . GetProperty ( "lat" ) . GetDouble ( ) ;
28+ double lon = cityRoot [ 0 ] . GetProperty ( "lon" ) . GetDouble ( ) ;
29+
30+ string weather_url = $ "http://api.openweathermap.org/data/2.5/weather?lat={ lat } &lon={ lon } &appid={ apiKey } &units=metric";
31+ Console . WriteLine ( weather_url ) ;
32+
33+ string weather_json = client . DownloadString ( weather_url ) ;
34+ Console . WriteLine ( weather_json ) ;
35+
36+ JsonDocument weatherDoc = JsonDocument . Parse ( weather_json ) ;
37+ JsonElement weatherRoot = weatherDoc . RootElement ;
38+ string wetter = weatherRoot . GetProperty ( "weather" ) [ 0 ] . GetProperty ( "description" ) . GetString ( ) ;
39+ double temp = weatherRoot . GetProperty ( "main" ) . GetProperty ( "temp" ) . GetDouble ( ) ;
40+ double wind = weatherRoot . GetProperty ( "wind" ) . GetProperty ( "speed" ) . GetDouble ( ) ;
41+
42+ Console . WriteLine ( $ "\n Wetter in { city } :") ;
43+ Console . WriteLine ( $ "Beschreibung: { wetter } ") ;
44+ Console . WriteLine ( $ "Temperatur: { temp } °C") ;
45+ Console . WriteLine ( $ "Windgeschwindigkeit: { wind } m/s") ;
46+
47+ // CSV-Datei erstellen/erweitern
48+ string csvFilePath = "wetterdaten.csv" ;
49+ string csvLine = $ "{ DateTime . Now : yyyy-MM-dd HH:mm:ss} ,{ city } ,{ wetter } ,{ temp } ,{ wind } ";
50+
51+ // Prüfen ob CSV-Datei bereits existiert
52+ if ( ! File . Exists ( csvFilePath ) )
53+ {
54+ // Header schreiben falls Datei nicht existiert
55+ string header = "Datum,Stadt,Beschreibung,Temperatur_C,Windgeschwindigkeit_ms" ;
56+ File . WriteAllText ( csvFilePath , header + Environment . NewLine ) ;
57+ }
58+
59+ // Wetterdaten anhängen
60+ File . AppendAllText ( csvFilePath , csvLine + Environment . NewLine ) ;
61+
62+ Console . WriteLine ( $ "\n Wetterdaten wurden in '{ csvFilePath } ' gespeichert.") ;
63+
64+ // ThingSpeak Daten senden
65+ if ( ! string . IsNullOrEmpty ( thingSpeakApiKey ) )
66+ {
67+ string thingSpeakUrl = $ "https://api.thingspeak.com/update?api_key={ thingSpeakApiKey } &field1={ temp } &field2={ wind } ";
68+
69+ try
70+ {
71+ string thingSpeakResponse = client . DownloadString ( thingSpeakUrl ) ;
72+ Console . WriteLine ( $ "ThingSpeak Response: { thingSpeakResponse } ") ;
73+
74+ if ( int . TryParse ( thingSpeakResponse , out int entryId ) && entryId > 0 )
75+ {
76+ Console . WriteLine ( $ "Daten erfolgreich an ThingSpeak gesendet! Entry ID: { entryId } ") ;
77+ }
78+ else
79+ {
80+ Console . WriteLine ( "Fehler beim Senden an ThingSpeak." ) ;
81+ }
82+ }
83+ catch ( Exception ex )
84+ {
85+ Console . WriteLine ( $ "Fehler beim Senden an ThingSpeak: { ex . Message } ") ;
86+ }
87+ }
88+ else
89+ {
90+ Console . WriteLine ( "ThingSpeak API Key nicht gefunden. Daten werden nicht gesendet." ) ;
91+ }
92+ }
93+ }
0 commit comments