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

Hallo zusammen,

 

ich wollte eigentlich fix eine Tabelle mit Werten über javascript zeichnen und die Werte vorher von unterschiedlichen TF Bricklets einlesen. Dazu habe ich unteranderem das html Beispiel vom temperatur IR genommen und das Ende vom Code noch ein paar Zeilen eingefügt, damit eine Tabelle gezeichnet wird:

 

<script src="./Tinkerforge.js" type='text/javascript'></script>
	<script type='text/javascript'>
		var wert = new Array();
		var ipcon;
		var textArea = document.getElementById("text");
		function info()
		{
			textArea.value = "";
			var HOST = "192.168.178.150";
			var PORT = "4280";
			var UIDT = "9vY";
			var UIDA = "jzp";
			var UIDM = "kvP";
			var SECRET = "DasGeheimnis22";
			if (ipcon !== undefined)
			{
				ipcon.disconnect();
			}
			ipcon = new Tinkerforge.IPConnection(); // Create IP connection
			var tir = new Tinkerforge.BrickletTemperatureIR(UIDT, ipcon); // Create device object
			var al = new Tinkerforge.BrickletAmbientLight(UIDA, ipcon); // Create device object
			var m = new Tinkerforge.BrickletMoisture(UIDM, ipcon);

			ipcon.connect(HOST, PORT, function(error)
			{
				textArea.value += 'Error: ' + error + '\n';
			}); // Connect to brickd
			// Don't use device before ipcon is connected

			// Register Connected Callback
			ipcon.on(Tinkerforge.IPConnection.CALLBACK_CONNECTED,
			// Authenticate each time the connection got (re-)established
			function(connectReason)
			{
				ipcon.authenticate(SECRET, function()
				{
					tir.getObjectTemperature(function(obj)
					{

						textArea.value += 'Object Temperature: ' + obj / 10 + ' °C\n';
						wert[0] = obj;
					}, function(error)
					{
						textArea.value += 'Error: ' + error + '\n';
					});
					tir.getAmbientTemperature(function(amb)
					{
						textArea.value += 'Ambient Temperature: ' + amb / 10 + ' °C\n';
					}, function(error)
					{
						textArea.value += 'Error: ' + error + '\n';
					});

					al.getIlluminance(function(illuminance)
					{
						textArea.value += 'Illuminance: ' + illuminance / 10 + ' Lux\n';
					}, function(error)
					{
						textArea.value += 'Error: ' + error + '\n';
					});
					m.getMoistureValue(function(moisture)
					{
						textArea.value += 'Moisture: ' + moisture + '\n';
					}, function(error)
					{
						textArea.value += 'Error: ' + error + '\n';
					});

				}, function(error)
				{
					textArea.value += 'Could not authenticate: ' + error + '\n';
				});

			});

			document.write("<table>");
			document.write("<tr>");
			document.write("<td class='fc'>Object Temperature:</td>");
			document.write("<td class='mc'>" + wert[0] + "</td>");
			document.write("<td class='lc'>°C</td>");
			document.write("</tr>");
			document.write("<tr>");
			document.write("<td class='fc'>Ambient Temperature:</td>");
			document.write("<td class='mc'>" + wert[1] + "</td>");
			document.write("<td class='lc'>°C</td>");
			document.write("</tr>");
			document.write("<tr>");
			document.write("<td class='fc'>Illuminance:</td>");
			document.write("<td class='mc'>" + wert[2] + "</td>");
			document.write("<td class='lc'>Lux</td>");
			document.write("</tr>");
			document.write("<tr>");
			document.write("<td class='fc'>Moisture:</td>");
			document.write("<td class='mc'>" + wert[3] + "</td>");
			document.write("<td class='lc'>[-]</td>");
			document.write("</tr>");
			document.write("</table>");
		}
	</script>
	<p>
		<script type='text/javascript'>
			info();
		</script>
	</p>

 

Der Code steht irgendwo im HTML Dokument und funktioniert auch soweit, also wie im Beispiel werden alle Werte in ein Text Feld eingetragen, aber leider nicht in meine gezeichnete Tabelle.

 

Am Anfang deklariere ich eine globale Variable "wert" als Array.

 

und beispielsweiße hier im Code:

 

tir.getObjectTemperature(function(obj)
	{

		textArea.value += 'Object Temperature: ' + obj / 10 + ' °C\n';
		wert[0] = obj;
	}, function(error)...

 

möchte ich den Wert von "obj" in das Array "wert" speichern:

 

wert[0] = obj;

 

nur leider gibt später in der Tabelle wo eigentlich dann der richtige Wert stehen solle ein "undefined".

 

Also wurde der Wert von "obj" nicht in wert[0] eingelesen. Bzw. nicht behalten. innerhalb von tir.getObjectTemperature kann ich mit dem Wert arbeiten, aber er wird irgendwie später dann nicht in der Tabelle angezeigt...

 

Nun meine Frage, wie mache ich das?

 

Mit JavaScript kenne ich mich nicht so gut aus, bin aber schon mal so weit gekommen...

Geschrieben
  • Autor

Oder um es einfacher zu formulieren:

Wie kann ich in JS z.B. den Wert vom illuminance in eine globale Variable speichern? So dass ich den Wert im gesamten JS benutzten kann.

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.