Go Down

Topic: Problem with connector between Arduino and MS SQL Server (Read 967 times) previous topic - next topic

fernando1828

Apr 08, 2019, 06:59 pm Last Edit: Apr 08, 2019, 07:02 pm by fernando1828
Hi everybody. After having surfed the network for a long time I found a direct connector between MS SQL server and arduino. The project I try to do is based on sending sensor values and recording them in the database. The idea is to not use any connector between arduino and SQL.
The problem is that the program does not compile (I mean the connector library example). I attach the libraries. Is there any other direct connector between SQL and Arduino?

Thanks!!

Link: https://github.com/mustafakemalgilor/arduino-mssql

PaulS

I'm not opening zip files. Post your sketch directly. Post links to the libraries. Post your error messages.
The art of getting good answers lies in asking good questions.

Thee_Captain

I find it fairly straight forward to make an HTTP endpoint and handle the SQL on the server. It also creates an opportunity for a good API.
Throw a little karma my way. What goes around comes around.

fernando1828

Links:

Arduino to sql:  https://github.com/mustafakemalgilor/arduino-mssql

UIPethernet: https://github.com/UIPEthernet/UIPEthernet

Code: [Select]

//Included by myself!!!
#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>
//Included by myself!!!

#include <Ethernet.h>
#include "sqlard.h"


uint8_t Ethernet_MacAddr[6] = { 0x74, 0x69, 0x69, 0x2D, 0x30, 0x31 }; /* MAC ADRESİ */
static byte Static_IPAddr[] = { 172, 16, 1, 2 };
static byte Gateway_IPAddr[] = { 172, 16, 1, 1 };
static byte Subnet_Mask[] = { 255, 255, 0, 0 };
EthernetClient client;

SQLard MSSQL(Gateway_IPAddr, 1433, &client);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial);
  Ethernet.begin(Ethernet_MacAddr, Static_IPAddr, Gateway_IPAddr, Gateway_IPAddr, Subnet_Mask);
  if (MSSQL.connect())
  {
    MSSQL.setCredentials(L"arduino", L"ard_login", L"ard_password", L"hostx");
    MSSQL.login();
  }

}

void loop() {
  // put your main code here, to run repeatedly:
  int loop_count = 0;
  delay(5000);

  long affected_rows = MSSQL.executeNonQuery(L"INSERT INTO [dbo].[test]([data]) VALUES('deger1234') ");
  long yeni = MSSQL.executeNonQuery(L"DROP TABLE [dbo].[test23]");
  Serial.print(affected_rows);
  Serial.println(" row(s) affected.");
  if (!client) {
    Serial.println("disconnecting.");
    client.stop();
  }
  if (++loop_count == 10)
  {
    MSSQL.executeNonQuery(L"DELETE FROM [dbo].[test]");
    loop_count = 0;
  }
}


Errors (some of them):

Quote
In file included from sqlard_test.ino:5:0:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPClient.h:25:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
 #import "Client.h"
  ^
In file included from sqlard_test.ino:5:0:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPClient.h:26:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
 #import "utility/mempool.h"
  ^
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPClient.h:29:4: warning: #import is a deprecated GCC extension [-Wdeprecated]
   #import "utility/uip.h"
    ^
In file included from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPEthernet.h:34:0,
                 from sqlard_test.ino:6:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPServer.h:23:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
 #import "Server.h"
  ^
In file included from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPEthernet.h:34:0,
                 from sqlard_test.ino:6:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPServer.h:24:2: warning: #import is a deprecated GCC extension [-Wdeprecated]
 #import "UIPClient.h"
  ^
In file included from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPUdp.h:23:0,
                 from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/Dhcp.h:7,
                 from sqlard_test.ino:2:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/ethernet_comp.h:5:24: error: redefinition of 'class UIPClient'
 #define EthernetClient UIPClient
                        ^
