Jump to content
View in the app

A better way to browse. Learn more.

Tinkerunity

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

Featured Replies

Geschrieben

Moin,

ich hab mir die Bricks bestellt und wollte mit denen Java lernen.

Ich bekomm es nur nicht hin den Listener einzubauen in meine Klasse, hat da vielleicht jemand Ahnung von?

Ich habe folgenden Code:

package java_lernen;

/**
* @(#)ShutdownHook.java
*
* Spielereien mit Bricks und Schliessen der Verbindung am Programmende
*
* @author CHaller
* @version 1.00 21/04/2012
*/

import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletAmbientLight;
import com.tinkerforge.BrickletIO4;
import com.tinkerforge.BrickletTemperature;
import Prog1Tools.IOTools.*;
import java.lang.Math.*;
import java.util.*;
import java.text.*;

public class Bricks01 {
private static final String host = "localhost";
private static final int port = 4223;
private static long Licht = 0;
private static long Temperatur = 0;
private static long LichtZwischen = 0;
private static long TemperaturZwischen = 0;
private IPConnection ipcon;
private BrickletAmbientLight al;
private BrickletIO4 io4;
private BrickletTemperature temp;

/* Methoden */


private void Bricks01 (){
    try {
	bricksStart();
	bricksGrundstellung();
	}
    catch (Exception e) {
	System.out.println(e.getMessage());
	}
    
}

private boolean bricksStart() throws Exception {
    //verbindung aufbauen
    ipcon = new IPConnection(host, port); // Can throw IOException
    al = new BrickletAmbientLight("7e7"); // Create device object
        ipcon.addDevice(al); // Can throw IPConnection.TimeoutException
    io4 = new BrickletIO4("7aC"); // Create device object
        ipcon.addDevice(io4); // Can throw IPConnection.TimeoutException
    temp = new BrickletTemperature("6EN"); // Create device object
        ipcon.addDevice(temp); // Can throw IPConnection.TimeoutException
    return true;
}

//verbindung beenden
private void bricksStop() throws Exception {
    ipcon.destroy();
}

//bricks "parametrieren"
private void bricksGrundstellung() throws Exception {
    temp.setTemperatureCallbackPeriod(1000); // Note: The temperature callback is only called every second if the temperature has changed since the last call!
    temp.addListener(new BrickletTemperature.TemperatureListener() { public void temperature(short temperature) { Temperatur = (long) temperature/100.0; } });
}

//Bildschirmausgabe
private void Ausgabe() {
    long date_zwischenzeit;
    Date date_zwischen = new Date();
    date_zwischenzeit = date_zwischen.getTime();
    SimpleDateFormat Uhrzeit = new SimpleDateFormat("HH:mm:ss");
    
    System.out.print("Zeit: " + Uhrzeit.format(date_zwischen));
    System.out.print(" Temperatur: " + Temperatur + " Grad");
    System.out.print(" Helligkeit: " + Licht + " Lux");
    System.out.print("\n");
}

/* start_methode */
public static void main(String[] args) {
	    
	Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
		@Override
		public void run() { 
		    System.out.println("Ende");
			}}));

	try {
	    Bricks01 brickTest = new Bricks01();
	    int i = 0;
	    do {
		brickTest.Ausgabe();
		i++;
		}
	    while (i<=1000);
	    
	    System.exit(0);
	    }
	catch (Exception e) {
	    System.out.println(e.getMessage());
	    }
	}
}

Ich hatte mir nun vorgestellt das der listener im Hintergrund der Klasse den Wert von Licht & Temperatur aktualisiert, und ich mit Ausgabe() diesen abfragen kann. Doch irgendwie klappt das nicht.

Kann mir da jemand helfen das zum laufen zu bekommen?

Geschrieben

Und was klappt da nicht ? Beschreibe mal den "Fehler".

----------

1000 Durchläufe sind auf einem modernen Rechner nicht sehr viel Zeit.... Du solltest da auf Zeit gehen und nicht auf einen Schleifenzähler, der wird wenn Du Pech hast vom Hotspot weg optimiert.

Geschrieben
  • Autor

Moin, danke für deinen Vorschlag.

Ich hab die do-while Schleife inzwischen dahingehend verändert das sie eine bestimmte Zeit wiederholt wird.

Doch auch jetzt noch ändert sich die Temperatur nicht.

 

