Wasserwaage

Beispiel für eine Wasserwage mit dem Arduino Esplora.

Sourcecode

#include <Esplora.h>
#include <SPI.h>
#include <TFT.h>
#include <math.h>

int r = EsploraTFT.height()/2;  // Radius der Libelle
int mx = EsploraTFT.width()/2;  // Mittelpunkt x
int my = EsploraTFT.height()/2; // Mittelpunkt y
int x_old = mx;
int y_old = my;
int x_new = x_old;
int y_new = y_old;
int sl_range = 255;  // Enpfindlichkeit für Libelle
int ang_range = 100;
int x_cal = -15;  // Kalibrierung 
int y_cal = -15;  // Kalibrierung
unsigned long interval = 50;
unsigned long prev_millis = 0;

String phi_x_old;
String phi_y_old;
String x_axis_old;
String y_axis_old;
String test_old;
double phi_x = 0; // Winkel in x-Richtung in °
double phi_y = 0; // Winkel in y-Richtung in °

void setup() {
  EsploraTFT.begin();
  displaySpiritLevel();
}

void loop() {
  double x_axis = Esplora.readAccelerometer(X_AXIS) + x_cal;
  double y_axis = Esplora.readAccelerometer(Y_AXIS) + y_cal;
  
  // Display rotieren
  /*if((-10.00 < phi_x && phi_x < 10.00) && (-10.00 < phi_y && phi_y < 10.00)){
     printString("Test 1",&test_old,1,21);
     EsploraTFT.setRotation(1);
  }else if ((-100 < phi_x < 30) && (-10 < phi_y < 10)){
    printString("Test 0",&test_old,1,21);
     EsploraTFT.setRotation(0);
  }else if ((30 < phi_x < 100) && (-10 < phi_y < 10)){
    printString("Test 2",&test_old,1,21);
     EsploraTFT.setRotation(2);
  //}else if (-10 < phi_x < 10 && -10 < phi_y < 10){
  
    
  //}else if (-10 < phi_x < 10 && -10 < phi_y < 10){
    
  }*/
  
  EsploraTFT.setRotation(2);
  mx = EsploraTFT.width()/2;
  my = EsploraTFT.height()/2;
  
  String s = "Test 1";  
  printString(s,&test_old,1,21);
     
  if(millis() - prev_millis > interval){
     displayAngel(x_axis,y_axis,ang_range); // Neigungswinkel anzeigen
     prev_millis = millis();
  }
   
  displayBubble(x_axis,y_axis,sl_range); // Libelle anzeigen
}

void displayAngel(double x_axis,double y_axis,int range){
  double a_x = map(x_axis,-range,range,-9.81,9.81); //Skalierung auf Beschleunigunswerte
  a_x = constrain(a_x,-9.81,9.81);
  double a_y = map(y_axis,-range,range,-9.81,9.81); //Skalierung auf Beschleunigunswerte
  a_y = constrain(a_y,-9.81,9.81);
  phi_x = asin(a_x/9.81) * 180/3.14; // Winkel in x-Richtung in °
  phi_y = asin(a_y/9.81) * 180/3.14; // Winkel in y-Richtung in °
  
  char c = (char)247; // Grad-Zeichen
  String phi_x_s = "Phi X: " + String(phi_x) + String(c);
  printString(phi_x_s,&phi_x_old,1,1);
  String phi_y_s = "Phi Y: " + String(phi_y) + String(c);
  printString(phi_y_s,&phi_y_old,1,11);
  
  }

void printString(String txt,String * txt_ptr_old,int x_pos,int y_pos){
  
  // alten Text löschen
  String txt_old = *txt_ptr_old;
  char txt_out_old[txt_old.length() + 1];
  txt_old.toCharArray(txt_out_old,txt_old.length() + 1);
  EsploraTFT.stroke(255,255,255);
  EsploraTFT.text(txt_out_old,x_pos,y_pos);
  
  /*EsploraTFT.noStroke();
  EsploraTFT.fill(255,255,255);
  EsploraTFT.rect(x_pos,y_pos,96,11);*/
  
  
  // neuen Text schreiben
  char txt_out[txt.length() + 1];
  txt.toCharArray(txt_out,txt.length() + 1);
  EsploraTFT.stroke(0,0,0);
  EsploraTFT.text(txt_out,x_pos,y_pos);
  
  *txt_ptr_old = txt;
  }
  