C:\Program Files (x86)\Arduino\libraries\Ethernet\src/EthernetClient.h:8:7: note: in expansion of macro 'EthernetClient'
 class EthernetClient : public Client {
       ^
In file included from sqlard_test.ino:5:0:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPClient.h:63:7: error: previous definition of 'class UIPClient'
 class UIPClient : public Client {
       ^
In file included from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPUdp.h:23:0,
                 from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/Dhcp.h:7,
                 from sqlard_test.ino:2:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/ethernet_comp.h:6:24: error: redefinition of 'class UIPServer'
 #define EthernetServer UIPServer
                        ^
C:\Program Files (x86)\Arduino\libraries\Ethernet\src/EthernetServer.h:8:7: note: in expansion of macro 'EthernetServer'
 class EthernetServer :
       ^
In file included from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPEthernet.h:34:0,
                 from sqlard_test.ino:6:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPServer.h:26:7: error: previous definition of 'class UIPServer'
 class UIPServer : public Server {
       ^
In file included from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/UIPUdp.h:23:0,
                 from C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/Dhcp.h:7,
                 from sqlard_test.ino:2:
C:\Users\Fer\Documents\Arduino\libraries\arduino_uip-master/ethernet_comp.h:4:18: error: conflicting declaration 'EthernetClass UIPEthernet'
 #define Ethernet UIPEthernet
                  ^
Error de compilación

PaulS

Code: [Select]
//Included by myself!!!
#include <Dhcp.h>
#include <Dns.h>
#include <ethernet_comp.h>
#include <UIPClient.h>
#include <UIPEthernet.h>
#include <UIPServer.h>
#include <UIPUdp.h>
//Included by myself!!!

Why? Why did you stuck a bunch of useless #include statements in your code?
The art of getting good answers lies in asking good questions.

fernando1828

Why? Why did you stuck a bunch of useless #include statements in your code?
Oh, some of them are useless, but I only need one or two. Otherwise, I get another error.

ieee488

I find it fairly straight forward to make an HTTP endpoint and handle the SQL on the server. It also creates an opportunity for a good API.
How about put your money where your mouth is and show some code?

PaulS

Quote
but I only need one or two. Otherwise, I get another error.
Which one(s)? What error?

The compiler is VERY specific. You need to learn to be, too.
The art of getting good answers lies in asking good questions.

fernando1828


PaulS

The compiler asked me for the UIPethernet library.
Show us the code without all the unnecessary #include statements, and exactly how the compiler asked you for the UIPethernet library.

The compiler is I is a crusty old f*ck, and doesn't ask for anything. It simply demands perfection.
The art of getting good answers lies in asking good questions.

fernando1828

#10
Apr 09, 2019, 07:16 pm Last Edit: Apr 09, 2019, 07:18 pm by fernando1828
Quote
Show us the code without all the unnecessary #include statements, and exactly how the compiler asked you for the UIPethernet library.
Code: [Select]

 #include <Ethernet.h>
#include "sqlard.h"


uint8_t Ethernet_MacAddr[6] = { 0x74,0x69,0x69,0x2D,0x30,0x31 }; /* MAC ADRESİ */
static byte Static_IPAddr[] = { 172,16,1,2 };            
static byte Gateway_IPAddr[] = { 172,16,1,1 };
static byte Subnet_Mask[] = { 255,255,0,0 };
EthernetClient client;

SQLard MSSQL(Gateway_IPAddr, 1433, &client);
void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);
  while (!Serial);
  Ethernet.begin(Ethernet_MacAddr, Static_IPAddr, Gateway_IPAddr, Gateway_IPAddr, Subnet_Mask);
  if(MSSQL.connect())
  {
      MSSQL.setCredentials(L"arduino", L"ard_login",L"ard_password",L"hostx");
      MSSQL.login();
  }
  
}

void loop() {
  // put your main code here, to run repeatedly:
  int loop_count = 0;
  delay(5000);

  long affected_rows = MSSQL.executeNonQuery(L"INSERT INTO [dbo].[test]([data]) VALUES('deger1234') ");
  long yeni = MSSQL.executeNonQuery(L"DROP TABLE [dbo].[test23]");
  Serial.print(affected_rows);
  Serial.println(" row(s) affected.");
  if (!client) {
    Serial.println("disconnecting.");
    client.stop();
  }
  if(++loop_count == 10)
  {
    MSSQL.executeNonQuery(L"DELETE FROM [dbo].[test]");
    loop_count = 0;
  }
}



Quote
The compiler is I is a crusty old f*ck, and doesn't ask for anything. It simply demands perfection.
Sure, I'm not perfect, thats why I am here!!!. I am using Arduino 1.6.5.
Error:

Quote
Opciones de compilación cambiadas, reconstruyendo todo
In file included from sqlard_test.ino:2:0:
C:\Users\Fer\Documents\Arduino\libraries\arduino-mssql-master/sqlard.h:13:27: fatal error: UIPEthernet.h: No such file or directory
   #include <UIPEthernet.h>
                           ^
compilation terminated.
Error de compilación

PaulS

Code: [Select]
C:\Users\Fer\Documents\Arduino\libraries\arduino-mssql-master/sqlard.h:13:27: fatal error: UIPEthernet.h: No such file or directory
   #include <UIPEthernet.h>

If you think that that means that you need to add the same single statement to your sketch, what happens when you do that?

What it looks to me like is that you haven't downloaded and/or properly installed the UIPEthernet library.
The art of getting good answers lies in asking good questions.

fernando1828

Code: [Select]
C:\Users\Fer\Documents\Arduino\libraries\arduino-mssql-master/sqlard.h:13:27: fatal error: UIPEthernet.h: No such file or directory
   #include <UIPEthernet.h>

If you think that that means that you need to add the same single statement to your sketch, what happens when you do that?

I got many errors, I've posted them before. (see my post # 2)

What it looks to me like is that you haven't downloaded and/or properly installed the UIPEthernet library.
I think the same, but I don't realice what Im doing wrong.

Thee_Captain

#13
Apr 10, 2019, 02:10 am Last Edit: Apr 10, 2019, 02:54 am by Thee_Captain
How about put your money where your mouth is and show some code?
Challenge accepted.

This is based off the ESP32 BasicHttpClient.ino example sketch. I thinned out some of it and moved the get request to its own function. That way it can be called by specialty "sensor" (anything really) functions.

You could do a similar sketch with Arduino's Ethernet WebClient example.

Code: [Select]
/**
 * BasicHTTPClient.ino
 *
 *  Created on: 24.05.2015
 *
 */

#include <Arduino.h>

#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>

#define USE_SERIAL Serial



WiFiMulti wifiMulti;
const char* ssid = "your_SSID";
const char* pass = "your_password";

char getURI[128] = "http://example.com/index.html";
//char getURI[128] = "http://example.com/flabberwab.html"; //404 Not Found - Test Response
//char getURI[128] = "http://example.com/device/5/sensor/251";


#define SENSOR_PIN 26


void setup() {
    USE_SERIAL.begin(115200);
    delay(3000);
    wifiMulti.addAP(ssid, pass);

    pinMode(SENSOR_PIN, INPUT);

    getRequest(getURI);
    delay(2000);
}

void loop() {
//  getRequest(getURI);
//  delay(5000);
  saveSensorValue();
  delay(5000);
}


void saveSensorValue(){
  char myDeviceEndPoint[128] = "http://example.com/device/5"; //your device endpoint
  int sensorVal = -1;
  char buf[12] = "";
  sensorVal = digitalRead(SENSOR_PIN);
  strcat(myDeviceEndPoint, "/sensor/"); //your sensor endpoint concatenated to your device endpoint
  strcat(myDeviceEndPoint, itoa(sensorVal, buf, 10) );
  getRequest(myDeviceEndPoint);
}



void getRequest(char* requestURI){
    // wait for WiFi connection
    if((wifiMulti.run() == WL_CONNECTED)) {
        HTTPClient http;
        USE_SERIAL.println("");
        http.begin(requestURI);

        USE_SERIAL.print("[HTTP] GET...\n");
        USE_SERIAL.println(requestURI);
       
        // start connection and send HTTP header
        int httpCode = http.GET();

        // httpCode will be negative on error
        if(httpCode > 0) {
            // HTTP header has been send and Server response header has been handled
            USE_SERIAL.printf("[HTTP] Response code: %d\n", httpCode);

            //request made it to the server
            //and has sent back a response
            if(httpCode == HTTP_CODE_OK) {
                String payload = http.getString();
                USE_SERIAL.println(payload);
            }
        } else {
          //Request did not reach the server
          USE_SERIAL.printf("[HTTP] Request failed, error: %s\n", http.errorToString(httpCode).c_str());
        }

        http.end();
    }
}
Throw a little karma my way. What goes around comes around.

fernando1828

Hey thank you very much for the answers, but let's not lose the goal:I want to connect the arduino with SQL directly with the program I've posted before.

Thanks!

Go Up