Um ganz sicher zu sein hab ich dem Listener noch folgenden Befehl hinzugefügt:

System.out.println(temperature/100.0);

Aber der Listener wird anscheinend kein einziges mal aktiv. Hab ich ihn falsch aufgerufen? Hab ich was übersehen? Ich hoffe mir kann da jemand helfen.

 

Hier nochmal mein kompletter Code:

package java_lernen;

/**
* @(#)ShutdownHook.java
*
* Spielereien mit Bricks und Schliessen der Verbindung am Programmende
*
* @author CHaller
* @version 1.00 21/04/2012
*/

import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletAmbientLight;
import com.tinkerforge.BrickletIO4;
import com.tinkerforge.BrickletTemperature;
import Prog1Tools.IOTools.*;
import java.lang.Math.*;
import java.util.*;
import java.text.*;

public class Bricks01 {
private static final String host = "localhost";
private static final int port = 4223;
private static long Licht = 0;
private static long Temperatur = 0;
private static long LichtZwischen = 0;
private static long TemperaturZwischen = 0;
private IPConnection ipcon;
private BrickletAmbientLight al;
private BrickletIO4 io4;
private BrickletTemperature temp;

/* Methoden */


private void Bricks01 (){
    try {
	bricksStart();
	bricksGrundstellung();
	}
    catch (Exception e) {
	System.out.println(e.getMessage());
	}
    
}

private boolean bricksStart() throws Exception {
    //verbindung aufbauen
    ipcon = new IPConnection(host, port); // Can throw IOException
    al = new BrickletAmbientLight("7e7"); // Create device object
        ipcon.addDevice(al); // Can throw IPConnection.TimeoutException
    io4 = new BrickletIO4("7aC"); // Create device object
        ipcon.addDevice(io4); // Can throw IPConnection.TimeoutException
    temp = new BrickletTemperature("6EN"); // Create device object
        ipcon.addDevice(temp); // Can throw IPConnection.TimeoutException
    return true;
}

//verbindung beenden
private void bricksStop() throws Exception {
    ipcon.destroy();
}

//bricks "parametrieren"
private void bricksGrundstellung() throws Exception {
    temp.setTemperatureCallbackPeriod(1000); // Note: The temperature callback is only called every second if the temperature has changed since the last call!
    temp.addListener(new BrickletTemperature.TemperatureListener() { public void temperature(short temperature) { Temperatur = (long)( temperature/100.0); System.out.println(temperature/100.0); } });
}

//Bildschirmausgabe
private void Ausgabe() {
    long date_zwischenzeit;
    Date date_zwischen = new Date();
    date_zwischenzeit = date_zwischen.getTime();
    SimpleDateFormat Uhrzeit = new SimpleDateFormat("HH:mm:ss");
    
    System.out.print("Zeit: " + Uhrzeit.format(date_zwischen));
    System.out.print(" Temperatur: " + Temperatur + " Grad");
    System.out.print(" Helligkeit: " + Licht + " Lux");
    System.out.print("\n");
}

/* start_methode */
public static void main(String[] args) {
	    
	Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
		@Override
		public void run() { 
		    System.out.println("Ende");
			}}));

	try {
	    Bricks01 brickTest = new Bricks01();

	    long date_zwischenzeit;
                    Date date_start = new Date();
                    long dateStartMilisekunden = date_start.getTime();
                    long dateEndMilisekunden = dateStartMilisekunden + (20 * 60 * 1000);
	    
	    do {
		brickTest.Ausgabe();

                        Date date_zwischen = new Date();
                        date_zwischenzeit = date_zwischen.getTime();
		}
	    while (date_zwischenzeit <= dateEndMilisekunden);
	    
	    System.exit(0);
	    }
	catch (Exception e) {
	    System.out.println(e.getMessage());
	    }
	}
}

 

EDIT:

OK, mein Fehler, Konstruktoren sind keine Methoden :D

Wenige Zeichen machen doch einen großen Unterschied.

Hier der Code mit dem Richtig angewendeten Konstruktor:

package java_lernen;

/**
* @(#)ShutdownHook.java
*
* Spielereien mit Bricks und Schliessen der Verbindung am Programmende
*
* @author CHaller
* @version 1.00 21/04/2012
*/

