Author Archive

Beispiel für eine MySQL Funktion

Ich bereite momentan die Spendenquittungen der Dingfabrik mit Python, Jinja2, MySQL und LaTeX auf. Um den SQL Code sauber zu halten, lagere ich einiges aus dem Python-Code in MySQL Funktionen aus.

Hier ein einfaches Beispiel, das basierend auf Parametern für Mitglieds-ID und Namenstyp entweder der Vornamen, Nachnamen oder kompletten Namen eines Mitglieds zurückgibt.

DROP FUNCTION IF EXISTS fs_getname;
DELIMITER $$
CREATE FUNCTION fs_getname(nummer INT, typ CHAR)
 RETURNS VARCHAR(100)
 NOT DETERMINISTIC
 BEGIN
	IF typ = 'c' THEN
		RETURN (SELECT TRIM(CONCAT(COALESCE(Vorname,"")," ",COALESCE(Name," "))) FROM Stammdaten WHERE  ID = nummer);
	ELSEIF typ = 'f' THEN
		RETURN (SELECT COALESCE(Vorname,"") FROM Stammdaten WHERE  ID = nummer);
	ELSEIF typ = 'l' THEN
		RETURN (SELECT COALESCE(Name,"") FROM Stammdaten WHERE  ID = nummer);		
	END IF;
END$$
DELIMITER ;

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Template for paper-based time planners

Here’s an old template I created for my time-planner. I’d probably do some things a bit different today but nevertheless it might be useful for somebody.

\documentclass[a4paper,12pt]{article}
\usepackage[latin1]{inputenc}
\usepackage{graphicx}
\setlength{\headsep}{-3 cm}
\setlength{\footskip}{1 cm}
\setlength{\textheight}{15 cm}
\setlength{\textwidth}{10 cm}
\begin{document}
\pagestyle{empty}
\setlength{\unitlength}{10mm}


\begin{center}
\begin{picture}(10,15)(0,10)
\put(0,0){\line(1,0){14.7}} 
\put(0,21){\line(1,0){14.7}}
\put(0,0){\line(0,1){21}} 
\put(14.7,0){\line(0,1){21}} 
\put(2.4,0.9){\line(0,1){17.5}} 
\put(2.85,0.9){\line(0,1){17.5}} 
\put(14.4,0.9){\line(0,1){17.5}} 
\put(13.91,0.9){\line(0,1){17.5}} 
\put(1.3,0.9){\line(1,0){6.4}} 
\put(7.9,0.9){\line(1,0){6.5}} 
\put(1.3,18.4){\line(1,0){6.4}} 
\put(7.9,18.4){\line(1,0){6.5}} 
\put(7.7,0.9){\line(0,1){17.5}} 
\put(7.9,0.9){\line(0,1){17.5}} 
                
% Beginn der inneren Linien                
                
\put(1.3,1.8){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,1.8){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,3.15){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,3.15){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,4.5){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,4.5){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,5.85){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,5.85){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,7.2){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,7.2){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,8.55){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,8.55){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,9.9){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,9.9){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,11.25){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,11.25){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,12.6){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,12.6){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,13.95){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,13.95){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,15.3){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,15.3){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,16.65){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,16.65){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,17.5){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,17.5){\line(1,0){6.5}} %untere Innenlinie 2
                
\put(1.3,18){\line(1,0){6.4}} %untere Innenlinie 1
\put(7.9,18){\line(1,0){6.5}} %untere Innenlinie 2
                
% klitzekleine Linien                
                
\put(2.4,1.35){\line(1,0){5.3}}                 
\put(7.9,1.35){\line(1,0){6.5}}                 
                
\put(2.4,1.8){\line(1,0){5.3}}                 
\put(7.9,1.8){\line(1,0){6.5}}                 
                
\put(2.4,2.25){\line(1,0){5.3}}                 
\put(7.9,2.25){\line(1,0){6.5}}                 
                
\put(2.4,2.7){\line(1,0){5.3}}                 
\put(7.9,2.7){\line(1,0){6.5}}                 
                
\put(2.4,3.15){\line(1,0){5.3}}                 
\put(7.9,3.15){\line(1,0){6.5}}                 
                
\put(2.4,3.6){\line(1,0){5.3}}                 
\put(7.9,3.6){\line(1,0){6.5}}                 
                
