Freitag, 29. März 2013

Connect and use a Sainsmart LCD12864 (ST7920) with a Arduino Mega 2560

Hello guest,

maybe here you find what you are looking for. A tutorial to wire a 12864 graphical LCD display with your Arduino Board and control it with the IDE afterwards.

My Equipment
  • Arduino Mega 2560 Board
  • Sainsmart J12864 LCD with serial/parallel backplane 
  • A few male/female wires

Whats the purpose of this blog?
  1. How to wire the 12864 LCD with your Arduino Board (Mega 2560 in my case)
  2. How to control the LCD


1. Wire LCD with board

Connect the pins of the serial interface of your LCD backplane with your arduino board as following described:

Take the backplane of the LCD and watch into the front of your serial/SPI interface like in the picture:



and connect the colored pins with the same colored jacks on the arduino board.


The result should look like:



Now your LCD is wired to your Arduino Mega board (should also work with other Arduino models).

2. Control the LCD

The easiest way for me was to use the u8glib driver from this site. How to install additional libraries in the Arduino IDE please have a look here.

After you've installed the u8glib libraries open your Arduino IDE and paste this code snippet to your sketch (do you see the jack numbers in the driver initialisation line?)


#include <U8glib.h>

U8GLIB_ST7920_128X64 u8g(13, 11, 12, U8G_PIN_NONE);        

void draw(void) {
  u8g.drawCircle(20, 20, 10);
  u8g.drawDisc(30, 30, 10);
  u8g.drawLine(60, 30, 90, 50);
  u8g.drawBox(50, 50, 20, 10);
  u8g.drawFrame(70, 10, 10, 30);
}

void setup(void) { }

void loop(void) {
  u8g.firstPage(); 
  do {
    draw();
  } while( u8g.nextPage() );
  // rebuild the picture after some delay
  delay(500);
}
After you compiled and uploaded your code to your Arduino you should see this:


Now your LCD is wired and the drivers are installed. Happy LCDing.

Feel free to leave a feedback. 

I've interested to control the LCD with the Sensor Shield V5 and the LCD-Serial interface. Unfortunately I had no luck to tell the u8glib driver how to adress it.

Best regards 

Ben


Donnerstag, 1. September 2011

Read MessPC temperature sensor and populate values in PRTG Network Monitor in Linux

Hello,

today I wan't to show a little tutorial howto read temperature sensors from the german company "MessPC" and populate the values in PRTG Network Monitor from Paessler.

Installing and configuring the sensors and the software

First you need the sensors. In my example I have the com port interface twin adapter (30203) with two attached sensors (30101) . 
Then you need the software for Linux which can be bought directly at the MessPC Online Shop. (http://www.messpc.de)

Attach the two sensors to the adapter and plug it into your com port interface. If you have more than one com port interface in your server or pc, remember which on you've plugged the adapter in. In most cases the interfaces are labeled.

You will get the software as a tarball. Untar it (tar -xvzf) in your favorite directory. You will find a file called "pcmeasure" which is the executable of the program and a file called "pcmeasure4linux.cfg". This is your config file.

In my example the ready configured file looks like

[global]
interval=5
samples=25000
loop=1000

[network]
networkserverport=4000

[ports]
com1.1=01
com1.2=01




The interesting part is "ports" label which describes your com port (in my case com port 1) and the sensor index. In my case two sensors with index 1 and 2 at com port 1. The twin adapter should have little labels an the cables to identify the sensors. The figures behind the equals sign describes the type of the sensor. "01" means temperature. The types are described in the installation textfile which should be also a content of the tarball.

If finished the config file start the program with
./pcmeasure

The program starts a deamon which stays in background and read the sensors every x seconds (interval). So there is no need to attach 
> /dev/null 2>&1 &

Now you should have a file called "pcmeasure.dat" in the same directory as your main programm. The file looks like

pcmeasure.com1.1.current=27.51
pcmeasure.com1.1.currenttimestamp=0
pcmeasure.com1.1.maxvalue=0.00
pcmeasure.com1.1.maxtimestamp=0
pcmeasure.com1.1.minvalue=0.00
pcmeasure.com1.1.mintimestamp=0

pcmeasure.com1.2.current=48.11
pcmeasure.com1.2.currenttimestamp=0
pcmeasure.com1.2.maxvalue=0.00
pcmeasure.com1.2.maxtimestamp=0
pcmeasure.com1.2.minvalue=0.00
pcmeasure.com1.2.mintimestamp=0

Here we are. The sensors are working. The labels 
 pcmeasure.com1.x.current=xx.xx
with the figure behind the equal sign is our current temperature. The file will be rewritten every x seconds (intervall) as long the pcmeasure deamon is running.

Installing the script for PRTG
The prerequisite is a working webserver. In my example I've decided for apache2. It's secure, fast, trusty and FREE ;-).


I've created a folder 
/var/www/messpc 
and configured it as virtual host.


With the following code snipet you can extract the temperature from every sensor of the pcmeasure.dat file and write it to a separate file into your webfolder for PRTG. The values has to be embedded into opening and closing brackets for PRTG.

#! /bin/sh

# $1 = searchstring in sourcefile i.e. pcmeasure.com1.1.current=
# $2 = sensorcount i.e 1

#echo $1
#echo $2

sourcefile=/root/pcmeasure/pcmeasure.dat
targetfile=/var/www/messpc/sensor$2.dat

searchstring=$1
startbracket="["
endbracket="]"

rawsensor1=`grep $searchstring $sourcefile`
#echo $rawsensor1
position=`expr index "$rawsensor1" =`
#echo $position
sensor1=${rawsensor1:position}
length=`expr length "$sensor1"`
#echo $length
z=$(($length - 1))
#echo $z
sensor1=${sensor1:0:z}
#echo $sensor1
echo $startbracket$sensor1$endbracket>$targetfile



Voilà!. After executing that script twice for every sensor you should have 2 files. Sensor1.dat and Sensor2.dat. This files should be viewable in your webbrowser.
I recommend to combine the extraction of the sensors into one basic script like


#! /bin/sh

echo Copy Sensor 1
./copy_sensor.sh pcmeasure.com1.1.current= 1
echo Copy Sensor 2
./copy_sensor.sh pcmeasure.com1.2.current= 2

Add the basic script into the crontab and execute it in every interval you want. For me every minute is more than enough.

Setup the sensors in PRTG



Start your PRTG Webgui and add a new device "MessPC" with the properties your server has.

Don't choose automatic sensor search. Select "manually" instead.





Add a sensor by clicking "Add sensor"


Sensorgroup is Webserver (HTTP,HTTPS) with type HTTP (Content)

The properties of the sensor1. Repeat it with sensor2. Name it however you want.


Here are the values in PRTG.




Thats it.
Now you should be able to controll the temperature with your PRTG Network Monitor. 

Hope this HowTo helped