ESP2MySQL

#include <ESP8266WiFi.h>
#include <WiFiClient.h>

#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>

#define TMP36 A0
#define LED D4

// WiFi
char WiFissid[] = "...";
char WiFipass[] = "...";
WiFiClient wifiClient;

// MySQL-Server
IPAddress server_addr( n, n, n, n );
char DBuser[] = "esp";
char DBpassword[] = "...";
MySQL_Connection conn( (Client *)&wifiClient );

// SQL-Skripte zum Anlegen der Tabelle:
// CREATE TABLE IF NOT EXISTS esp.daten ( zeit timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, temp float NOT NULL )

char INSERT_SQL[] = "INSERT INTO esp.daten( temp ) VALUES ( %.1f )";
char query[128];

int sensorwert = 0;
float voltage = 0, temperatur = 0;

void setup() 
{
  Serial.begin( 115200 );
  while( !Serial );
  pinMode( TMP36, INPUT );
  pinMode( LED, OUTPUT );

  Serial.print( "Connecting to " );
  Serial.println( WiFissid );
  WiFi.begin( WiFissid, WiFipass );
  while( WiFi.status() != WL_CONNECTED ) 
  {
    delay( 200 );
    Serial.print( "." );
  }
  Serial.println( "" );
  Serial.println( "WiFi Connected" );
  Serial.println( "-------------------------------------------------" );
  Serial.println( "Connecting to database" );
  while( conn.connect( server_addr, 3306, DBuser, DBpassword ) != true )
  {
    delay( 200 );
    Serial.print ( "." );
  }
  Serial.println( "" );
  Serial.println( "Connected to SQL Server!" );  
  Serial.println( "-------------------------------------------------" );
}

void loop() 
{
  // Auslesen und umrechnen des Sensorwertes
  sensorwert=analogRead(TMP36);
  voltage = (float)sensorwert * 3.3; 
  voltage /= 1023.0; 
  temperatur = (voltage - 0.5) * 100 - 4.0; // 4.0 mit meinem Sensor gemessen, kann abweichen

  // Temperatur in SQL-String einfügen
  sprintf( query, INSERT_SQL, temperatur );

  Serial.print( "Recording data: " );
  Serial.println( query );
  
  // Verbindung zur Datenbank herstellen
  MySQL_Cursor *cur_mem = new MySQL_Cursor( &conn );
  // Abfrage (INSERT) an die DB schicken
  if( cur_mem->execute( query ) == true )
  {
    Serial.println( "Error inserting data!" );
    while(1)
    {
      digitalWrite(LED, HIGH);
      delay(100);
      digitalWrite(LED, LOW);
      delay(500);
    }
  }
  delete cur_mem;

  delay( 10000 );
}