\put(2.4,4.05){\line(1,0){5.3}}                 
\put(7.9,4.05){\line(1,0){6.5}}                 
                
\put(2.4,4.5){\line(1,0){5.3}}                 
\put(7.9,4.5){\line(1,0){6.5}}                 
                
\put(2.4,4.95){\line(1,0){5.3}}                 
\put(7.9,4.95){\line(1,0){6.5}}                 
                
\put(2.4,5.4){\line(1,0){5.3}}                 
\put(7.9,5.4){\line(1,0){6.5}}                 
                
\put(2.4,6.3){\line(1,0){5.3}}                 
\put(7.9,6.3){\line(1,0){6.5}}                 
                
\put(2.4,6.75){\line(1,0){5.3}}                 
\put(7.9,6.75){\line(1,0){6.5}}                 
                
\put(2.4,7.2){\line(1,0){5.3}}                 
\put(7.9,7.2){\line(1,0){6.5}}                 
                
\put(2.4,7.65){\line(1,0){5.3}}                 
\put(7.9,7.65){\line(1,0){6.5}}                 
                
\put(2.4,8.1){\line(1,0){5.3}}                 
\put(7.9,8.1){\line(1,0){6.5}} 

\put(2.4,8.55){\line(1,0){5.3}} 
\put(7.9,8.55){\line(1,0){6.5}} 

\put(2.4,9){\line(1,0){5.3}} 
\put(7.9,9){\line(1,0){6.5}} 

\put(2.4,9.45){\line(1,0){5.3}} 
\put(7.9,9.45){\line(1,0){6.5}} 

\put(2.4,9.9){\line(1,0){5.3}} 
\put(7.9,9.9){\line(1,0){6.5}} 

\put(2.4,10.35){\line(1,0){5.3}} 
\put(7.9,10.35){\line(1,0){6.5}} 

\put(2.4,10.8){\line(1,0){5.3}} 
\put(7.9,10.8){\line(1,0){6.5}} 

\put(2.4,11.25){\line(1,0){5.3}} 
\put(7.9,11.25){\line(1,0){6.5}} 

\put(2.4,11.7){\line(1,0){5.3}} 
\put(7.9,11.7){\line(1,0){6.5}} 

\put(2.4,12.15){\line(1,0){5.3}} 
\put(7.9,12.15){\line(1,0){6.5}} 

\put(2.4,12.6){\line(1,0){5.3}} 
\put(7.9,12.6){\line(1,0){6.5}} 

\put(2.4,13.05){\line(1,0){5.3}} 
\put(7.9,13.05){\line(1,0){6.5}} 

\put(2.4,13.5){\line(1,0){5.3}} 
\put(7.9,13.5){\line(1,0){6.5}} 

\put(2.4,13.95){\line(1,0){5.3}} 
\put(7.9,13.95){\line(1,0){6.5}} 

\put(2.4,14.4){\line(1,0){5.3}} 
\put(7.9,14.4){\line(1,0){6.5}} 

\put(2.4,14.85){\line(1,0){5.3}} 
\put(7.9,14.85){\line(1,0){6.5}} 

\put(2.4,15.3){\line(1,0){5.3}} 
\put(7.9,15.3){\line(1,0){6.5}} 

\put(2.4,15.75){\line(1,0){5.3}} 
\put(7.9,15.75){\line(1,0){6.5}} 

\put(2.4,16.2){\line(1,0){5.3}} 
\put(7.9,16.2){\line(1,0){6.5}} 

\put(2.4,17.1){\line(1,0){5.3}} 
\put(7.9,17.1){\line(1,0){6.5}} 


%######

\put(8.35,0.9){\line(0,1){17.5}} 
\put(8.8,0.9){\line(0,1){17.5}} 
\put(7.2,0.9){\line(0,1){17.5}} 

%################################

\put(0.75,3.1){\circle{0.6}} 
\put(0.75,5){\circle{0.6}} 
\put(0.75,6.9){\circle{0.6}} 

\put(0.75,13.9){\circle{0.6}} 
\put(0.75,15.8){\circle{0.6}} 
\put(0.75,17.7){\circle{0.6}} 


