# Access Temperature sensor data of 3D printer via Serial connection

I want to access the bed and nozzle temperature sensor data of a 3D printer via serial connection. Could someone please guide me in the right direction?

Thanks!

Edit: Response from printer:

Connecting to printer...
Connection response from printer:
6R7�P)��h>L�JO� � ��V�\���r��T �� SK�<ʪ� �>�Vw^$���|���R n�I, o�!����H>� mx�}M# �LYԣPh���^@ ��rp � TP�j�~1�� V6 6 � f� 6�k#���&�H".��k K2���ek�(��&��^K�k2����Ul�(� ���~N�ʈ*.�>Z��k#d$�����(� ��
��(� ��� v��
���
Temperature response from printer:
^CTraceback (most recent call last):
File "printer_files/serialaccess.py", line 15, in <module>
File "/usr/lib/python2.7/dist-packages/serial/serialposix.py", line 446, in read
KeyboardInterrupt


After the code asks the printer for temperature values, there is no output on the terminal anymore. I waited for a while and then killed it using control+c. It is clear that the printer is responding to the connection response but I am not sure why it is not returning the temperature values. Again, thanks a lot for your help, Demetris!

• This is veering into off-topic territory. This is not longer about the tools and applications of 3D printers, it's about programming and developing applications for one. Jul 25 '16 at 19:58

Assuming your printer accepts conventional G-code flavor, extruder and bed temperature can be retrieved by sending M105 through the serial port. The printer will respond with ok T:XXX.X B:XXX.X where T is the nozzle temperature and B is the bed temperature.

You can read more about the specific G-code in the RepRap wiki.

EDIT: I am editing the answer to include information for connecting to the printer using a simple python script.

The following script, first opens a serial connection to the printer. For Marlin firmware, when you first connect to the printer, it needs some time to initialize and respond back. That is why some delay is needed before reading the response. After printing the response, the script sends the M105 command, waits for 100ms, then reads the response of the serial buffer and prints it on the screen.

Note /dev/ttyUSB0 is the serial port name and in your case it may be different. Also 250000 is the baud rate of the connection; 250000 is the default value of my printer so you need to replace this with the baud rate that your printer uses.

import serial
import time

ser_printer = serial.Serial('/dev/ttyUSB0', 250000)
print "Connecting to printer..."
time.sleep(30)  # Allow time for response
buffer_bytes = ser_printer.inWaiting()
print "Connection response from printer:"
print response
ser_printer.write('M105\n')
time.sleep(0.1)  # Allow time for response
print "Temperature response from printer:"
`