void displayBubble(double x_axis,double y_axis,int range){
  x_axis = map(x_axis,-range,range,-r,r);
  y_axis = map(y_axis,-range,range,-r,r);
  
  // Umrechnung in Polarkoordinaten
  int r_1 = sqrt((x_axis * x_axis) + (y_axis * y_axis));
  double phi = atan2(y_axis,x_axis);
  
  //Beschränkung des Abstandes
  r_1 = min(r_1,r-10);
  
  //Zurückrechnung in kartesische Koordinaten
  x_axis = r_1 * cos(phi);
  y_axis = r_1 * sin(phi);
  
  x_new = x_axis  + mx;
  y_new = -y_axis  + my;

  EsploraTFT.noStroke();
  EsploraTFT.fill(255,255,0);
  EsploraTFT.circle(x_old,y_old,8);
  
  EsploraTFT.stroke(128,128,128);
  EsploraTFT.fill(255,255,255);
  EsploraTFT.circle(x_new,y_new,8);
  
  EsploraTFT.stroke(0, 0, 0);
  EsploraTFT.noFill();
  EsploraTFT.circle(mx,my,10);
  
  x_old = x_new;
  y_old = y_new;
  }
  
void displaySpiritLevel(){
  EsploraTFT.background(255,255,255);
  EsploraTFT.stroke(0, 0, 0);
  EsploraTFT.fill(255,255,0);
  EsploraTFT.circle(mx,my,r);
  EsploraTFT.noFill();
  EsploraTFT.circle(mx,my,10);
  }

 

RSS-Feed Reader

Diesen RSS-Feed-Reader habe ich lange Zeit um auf meiner Webseite RSS-Feeds einzublenden. Eingebunden wird das ganze über einen iframe in der Seite.

Z.B.:

<iframe src="https://your-domain/rssfeed.php?url=http://newsfeed.zeit.de/politik/index" width="100%" height="600"></iframe>

Viel Spaß bei der Nutzung!

Sourcecode

<?php
$display_channel_name = "false";
$offset = "0";
$number_items = "0";
$number_chars = "0";
$rss_cache_dir = 'absolute path to your cache directory';

echo '<html>'."\n";
echo '<head>'."\n";
echo '<link rel="stylesheet" type="text/css" href="https://your-domain/rss_styles.css" />'."\n";
echo '</head>'."\n";
echo '<body>'."\n";

if(isset($_GET['url']) && $_GET['url'] != ''){
  $url = trim(strip_tags($_GET['url']));
  if(isset($_GET['display_channel_name'])){$display_channel_name = trim(strip_tags($_GET['display_channel_name']));};
  if(isset($_GET['offset'])){$offset = trim(strip_tags($_GET['offset']));};
  if(isset($_GET['num_items'])){$num_items = trim(strip_tags($_GET['num_items']));};
  if(isset($_GET['num_chars'])){$num_chars = trim(strip_tags($_GET['num_chars']));};
  
  $rss = '';
  $rss_file = 'rss_'.md5($url);
  
  if(date("j",@filemtime("$rss_cache_dir/$rss_file")) == date("j",time())){
    $fh = @fopen("$rss_cache_dir/$rss_file","r");
    if($fh != ''){
      while(!feof($fh)){
        $rss .= fgets($fh,2048);
        };
      fclose($fh);
      echo $rss;
      exit();
      };
    };
    
  $ret = getrss($url);
  $rss_channel = $ret[0];
  $rss_items = $ret[1];
  
  if($display_channel_name == 'true'){
    $rss .= '<h1>'."\n";
    if($rss_channel['link']){ 
      $rss .= '<a href="'.$rss_channel['link'].'" target="_blank">'.$rss_channel['title'].'</a><br/>'."\n";
    }else{
      $rss .= $rss_channel['title'].'<br/>'."\n";
      };
    $rss .= '<font size="-1">'.$rss_channel['description'].'</font>'."\n";
    $rss .= '</h1>'."\n";
    };

  if($num_items == 0 or $num_items > count($rss_items)-$offset){$num_items = count($rss_items)-$offset;};
  $rss .= '<table>'."\n";
  for($i=0;$i<$num_items;$i++){
    $rss .= '<tr><td><a class="link" href="'.$rss_items[$i]['link'].'" target="_blank">'.$rss_items[$i]['title'].'</a><br/>'."\n";
    $rss .= '<div class="description">'."\n";
    
    $description = '';
    if(isset($rss_items[$i]['content:encoded']) && $rss_items[$i]['content:encoded'] != ''){
      $description = $rss_items[$i]['content:encoded'];
    }elseif(isset($rss_items[$i]['description']) && $rss_items[$i]['description'] != ''){
      $description = $rss_items[$i]['description'];
      };
    
    if($description != ''){
      if(($num_chars)&&($num_chars<strlen($description))){
        $rss .= substr($description,0,strpos($description," ",$num_chars))." ...";
      }else{
        $rss .= $description;
        };
      if($rss_items[$i]['link']){
        $rss .= '&nbsp;<a href="'.$rss_items[$i]['link'].'" target="_blank">mehr...</a><br/>'."\n";
        };
      };
    $rss .= '</div></td></tr>'."\n";
    };
  if($num_items <= 0){
    $rss .= '<tr><td>'."\n";
    $rss .= '</td></tr>'."\n";
    };
  $rss .= '</table>'."\n";
  
  if($rss_channel['dc:publisher']){$rss .= "<p>By {$rss_channel['dc:publisher']}</p>\n";};
  if($rss_channel['dc:date']){$rss .= "<p>{$rss_channel['dc:date']}</p>\n";};
  if($rss_channel['copyright']){$rss .= "<div class='copy'>{$rss_channel['copyright']}</div>\n";};

  $fh = @fopen("$rss_cache_dir/$rss_file","w");
  if($fh != ''){
    fwrite($fh,$rss);
    };
  echo $rss;
}else{
  echo 'Keine URL!<br>'."\n";
  exit();
  };
