### B4J Log Temperature

This is an example of reading serial data from the Arduino.
The Arduino is connected to a PC port COM4.
Arduino Sketch
/*
* IoT Arduino Experiment - Read Data from Temperature Sensor TMP36 and log to the serial port.
* The TMP36 is connected (looking at the flat side of the sensore from left to right):
* Pin 1 (left) +5v (or 3.3v)
* Pin 2 (middle) Analog 0 (A0)
* Pin 3 (right) Ground
* Taken from:
* http://www.learningaboutelectronics.com/Articles/TMP36-temperature-sensor-circuit.php
* http://www.analog.com/media/en/technical-documentation/data-sheets/TMP35_36_37.pdf
* Tested with: Arduino UNO.
* Changed: 2016.04.05 rwbLinn
*/
// Define arduino pin A0 which is connected to the output pin of the TMP36 temperature sensor
int outputpin = 0;
// Set the ground pin to LOW and the input voltage pin to HIGH
void setup()
{
Serial.begin(9600);
}
// Main loop
void loop()
{
// Get the raw value (between 0 and 1023) divided by 205.
// 205 because a span of 1024 values occupies 5V, or 1024/5= 205 values per volt.
// Read the analog data from A0
int rawvoltage = analogRead(outputpin);
// In case want to know the raw voltage from the sensor
// Serial.print("Vout = ");
// Serial.println(rawvoltage);
// Convert rawvoltage to celsius and fahrenheit
float volts = rawvoltage/205.0;
// Calculate Celsius
float celsiustemp = 100.0 * volts - 50;
// And Fahrenheit
float fahrenheittemp = celsiustemp * 9.0/5.0 + 32.0;
// Send the result to the serial line (can be displayed in the Arduino IDE Serial Monitor, but also read by f.e. B4J
Serial.print(celsiustemp);
Serial.print(" Celsius, ");
Serial.print(fahrenheittemp);
Serial.println(" Fahrenheit");
// Wait for 5 seconds to measure again
delay(5000);
}
B4J Code
'Project: IoT Arduino Experiment - Read Data from Temperature Sensor TMP36, log to the serial port and read by B4J application
'Non-UI application (console / server application)
'Libraries used: jSerial, jRandomAccessFile
#Region Project Attributes
#CommandLineArgs:
#MergeLibraries: True
#End Region
`Sub Process_Globals
Private sp As Serial 'Library jSerial
Private astream As AsyncStreams 'Library jRanomAccessFile
End Sub`
`Sub AppStart (Args() As String)
Log($"${DateTime.Time(DateTime.Now)} Experiment started"$)
'Init the serial port and the asyncstream
If SerialPortInit Then
astream.Initialize(sp.GetInputStream, sp.GetOutputStream, "astream")
End If
'DO NOT FORGET
StartMessageLoop
End Sub`
'Init the serial port
`Sub SerialPortInit As Boolean
Dim Result As Boolean = False
Dim BaudRate As Int = 9600
Dim DataBits As Int = 8
Dim StopBits As Int = 1
Dim Parity As Int = 0
Try
sp.Initialize("")
sp.Open("COM4")
sp.SetParams(BaudRate , DataBits , StopBits , Parity )
Result = True
Catch
Log($"Error initializing port COM4: ${LastException}"$)
End Try
Return Result
End Sub`
`Sub MainForm_Closed
Try
If astream.IsInitialized Then astream.Close
sp.Close
Catch
Log($"Error closing the port: ${LastException}"$)
End Try
Log($"${DateTime.Time(DateTime.Now)} Experiment stopped"$)
End Sub`
`Sub AStream_Error
Log($"Error reading data from port COM4: ${LastException}"$)
astream.Close
End Sub`
'Handle newdata from the arduino coming in via the serial line.
'Parameter: Buffer as byte. The buffer needs to be converted to a string
`Sub AStream_NewData (Buffer() As Byte)
Dim S As String = BytesToString(Buffer, 0, Buffer.Length, "UTF8")
Log($"${DateTime.Time(DateTime.Now)}: ${S}"$)
End Sub`
Output
13:31:18 Experiment started
13:31:20: 21.22 Celsius, 70.20 Fahrenheit
13:31:25: 20.73 Celsius, 69.32 Fahrenheit

Changed: 2018-06-07 09:08, Category: Arduino, File: T6C161M1200.htm, ID: 1200