Sending arduino sensor data to database MySQL

Hi everyone... I’m newbie in Arduino IDE programming. I worked on my graduation project here. My project is to send data from ethernet arduino using sensor INA219 from arduino to database MySQL. I got error ‘sendDB’ was not declared in this scope error during the compilation process. What to do so that "sendToDB" doesn't make errors? This is my code. If there are any mistakes in my code, can anyone help correct them? I appreciate any help. TIA~~

#include <SPI.h>
#include <Ethernet.h>
#include <Wire.h> 
#include <Adafruit_INA219.h>

Adafruit_INA219 sensor219 (0x41);
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //MAC Address

float DataVoltage;
float DataCurrent;

int period = 30000; //interval save data to db
unsigned long time_now = 0;

unsigned long byteCount = 0;
bool printWebData = true;  // set to false for better speed measurement


char server[] = "192.168.10.10";
IPAddress ip(192,168,10,5); //arduino IP
EthernetClient client; 

void setup() {
  Serial.begin(9600);
  sensor219.begin();

  Ethernet.begin(mac, ip);
  Serial.print("Local IP: ");
  Serial.println(Ethernet.localIP());
  delay(2000);
}

void loop() {
  if(millis() >= time_now + period){
    time_now += period;
    SendtoDB();
  }
  int len = client.available();
  if (len > 0) {
        byte buffer[80];
        if (len > 80) len = 80;
        client.read(buffer, len);
        if (printWebData) {
          Serial.write(buffer, len); // show in the serial monitor (slows some boards)
        }
        byteCount = byteCount + len;
  }

//insert data ke DB via injeksi control.php
void SendtoDB(){
   if (client.connect(server, 80))
    Serial.println("");
    Serial.println("connected");
    // Make a HTTP request:
    Serial.print("GET /arduino_mysql/control.php?dataVoltage=");
    Serial.print(DataVoltage);
    Serial.print("&dataCurrent=");
    Serial.println(DataCurrent);
    Serial.println("");
    
    client.print("GET /arduino_mysql/control.php?dataVoltage=");     //YOUR URL
    client.print(DataVoltage);
    client.print("&dataCurrent=");
    client.print(DataCurrent);
    client.print(" ");      //SPACE BEFORE HTTP/1.1
    client.print("HTTP/1.1");
    client.println();
    client.println("Host: 192.168.10.5");
    client.println("Connection: close");
    client.println();
    else {
    // if you didn't get a connection to the server:
    Serial.println("connection failed");
  }
 }
}

image

and here my script to insert data to Database

<?php
class ethernet{
public $link='';
function __construct($current, $voltage){
$this->connect();
$this->storeInDB($current, $voltage);
}
 
function connect(){
$this->link = mysqli_connect('localhost','root','') or die('Cannot connect to the DB');
mysqli_select_db($this->link,'arduino_sensor') or die('Cannot select the DB');
}
 
function storeInDB($current, $voltage){
$query = "insert into sensor_ethernet set voltage='".$voltage."', current='".$current."'";
$result = mysqli_query($this->link,$query) or die('Errant query:  '.$query);
if($result === TRUE){echo "Data Tersimpan";}else{echo "Gagal Menyimpan data";}
}
 
}
if($_GET['dataCurrent'] != '' and  $_GET['dataVoltage'] != ''){
$ethernet=new ethernet($_GET['dataCurrent'],$_GET['dataVoltage']);
}

?>

Check your curly brackets {}
The first fault is missing close bracket on end of loop() so SendtoDB() is part of loop and cannot be found. Your also missing brackets in the SendDB() procedure.

1 Like

Have you noticed the code formatting 'saying' how something is missing? Did you count { and the } do they equal?

1 Like