ChuckBell / MySQL_Connector_Arduino

Database connector library for using MySQL with your Arduino projects.
332 stars 133 forks source link

BME280 disables server connection #68

Closed sotb11 closed 6 years ago

sotb11 commented 6 years ago

I use example codes for mysql connection and bme280. Both works seperatedly, but together there is stop on mysql server connection. What can I do with this?


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

BME280I2C bme;

byte mac_addr[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
String t, h, p;
char hostname[] = "000000"; // change to your server's hostname/URL
char user[] = "000000";               // MySQL user login username
char password[] = "pass";         // 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(9600);
  while (!Serial); // wait for serial port to connect
  Wire.begin();
  //delay(1000);
    // Nadawanie MAC 
   Ethernet.begin(mac_addr);
   Serial.println(Ethernet.localIP());
  // Begin DNS lookup
  dns_client.begin(Ethernet.dnsServerIP());
  dns_client.getHostByName(hostname, server_ip);
  Serial.println(server_ip);
  // End DNS lookup

  if(!bme.begin()) {
      Serial.println("Could not find the BME280 Sensor, check wiring"); 
      while (1);
  }
    // bme.chipID(); // Deprecated. See chipModel().
  switch(bme.chipModel())
  {
     case BME280::ChipModel_BME280:
       Serial.println("Found BME280 sensor! Success.");
       break;
     case BME280::ChipModel_BMP280:
       Serial.println("Found BMP280 sensor! No Humidity available.");
       break;
     default:
       Serial.println("Found UNKNOWN sensor! Error!");
  }

  connecting();
}

void connecting(){
Serial.println("Connecting..."); // and stop.
delay(1000);
  if (conn.connect(server_ip, 3306, user, password)) { 
    Serial.println("Connected.");
    delay(1000);
  }
  else{
    Serial.println("Not Connected.");
    conn.close();
  }
}

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

}

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");

   char INSERT_SQL[] = "INSERT INTO sh177864_alberts.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);

   delay(1000);
}```
Bolukan commented 6 years ago

Please add your errors and if relevant your database table to check whether INSERT INTO sh177864_alberts.ruchLog (ruch1) VALUES ('tak'); is valid. PS: With 3 ``` you get code layout.

sotb11 commented 6 years ago

If I use only mysql basic_insert.ino example code it works properly. There is no errors on both Arduino monitor and Mysql base. Data is saved in the database. Everything is ok. Arduino monitor shows:

195.114.1.xxx
Connecting...
Connected to server version 5.1.59-superhost-p1-log
Recording data.
Recording data.

But if I add BME_280_I2C_Test.ino from arduino examle code library (combined code in my first message) it stops on line:

Serial.println("Connecting...");

Arduino monitor shows:

192.168.2.128 (my computer ip)
195.114.1.xxx (this is mysql server ip solved by Dns.h library)
Found BME280 sensor! Success.
Connecting...

And nothing more. Data is not saved in the database because of lack of connection. This is message from compiler:

./opt/arduino-builder/arduino-builder -compile -core-api-version 10611 -hardware opt/arduino-builder/hardware -hardware ./opt/cores -tools opt/arduino-builder/tools -tools ./opt/tools -built-in-libraries opt/libraries/latest -logger humantags -fqbn arduino:avr:uno -build-cache /tmp -build-path /tmp/836029334/build -verbose -libraries /tmp/836029334/custom -libraries /tmp/836029334/pinned /tmp/836029334/connect_by_hostname_2

Sketch uses 28816 bytes (89%) of program storage space. Maximum is 32256 bytes.

Global variables use 1525 bytes (74%) of dynamic memory, leaving 523 bytes for local variables. Maximum is 2048 bytes.

Programming with: Serial

Flashing with command:C:/Users/sotb/.arduino-create/arduino/avrdude/6.3.0-arduino9/bin/avrdude.exe -CC:/Users/sotb/.arduino-create/arduino/avrdude/6.3.0-arduino9/etc/avrdude.conf -q -q -patmega328p -carduino -PCOM7 -b115200 -D -Uflash:w:C:/Users/sotb/AppData/Local/Temp/arduino-create-agent996801964/connect_by_hostname_2.hex:i

sotb11 commented 6 years ago

I commented switch(bme.chipModel()) section and some client->print lines and code start to work. It looks like lack of memory :( So I need to change Arduino Uno for Arduino Mega to continue project. Code works if: Sketch uses 24058 bytes (74%) of program storage space. Maximum is 32256 bytes. Global variables use 1365 bytes (66%) of dynamic memory, leaving 683 bytes for local variables. Maximum is 2048 bytes. Thanks.

Bolukan commented 6 years ago

I had a comparable problem, some compilations (after changes) worked, others didn't. I could not discover a logic in my changes. A too small char buffer (and no checks) for the query was the cause. (Using ESP8266's with 4MB flash and 80KB RAM I never stumble against memory size problems)

sotb11 commented 6 years ago

I bought D1 board with 32 MB flash, but I did not connect it yet. Now I see I should start with D1 :)

Bolukan commented 6 years ago

I just finished this code. Take a look: https://github.com/Bolukan/BME280-to-MySQL-logger

sotb11 commented 6 years ago

Very interesting. I did not finish with Arduino yet. I have bought PMS3003 too. I saw finished (polish) code for D1mini + BME280 + PMS3003 and without mysql - https://www.elektroda.pl/rtvforum/viewtopic.php?p=17511823#17511823 It works on 5V power supply.

Bolukan commented 6 years ago

More good code (at least the top one) on github: https://github.com/topics/pms3003 And a new version: PMS7003