Соединение с сервером разрывается, когда код инициализации BME280 не комментируется - (arduino) - PullRequest
0 голосов
/ 26 октября 2018

Arduino + w5100 ethernet щит + BME280 бош. Я использую пример кода для соединения с MySQL - это работает. Я попробовал пример кода с BME280 - он тоже работает. Когда я соединяю эти сценарии, он останавливается при подключении к серверу MySQL Я нашел, если я прокомментирую этот фрагмент:

    while(!bme.begin())
  {
    Serial.println("Could not find BME280I2C sensor!");
    delay(1000);
  }

подключение к серверу MySQL снова работает. Что случилось? Ниже приведен весь код.

#include <Ethernet.h>
#include <MySQL_Connection.h>
#include <Dns.h>
#include <MySQL_Cursor.h>
#include <BME280I2C.h>
#include <Wire.h>

BME280I2C bme;

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char hostname[] = "*********"; // change to your server's hostname/URL
char user[] = "*******";               // MySQL user login username
char password[] = "*********";         // MySQL user login password
char INSERT_SQL[] = "";


IPAddress server_ip;
EthernetClient client;
MySQL_Connection conn((Client *)&client);
DNSClient dns_client;   // DNS instance

void setup() {
  Serial.begin(115200);
  while (!Serial); // wait for serial port to connect

  Wire.begin();
  delay(1000);
  while(!bme.begin())
  {
    Serial.println("Could not find BME280I2C sensor!");
    delay(5000);
  }

  Ethernet.begin(mac_addr);
  // Begin DNS lookup
  dns_client.begin(Ethernet.dnsServerIP());
  dns_client.getHostByName(hostname, server_ip);
  Serial.println(server_ip);
  // End DNS lookup
  Serial.println("Connecting...");
  if (conn.connect(server_ip, 3306, user, password)) {
    Serial.println("Connected.");
    delay(1000);
  }
  else
    {Serial.println("Connection failed.");
  conn.close();}

}

void loop() {
//  printBME280Data(&Serial);
   //delay(500);

  char INSERT_SQL[] = "INSERT INTO sh177864.ruchLog (ruch1) VALUES ('tak')";
  Serial.println("Recording data.");

  // Initiate the query class instance
  MySQL_Cursor *cur_mem = new MySQL_Cursor(&conn);
  // Execute the query
  cur_mem->execute(INSERT_SQL);
  // Note: since there are no results, we do not need to read any data
  // Deleting the cursor also frees up memory used
  delete cur_mem;
    delay(10000);
}

//void printBME280Data
//(
//   Stream* client
//)
//{
//   float temp(NAN), hum(NAN), pres(NAN);

//   BME280::TempUnit tempUnit(BME280::TempUnit_Celsius);
//   BME280::PresUnit presUnit(BME280::PresUnit_Pa);

//   bme.read(pres, temp, hum, tempUnit, presUnit);

//   client->print("Temp: ");
//   client->print(temp);
//   client->print("°"+ String(tempUnit == BME280::TempUnit_Celsius ? 'C' :'F'));
//   client->print("\t\tHumidity: ");
//   client->print(hum);
//   client->print("% RH");
//   client->print("\t\tPressure: ");
//   client->print(pres);
//   client->println(" Pa");

//   delay(1000);
//}
...