需要支持,以通过ESP8266将DHT22数据写入mySQL

时间:2019-05-30 18:27:49

标签: mysql lua esp8266

我想从通过ESP8266连接的DHT22读取数据,并将其写入LAMP-Stack上的mySQL。 硬件应正确设置(接线),并结合各种示例得出的草图。 =>从传感器读取温度/湿度 =>访问数据库并写入示例表即可。

我需要帮助来调整代码,以便将传感器数据写入DB。如您所见,我不是阳光下最强大的开发人员,但我想对您来说这是小菜一碟。...


#include <ESP8266WiFi.h>           
#include <MySQL_Connection.h>
#include <MySQL_Cursor.h>
#include <DHT.h> 
#define DHTPIN 2     // benutzter ARDUINO-Pin
#define DHTTYPE DHT22   // unser Sensor: DHT22

DHT dht(DHTPIN, DHTTYPE);

IPAddress server_addr(xxxxxxxxx);  // IP of the MySQL *server* here

char user[] = "xxxxxxx";              // MySQL user login username
char password[] = "xxxxxxxxxx";        // MySQL user login password
char INSERT_SQL[] = "INSERT INTO test_arduino.hello_arduino (message) VALUES (''h'')";

// WiFi card example
char ssid[] = "xxxxxxx";         // SSID
char pass[] = "xxxxxxx";     // SSID Password

WiFiClient client;                 // For WiFi instead of EthernetClient
MySQL_Connection conn(&client);
MySQL_Cursor* cursor;

void setup()
{
  dht.begin();  
  Serial.begin(9600);
  while (!Serial); // wait for serial port to connect. Needed for Leonardo only

  // Begin WiFi section
  Serial.printf("\nConnecting to %s", ssid);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }

  // print out info about the connection:
  Serial.println("\nConnected to network");
  Serial.print("My IP address is: ");
  Serial.println(WiFi.localIP());

  Serial.print("Connecting to SQL...  ");
  if (conn.connect(server_addr, 3306, user, password))
    Serial.println("OK.");
  else
    Serial.println("FAILED.");

  // create MySQL cursor object
  cursor = new MySQL_Cursor(&conn);
}

void loop()
{

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  if (conn.connected())
    cursor->execute(INSERT_SQL);

  delay(5000);
  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
}

0 个答案:

没有答案