% Beschriftung
%\put(13,19){\scalebox{25}[5]{\textsc{\huge{15}}}}
\put(12,19){\scalebox{2}{\huge \textbf{15}}}
\put(8.4,18.05){\tiny{A}}
\put(8.5,18.2){\tiny{B}}
\put(8.6,18.05){\tiny{C}}

%\put(12,18.5){April 2003}
%\put(12,18.5){\ding{51}}
\put(3,18.05){Termine}
\put(7.22,18.075){{\scriptsize OK}}
\put(13.93,18.075){{\scriptsize OK}}
%\put(8.9,18.05){Kontakte}
\put(8.9,18.05){Aufgaben}
%\put(8.9,10.45){Aufgaben}

\put(1.35,1.4){20} 
\put(1.35,2.75){18}
\put(1.35,4.1){17} 
\put(1.35,5.45){16}
\put(1.35,6.8){15}
\put(1.35,8.15){14}
\put(1.35,9.5){13}
\put(1.35,10.85){12}
\put(1.35,12.2){11}
\put(1.35,13.55){10}
\put(1.35,14.9){09}
\put(1.35,16.25){08}

\end{picture}
\end{center}

\end{document}

samson

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Temperatur und Luftfeuchtigkeit messen mit dem Arduino Teil III – Auswertung

Nachdem jetzt die Daten erfolgreich in der Datenbank gespeichert werden können, wird es Zeit, die Daten zu visualisieren.

Dazu nutze ich pChart, eine für nicht-kommerzielle Zwecke freie PHP Bibliothek. Die Verbindung zur Datenbank war schnell erstellt, das folgende Beispiel ist nur eine Anpassung des Codes von http://wiki.pchart.net/doc.mysql.integration.html

Hinweis: Der Code ist noch nicht aufgeräumt, dies folgt in den nächsten Tagen.

<?php   
	/* pChart library inclusions */
	include("../pChart/class/pData.class.php");
	include("../pChart/class/pDraw.class.php");
	include("../pChart/class/pImage.class.php");
 
	/* Create and populate the pData object */
	$MyData = new pData();  
 
	/* Connect to the MySQL database */
	$db = mysql_connect("<server>", "<user>", "<password>");
	mysql_select_db("uwezie_db7",$db);
	/* Build the query that will returns the data to graph */
	$Requete = "SELECT temperature, humidity, timestamp FROM ( SELECT @row := @row +1 AS rownum, temperature, humidity, timestamp FROM ( SELECT @row :=0) r, data ) ranked WHERE rownum % 5 = 1 and timestamp >= now() - INTERVAL 24 hour";
	$Result  = mysql_query($Requete,$db);
$timestamp=""; $temperature=""; $humidity="";
 
if($Result == FALSE) {
    die(mysql_error()); // TODO: better error handling
}
 
while($row = mysql_fetch_array($Result))
 {
  /* Get the data from the query result */
  $timestamp   = $row["timestamp"];
  $temperature = $row["temperature"];
  $humidity    = $row["humidity"];
 
  /* Save the data in the pData array */
  //$myData->addPoints($timestamp,"Timestamp");
  $MyData->addPoints($temperature,"Temperature");
  //$myData->addPoints($humidity,"Humidity");
 }
 
 /* Create the pChart object */
 $myPicture = new pImage(1600,800,$MyData);
 /* Turn of Antialiasing */
 $myPicture->Antialias = FALSE;
 /* Add a border to the picture */
// $myPicture->drawRectangle(0,0,1599,799,array("R"=>0,"G"=>0,"B"=>0));
 /* Write the chart title */ 
 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/Forgotte.ttf","FontSize"=>11));
 $myPicture->drawText(150,35,"Temperature",array("FontSize"=>30,"Align"=>TEXT_ALIGN_BOTTOMMIDDLE));
 /* Set the default font */
 $myPicture->setFontProperties(array("FontName"=>"../pChart/fonts/pf_arma_five.ttf","FontSize"=>8));
 /* Define the chart area */
 $myPicture->setGraphArea(60,40,1500,700);
 /* Draw the scale */
 $labelskip =  30; // 1000/24; // values = 1000, hours = 24
$AxisBoundaries = array(0=>array("Min"=>15,"Max"=>25));
$scaleSettings  = array("GridR"=>200,"GridG"=>200,"GridB"=>200,"DrawSubTicks"=>FALSE,"CycleBackground"=>FALSE,"Mode"=>SCALE_MODE_MANUAL, "ManualScale"=>$AxisBoundaries,"LabelSkip"=>$labelskip);
 
