MySQL Forums
Forum List  »  Connector/Arduino

Connector loop my_conn executing before setup
Posted by: Mark Iveson
Date: January 18, 2014 09:06PM

I have a strange issue with an Arduino Uno board that also has a DHT sensor and ehternet.

Essentially if I put any of the my_conn.cmd_query in the loop() then the arduino for some reason executes the my_conn without having a connection setup, causing the arduino to hang as no response is received....

If I comment out the my_conn line in the loop() everything works fine. I need the my_conn.cmd_query as I want to upload temp/humidity ever few minutes to a mysql database. It seems appropriate to use the loop() to insert the data.

Here is the code that I'm using....

#include "DHT.h" // Humidity/Temp Header

// Network related headers
#include <SPI.h>
#include <Ethernet.h>
#include <sha1.h>
#include <stdlib.h>

// SQL Headers
#include "mysql.h"

#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)


byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x03 };
IPAddress server(192,168,0,99); // Supply the IP of the MySQL *server* here

char user[] = "test"; // can be anything but the user must have
char password[] = "1234"; // access rights to connect (host must permit it)

EthernetClient client;
Connector my_conn; // The Connector/Arduino reference

void setup() {

while(!Serial) {

if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet address using DHCP");
Serial.print( "My IP address: " );

for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.println("Connecting to SQL Server:");

if (my_conn.mysql_connect(server,3306,user,password)) {

Serial.println("Connection failed.");



void loop() {

// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h = dht.readHumidity();
float t = dht.readTemperature();
if (isnan(t) || isnan(h)) {
Serial.println("Failed to read from DHT");
Serial.print("Humidity: ");
Serial.print(" %\t");
Serial.print("Temperature: ");
Serial.println(" *C");

// Let's write some data to the PHP Server (yee ha)
// Make sure sql is connected otherwise it will fail


if(my_conn.is_connected()==1) {
//my_conn.cmd_query("insert into values('','',t,h)");
Serial.println("SQL Connected");

The //my_conn.cmd_query if commented out will cause the Arduino to acquire an Ethernet address and successfully connect to SQL. If I leave the line in then for some reason the my_conn.cmd_query causes an unauthenticated logon attempt.

It is almost as if having a my_conn.anthing causes a connect to be attempted.

Very strange and it's been frustrating me for over a week now.

Version is 1.0b - mysql is perfect (I have test multiple logins without any dramas). I'm struggling to determine why commenting out the line causes it to work (however it won't write to the database) and un-commenting causes the unauthorised requests to mysql.

It gives the impression that the loop() is running before the setup() which cannot be right.

Perhaps I'm missing something very obvious....

Thanks for the help


Options: ReplyQuote

Written By
Connector loop my_conn executing before setup
January 18, 2014 09:06PM

Sorry, you can't reply to this topic. It has been closed.

Content reproduced on this site is the property of the respective copyright holders. It is not reviewed in advance by Oracle and does not necessarily represent the opinion of Oracle or any other party.