echo '</body>'."\n";
echo '</html>'."\n";

function getrss($url){
  $rss_channel = array();
  $rss_items = array();
  $simple = implode('',@file($url));

  $p = xml_parser_create();
  xml_parse_into_struct($p,$simple,$vals,$index);
  xml_parser_free($p);
  $type=0;
  
  $id=0;
  for($i=0;$i<count($vals);$i++){

    if((strtolower($vals[$i]['tag']) == 'channel') && ($vals[$i]['type'] == 'open')){$id=$vals[$i]['level']+1;};
    if(($type == 0) && ($id == $vals[$i]['level'])){
      $rss_channel[strtolower($vals[$i]['tag'])] = $vals[$i]['value'];
    }else{
      $tmp[strtolower($vals[$i]['tag'])] = $vals[$i]['value'];
      };

    if(strtolower($vals[$i]['tag']) == 'item'){
      if(($vals[$i]['type']=="open")&&($type==0)){$type=1;};
      if($vals[$i]['type']=="close"){
        $rss_items[]=$tmp;
        $tmp = array();
        };
      };
    };
  $ret[0] = $rss_channel;
  $ret[1] = $rss_items;
  return($ret);
  };

?>

Mail Kontaktformular

Diesen PHP-Code hatte ich für mein Kontaktformular genutzt. Er sollte einigermaßen Spam-sicher sein. Ich hatte es auf einer anderen Webseite über mehrere Jahre genutzt und keine diesbezüglichen Probleme gehabt. Die Parameter zu Beginn müssen angepasst werden, insbesondere „$to“, welcher den Empfänger definiert, zu welchem die Mail geschickt wird.

Viel Spaß beim nutzen oder umschreiben!

Sourcecode

<?php
$to = 'Mailadresse von Webmaster';
$lifetime = 86400; // lebensdauer der session, 24 stunden
$min_response_time = 10; // eine nachricht kann nicht in weniger als 10 sekunden geschrieben werden
$max_response_time = 1800; // das schreiben einer nachricht dauert maximal 30 minuten
$max_number_of_attempts = 10; // maximal 10 Versuche in $lifetime
$max_number_of_unfilled_form_attempts = 3; // maximal 3 versuche mit nicht richtig ausgefülltem formular
$stylesheet = 'http://www.no-brain-no-pain.de/static_styles.css';

session_set_cookie_params($lifetime);
session_name('mail');
session_start();
if(!isset($_SESSION['state'])){$_SESSION['state'] = '0';};
if(!isset($_SESSION['start_time'])){$_SESSION['start_time'] = time();};
if(!isset($_SESSION['number_of_attempts'])){$_SESSION['number_of_attempts'] = 0;};
if(!isset($_SESSION['number_of_unfilled_form_attempts'])){$_SESSION['number_of_unfilled_form_attempts'] = 0;};
$response_time = time() - $_SESSION['start_time'];

if($_SESSION['number_of_attempts'] > $max_number_of_attempts){
    //error
    $text = mail_box_open($stylesheet);
    $text .= '<h2>Die maximale Anzahl der Mail-Versuche in den letzten 24h wurde überschritten!</h2>'."\n";
    $text = mail_box_close();
    echo $text;
    exit();
    };