$serieSettings = array("R"=>229,"G"=>11,"B"=>11,"Alpha"=>100);
$MyData->setPalette("Temperature",$serieSettings);
 
 
 $myPicture->drawScale($scaleSettings);
 /* Turn on Antialiasing */
 $myPicture->Antialias = TRUE;
 /* Draw the line chart */
 $myPicture->drawLineChart();
 /* Write the chart legend */
 $myPicture->drawLegend(540,20,array("Style"=>LEGEND_NOBORDER,"Mode"=>LEGEND_HORIZONTAL));
 /* Render the picture (choose the best way) */
 $myPicture->autoOutput("drawobjects2.png");
?>
 
<img src="drawobjects2.png"/>

index5.php

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Fancy bullet lists with TikZ

At the end of October there was a question on TSX on how to create visually appealing counters in LaTeX documents. One of the answers (by Sandor Kazi) was the following:

\documentclass{beamer}
 
 
\usepackage{enumitem}
\usepackage{refcount}
\usepackage{tikz}
    \usetikzlibrary{calc}
 
\makeatletter
\newcommand\statuslabel[3][]{%
    \def\scale{#1}
    \ifx\scale\empty\def\scale{1}\fi
 
    \let\no\relax
    \let\n\relax
    \newcommand \no {#2}
    \newcommand \n {#3}
    \def \colorbef {blue}
    \def \colorat {orange}
    \def \coloraft {black!40}
 
    \let\stop\relax
    \sbox\z@{\@tempcnta=0\no\relax}\ifdim\wd0>\z@\relax\@latex@warning{Not a number (\#2): \no}\def\stop{1}\fi
    \sbox\z@{\@tempcnta=0\n\relax}\ifdim\wd0>\z@\relax\@latex@warning{Not a number (\#3): \n}\def\stop{1}\fi
    \ifx\stop\relax
        \ifnum\no>\n\@latex@warning{Wrong parameter order?}\def\stop{1}\fi
    \fi
 
    \ifx\stop\relax
    \else
        \def \no {1}
        \def \n {1}
        \def \colorat {red}
        \def \stop {??}
    \fi
 
    \begin{tikzpicture}[scale=0.1*\scale]
 
    \def \radiusout {2cm}
    \def \radiusin {1.3cm}
    \ifnum\n=1
        \def \margin {0}
    \else
        \def \margin {25/\n}
    \fi
 
    \foreach \s in {1,...,\n}
    {
        \node[circle, scale=\scale] at (0,0) {\tiny\stop};
        \fill[\ifnum\s>\no\coloraft\relax\else\ifnum\s<\no\colorbef\else\colorat\fi\fi]
            ({90-360/\n * (\s - 1)-\margin}:\radiusout) arc ({90-360/\n * (\s - 1)-\margin}:{90-360/\n * (\s)+\margin}:\radiusout) --
            ({90-360/\n * (\s)+\margin}:\radiusin) arc ({90-360/\n * (\s)+\margin}:{90-360/\n * (\s - 1)-\margin}:\radiusin);
    }
    \end{tikzpicture}
}
\makeatother
 
\makeatletter
\newcounter{myenum} % to create unique labels
\newenvironment{myenum}
  {\stepcounter{myenum}
   \edef\nref{\getrefnumber{myenum@\arabic{myenum}}}
   \edef\nref{\expandafter\@firstofone\nref}
   \begin{enumerate}[
     label=\protect\statuslabel{\arabic*}{\nref},
     ref=\arabic*]}
  {\label{myenum@\arabic{myenum}}%
   \end{enumerate}}
\makeatother
 
\begin{document}
 
\begin{frame}
 
\begin{myenum}
\item example
\item example
\item example
\item fsdfs
\end{myenum}
 
\end{frame}
 
\end{document}

visu

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Conditional typesetting based on the used Babel language

Here is a short piece of code that shows how the set document language may be evaluated during the LaTeX run. Credit goes to Heiko Oberdiek for his nice iflang package.

\documentclass[english]{scrartcl}
\usepackage{babel,iflang}
\newcommand{\tr}[2]{\IfLanguageName{ngerman}{#1}{#2}}
 
\begin{document}
 
\tr{Deutscher Text}{English text}
 
\end{document}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Conditional typesetting with ifthen

ifthen.sty allows the definition of various interesting constructs to control, which parts of a document are printed. Here’s a short example for the use of boolean variables:

\documentclass{minimal}
 
\usepackage{ifthen}
\newboolean{somevariable}
\setboolean{somevariable}{false}
 
\begin{document}
 
\ifthenelse{\boolean{somevariable}}{Text if somevariable is true.}{Text if somevariable is false.}
 
\end{document}

ifthen (PDF)

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Temperatur und Luftfeuchtigkeit messen mit dem Arduino Teil I 1/2

Den ursprünglichen Sketch aus dem Artikel vom 24.12. habe ich so abgeändert, dass jetzt in der ersten Zeile die aktuelle Temperatur und Luftfeuchtigkeit gezeigt wird und in der zweiten Zeile die Min/Max Werte für die gemessene Temperatur.

// Beispielcode, um Temperatur/Luftfeuchtigkeit
// eines DHT22 auf einem Sparkfun SerialLCD auszugeben
 
#include "DHT.h"
#include <SoftwareSerial.h>
 
#define DHTPIN 2     // Pin für den DHT22
SoftwareSerial mySerial(4,3); // SoftwareSerial für das LCD
 
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);
 
float maxtemp = 0.0;
float mintemp = 100.0;
 
 
void setup() {
  mySerial.begin(9600); // starte LCD Verbindung
  Serial.begin(9600); // starte zus. serielle Verbindung
  delay(500); // wait for display to boot up
 
  mySerial.write(254); // setze Cursor in die erste Zeile
  mySerial.write(128);
 
  mySerial.write("                "); // clear LCD
  mySerial.write("                ");
  delay(500);
  mySerial.write("  DHT22 Sensor");
 
  dht.begin();
}
 
char tempstring[10], humstring[10];
 
void loop() {
  float h = dht.readHumidity();
  float t = dht.readTemperature();
  delay(5000); // lese alle 5 Sekunden ein
 
  // check if returns are valid, if they are NaN (not a number) then something went wrong!
  if (isnan(t) || isnan(h)) {
    mySerial.write(254); // move cursor to beginning of first line
    mySerial.write(128);
    mySerial.println("DHT22 nicht gefunden");
  } else {
    mySerial.write(254); // move cursor to beginning of first line
    mySerial.write(128);
    mySerial.write("                "); // clear display
    mySerial.write("                ");
    mySerial.write(254); // move cursor to beginning of first line
    mySerial.write(128); 
    mySerial.print(t);
    mySerial.print(" *C ");      
    mySerial.print(h);
    mySerial.print("%");
    mySerial.write(254); // move cursor to beginning of 2nd line
    mySerial.write(192);
    if (t<mintemp){mintemp = t;};
    if (t>maxtemp){maxtemp = t;};
    mySerial.print(mintemp);
    mySerial.print(" ");
    mySerial.print(maxtemp);
    Serial.println(t);
 
  }
}

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Autocompiling LaTeX-files under Linux

To automatically compile files under Linux there are several options available: Gummi (gummi.midnightcoding.org/) is an editor that compiles the LaTeX file whenever it is modified; latexmk (http://www.ctan.org/pkg/latexmk) is another option.

A third option uses Linux‘ inotify, part of the Linux kernel. It tracks changes to files or directories on the filesystem level. For more information see the wikipedia entry: http://en.wikipedia.org/wiki/Inotify

Here’s a short tutorial how to get it to work under Ubuntu/Debian:

  1. Install inotify-tools using sudo apt-get install inotify-tools
  2. Run the following line from the terminal to tell inotify to watch for any changes of the TeX-file (change the name if necessary). I use a simple pdflatex here, make or arara are of course fine as well.
while true; do inotifywait -e modify autocomp1.tex && pdflatex autocomp1.tex; done

Unfortunately it does not work with TeXworks‘ built-in document viewer, it does not recognize that the PDF has been modified externally.

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Temperatur und Luftfeuchtigkeit messen mit dem Arduino Teil II – Speicherung der Messdaten

Nachdem das Messen und Anzeigen der DHT22 Daten recht einfach war, geht es in diesem Posting um die Speicherung der Daten. Ich habe es technisch so gelöst, dass der Arduino über das Ethernet-Shield eine URL aufruft und per Parameter die entsprechenden Variablen übergibt.

Hier der passende Arduino-Code. Der Ethernet Shield holt sich per DHCP eine IP-Adresse vom Server und ruft eine URL auf. Hinter der URL liegt ein PHP Skript, das die Daten mit SensorID und Zeitstempel in eine MySQL Datenbank schreibt. Ich nutze den Zeitstempel des Servers, dadurch spare ich es mir, eine RTC in die Schaltung einzubauen.

#include <Ethernet.h>
#include <SPI.h>
#include "DHT.h"
 
bool connected = false;
 
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0x22, 0x81};
byte serverIP[] = {123,123,123,123}; // Server IP address
 
EthernetClient client;
 
#define SENSORID 1
#define DHTPIN  5     // input pin 5
#define DHTTYPE DHT22   // DHT 22  (AM2302)
DHT dht(DHTPIN, DHTTYPE);
 
 
void setup()
{
  Ethernet.begin(mac); // no IP => DHCP
  Serial.begin(9600);
  Serial.println("setup done");
  dht.begin();
}
 
void loop()
{
  Serial.println("Entered loop");
if (!connected){ 
   Serial.println("Not connected");
  if (client.connect(serverIP,80)){
      connected = true;
      Serial.println("Connected to Server");
      Serial.println("Sending request to server");
      client.print("GET /somedir/someurl.php?s=");
      client.print(SENSORID);      
      client.print("&t=");
      client.print(dht.readTemperature());
      client.print("&h=");
      client.print(dht.readHumidity());
      client.println("HTTP/1.1");
      client.println("Host: www.uweziegenhagen.de");
      client.println("User-Agent: Arduino");
      client.println("Accept: text/html");
      client.println();
     } else {
        Serial.println("Cannot connect to server");
      }
    }   
  else {
    delay(500);
    while (client.connected() && client.available()) {
        char c = client.read();
        Serial.print(c);
      }
      Serial.println();
       client.stop(); 
       connected=false;
}
 
  Serial.println("Done now");
  delay(60000);
}

Hier noch das entsprechende PHP-Skript:

<?php
header('Content-type: text/plain');
echo "Sensor:" . $_GET['s'] . "Temperature= " . $_GET['t'] . " Humidity= " . $_GET['h'];
 
$link = mysql_connect("<server>", "<user>", "password") or die("Keine Verbindung möglich: " . mysql_error());
mysql_select_db("<database>") or die("Auswahl der Datenbank fehlgeschlagen");
$query = "INSERT INTO data values(" $_GET['s'] . "," . $_GET['t'] . "," . $_GET['h'] . ",NOW())";
$result = mysql_query($query) or die("Anfrage fehlgeschlagen: " . mysql_error());
 
mysql_close($link);
?>

Datenbank-Setup:

CREATE TABLE IF NOT EXISTS `data` (
  `sensor` tinyint(1) NOT NULL,
  `temperature` float NOT NULL,
  `humidity` float NOT NULL,
  `timestamp` datetime NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

IMG_8334

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website

Kalenderwochen erzeugen mit Python

Basierend auf einem Kommentar zu http://code.activestate.com/recipes/521915-start-date-and-end-date-of-given-week/ hier ein kurzer Code-Schnipsel, der die Anfangs- und Endtage aller Kalenderwochen in 2014 ausspuckt.

from datetime import date, timedelta
 
def get_week_days(year, week):
    d = date(year,1,1)
    if(d.weekday()>3):
        d = d+timedelta(7-d.weekday())
    else:
        d = d - timedelta(d.weekday())
    dlt = timedelta(days = (week-1)*7)
    return d + dlt,  d + dlt + timedelta(days=6)
 
for x in range(1, 54):
	a,b = get_week_days(2014,x);
	print ("\\subsection{" + a.strftime('%d.%m.%Y') + " -- " +  b.strftime('%d.%m.%Y') + "}\n\n\n");

Uwe

Uwe Ziegenhagen likes LaTeX and Python, sometimes even combined. Do you like my content and would like to thank me for it? Consider making a small donation to my local fablab, the Dingfabrik Köln. Details on how to donate can be found here Spenden für die Dingfabrik.

More Posts - Website