import com.tinkerforge.IPConnection;
import com.tinkerforge.BrickletAmbientLight;
import com.tinkerforge.BrickletIO4;
import com.tinkerforge.BrickletTemperature;
import Prog1Tools.IOTools.*;
import java.lang.Math.*;
import java.util.*;
import java.text.*;

public class Bricks01 {
private static final String host = "localhost";
private static final int port = 4223;
private static long Licht = 0;
private static long Temperatur = 0;
private static long LichtZwischen = 0;
private static long TemperaturZwischen = 0;
private IPConnection ipcon;
private BrickletAmbientLight al;
private BrickletIO4 io4;
private BrickletTemperature temp;

/* Methoden */


Bricks01(){
    try {
	bricksStart();
	bricksGrundstellung();
	}
    catch (Exception e) {
	System.out.println(e.getMessage());
	}
    
}

private boolean bricksStart() throws Exception {
    //verbindung aufbauen
    ipcon = new IPConnection(host, port); // Can throw IOException
    al = new BrickletAmbientLight("7e7"); // Create device object
        ipcon.addDevice(al); // Can throw IPConnection.TimeoutException
    io4 = new BrickletIO4("7aC"); // Create device object
        ipcon.addDevice(io4); // Can throw IPConnection.TimeoutException
    temp = new BrickletTemperature("6EN"); // Create device object
        ipcon.addDevice(temp); // Can throw IPConnection.TimeoutException
    return true;
}

//verbindung beenden
private void bricksStop() throws Exception {
    ipcon.destroy();
}

//bricks "parametrieren"
private void bricksGrundstellung() throws Exception {
    System.out.println("Grundstellung");
    temp.setTemperatureCallbackPeriod(1000); // Note: The temperature callback is only called every second if the temperature has changed since the last call!
    temp.addListener(new BrickletTemperature.TemperatureListener() { public void temperature(short temperature) { Temperatur = (long)( temperature/100.0); System.out.println(temperature/100.0); } });
    
    al.setIlluminanceCallbackPeriod(1000);
    al.addListener(new BrickletAmbientLight.IlluminanceListener() { public void illuminance(int illuminance) { Licht = (long)(illuminance/10.0); System.out.println(illuminance/10.0); } });
}

//Bildschirmausgabe
private void Ausgabe() {
    long date_zwischenzeit;
    Date date_zwischen = new Date();
    date_zwischenzeit = date_zwischen.getTime();
    SimpleDateFormat Uhrzeit = new SimpleDateFormat("HH:mm:ss");
    
    System.out.print("Zeit: " + Uhrzeit.format(date_zwischen));
    System.out.print(" Temperatur: " + Temperatur + " Grad");
    System.out.print(" Helligkeit: " + Licht + " Lux");
    System.out.print("\n");
}

/* start_methode */
public static void main(String[] args) {
	    
	Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() { 
		@Override
		public void run() { 
		    System.out.println("Ende");
			}}));

	try {
	    Bricks01 brickTest = new Bricks01();

	    long date_zwischenzeit;
                    Date date_start = new Date();
                    long dateStartMilisekunden = date_start.getTime();
                    long dateEndMilisekunden = dateStartMilisekunden + (60 * 1000);
	    
	    do {
		brickTest.Ausgabe();

                        Date date_zwischen = new Date();
                        date_zwischenzeit = date_zwischen.getTime();
		}
	    while (date_zwischenzeit <= dateEndMilisekunden);
	    
	    System.exit(0);
	    }
	catch (Exception e) {
	    System.out.println(e.getMessage());
	    }
	}
}

Geschrieben

Oh, das hab ich auch nciht gesehen. Sag mal, hat Java wenigstens eine Warnung oder sowas geworfen? Hätte nicht erwartet, dass man eine Methode so nennen darf wie den Klassen-konstruktor.

Geschrieben
  • Autor

Moin,

ne, keine Fehlermeldung oder so. Wurde halt einfach nicht gestartet mein "Konstruktor" :D

Geschrieben

Doch ne Warnung kommt ( wenigstens bei mir im Eclipse )

Description Resource Path Location Type

This method has a constructor name Tinkerforge.java /Tinkerforge/src/li/xx/tinkerforge/yyy/client line 63 Java Problem

 

Geschrieben

Naja manche Compiler übergehen das einfach. Für die ist ein Konstruktor nicht zwangsläufig notwendig.

Daher generiert das Programm keinen Fehler. Nur es passiert halt auch nichts ;)

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Gast
Reply to this topic...

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.