$text = mail_box_open($stylesheet);
if($_SESSION['state'] == 0 && !isset($_POST['submit'])){ //mail neu aufgerufen
  // send mail form
  $_SESSION['start_time'] = time();
  $text .= form('','','',false);
  $_SESSION['state'] = 1;
}elseif($_SESSION['state'] == 0  && isset($_POST['submit'])){
  // error, form abgeschickt und cookie gelöscht...
  $_SESSION['start_time'] = time();
  $text .= form('','','',false);
  $_SESSION['state'] = 1;
}elseif($_SESSION['state'] == 1  && !isset($_POST['submit'])){
  // error, wenn fenster nur geschlossen ohne abschicken
  $_SESSION['start_time'] = time();
  $text .= form('','','',false);
  $_SESSION['state'] = 1;
}elseif($_SESSION['state'] == 1  && isset($_POST['submit'])){ //ausgefülltes formular erwartet		
  if($response_time < $min_response_time){
    // error
    $text .= '<h2>Die schreiben zu schnell!</h2>'."\n";
    $_SESSION['state'] = 0;
  }elseif($response_time > $max_response_time){
    // error
    $text .= '<h2>Die schreiben zu langsam!</h2>'."\n";
    $_SESSION['state'] = 0;
  }else{
    if($_SESSION['number_of_unfilled_form_attempts'] > $max_number_of_unfilled_form_attempts){
      //error
      $text .= '<h2>Die maximale Anzahl ungültiger Mail-Versuche wurde überschritten!</h2>'."\n";
      $_SESSION['number_of_unfilled_form_attempts'] = 0;
      $_SESSION['state'] = 0;
    }else{
      // parameter prüfen und mail senden
      $from = 'From: '.trim(strip_tags($_POST['from']));
      $subject = trim(strip_tags($_POST['subject']));
      $message = trim(strip_tags($_POST['message']));
      if($from != '' && $subject != '' && $message != ''){
        if(check_mail_address($from)){
          if(mail($to,$subject,$message,$from)){
            $text .= '<h2>Die folgende Mail wurde verschickt:</h2>'."\n";
            $text .= form($from,$subject,$message,true);
            $_SESSION['state'] = 0;
          }else{
            $text .= '<h2>Die Mail konnte nicht verschickt werden!</h2>'."\n";
            $_SESSION['state'] = 0;
            };
        }else{
          // error
          $_SESSION['start_time'] = time();
          $text .= '<h2>Ungültige Mailadresse!</h2>'."\n";
          $text .= form($from,$subject,$message,false);
          $_SESSION['number_of_unfilled_form_attempts']++;
          };
      }else{
        //mail unvollständig
        $_SESSION['start_time'] = time();
        $text .= '<h2>Das Formular ist nicht vollständig ausgefüllt!</h2>'."\n";
        $text .= form($from,$subject,$message,false);
        $_SESSION['number_of_unfilled_form_attempts']++;
        };
      };
    };
  $_SESSION['number_of_attempts']++;
  };
$text .= mail_box_close();
echo $text;

function mail_box_open($stylesheet){
  $text = '<html>'."\n";
  $text .= '<head>'."\n";
  $text .= '<link rel="stylesheet" type="text/css" href="'.$stylesheet.'" />'."\n";
  $text .= '<title>Mail Box</title>'."\n";
  $text .= '</head>'."\n";
  $text .= '<body id="page">'."\n";
  $text .= '<div class="static">'."\n";
  $text .= '<div class="box">'."\n";
  $text .= '<div class="box_head">Mail an Webmaster</div>'."\n";
  $text .= '<div class="box_content">'."\n";
  return($text);
  }

function mail_box_close(){
  $text = '</div>'."\n";
  $text .= '</div>'."\n";
  $text .= '</div>'."\n";
  $text .= '</body>'."\n";
  $text .= '</html>'."\n";
  return($text);
  }
  
function form($from,$subject,$message,$ro){
  if($ro){$ro='readonly="readonly"';}else{$ro='';};
  $text = '<form action="'.$_SERVER['REQUEST_URI'].'" method="post">'."\n";
  $text .= '<p>Ihre E-Mail-Adresse:<br><input type="text" size="32" maxlength="64" name="from" value="'.$from.'" '.$ro.'></p>'."\n";
  $text .= '<p>Betreff:<br><input type="text" size="32" maxlength="64" name="subject" value="'.$subject.'" '.$ro.'></p>'."\n";
  $text .= '<p>Nachricht:<br><textarea cols="64" rows="24" name="message" '.$ro.'>'.$message.'</textarea></p>'."\n";
  $text .= '<p><input type="submit" name="submit" value="Absenden" '.$ro.'>'."\n";
  $text .= '<input type="reset" name="reset" value="Abbrechen" '.$ro.'></p>'."\n";
  $text .= '</form>'."\n";
  return($text);
  }

function check_mail_address($from){
  $f = false;
  $temp = explode('@',$from);
  if(count($temp) === 2){
    if(count(explode('.',$temp[1])) === 2){
      $f = true;
      };
    };
  return($f);
  }	
?>