Hi all and thank you in advance for any help received.
I'm trying to get the output from an electronic dartboard posted to MySQL via php. I am new to Arduino and C++, so have been reading, learning and copying code as I go, but after several days of fruitless effort I gave in and decided to post on the forum. I assume it's something simple I just haven't got my head around yet. While I've tried to be careful there's more than likely some junk in the code after all my trial and error testing.
I have the code reading the dartboard OK, as well as ethernet and triggering the php to MySQL. The problems I've been bashing my head against are:
1.The code creates the php request indefinitely, so fills up the database
2. The php mostly doesn't get what is shown by Serial.print
Code:
/*
From https://www.hackster.io/ricardo-alves/opendarts-homemade-dartboard-machine-2a2914
*/
//Import LIbraries
#include <SPI.h>
#include <Ethernet.h>
#include <stdio.h>
//Setup dartboard matrix
int masterLines(8); //Change here to the number of lines of your Master Layer
int slaveLines(8); //Change here to the number of lines of your Slave Layer
int matrixMaster[] = {52, 50, 48, 46, 44, 42, 40, 38}; //Put here the pins you connected the lines of your Master Layer
int matrixSlave[] = {39, 37, 35, 33, 31, 29, 27, 25}; //Put here the pins you connected the lines of your Slave Layer
char masterBuffer[1];
int result;
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
// Enter the IP address and network details for Arduino, as mentioned we will use 192.168.0.16
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192,168,1,177);
IPAddress subnet(255,255,255,0);
IPAddress gateway(192,168,1,1);
IPAddress dnsserver(192,168,1,1);
IPAddress server(192,168,1,55); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie. "www.yourwebpage.com")
// Initialize the Ethernet server library
EthernetClient client;
void setup() {
// Serial.begin starts the serial connection between computer and Arduino
Serial.begin(9600);
// start the Ethernet connection
Ethernet.begin(mac,ip,dnsserver,gateway,subnet);
Serial.print(ip);
Serial.println("");
//Start dart collection
Serial.println("OpenDarts"); //This line is not necessary, is just here for debug purposes
for(int i = 0; i < slaveLines; i++){
pinMode(matrixSlave[i], INPUT_PULLUP);
}
for(int i = 0; i < masterLines; i++){
pinMode(matrixMaster[i], OUTPUT);
digitalWrite(matrixMaster[i], HIGH);
}
}
void loop() {
for(int i = 0; i < masterLines; i++){
digitalWrite(matrixMaster[i], LOW);
for(int j = 0; j < slaveLines; j++){
if(digitalRead(matrixSlave[j]) == LOW){
// Serial.print(j);
// Serial.print(",");
// Serial.println(i);
sprintf(masterBuffer, "%d%d", j, i); // combine the dartboard reading, passed into php value=
Serial.println(masterBuffer);
delay(300);
break;
}
}
digitalWrite(matrixMaster[i], HIGH);
}
MySQLLoop();
}
void MySQLLoop(){
// Connect to the server (your computer or web page)
if (client.connect(server, 8081)) {
client.print("GET /darts/insert_arduino.php?"); // This is the php page with the mysql script
client.print("value="); // This creates the variable for the php page/
client.print(masterBuffer); // We are making a GET request just like we would from our browser but now with live data from the board
client.println(" HTTP/1.1"); // Part of the GET request
client.println("Host: 192.168.1.55"); // IMPORTANT: If you are using XAMPP you will have to find out the IP address of your computer and put it here (it is explained in previous article). If you have a web page, enter its address (ie.Host: "www.yourwebpage.com")
client.println("Connection: close"); // Part of the GET request telling the server that we are over transmitting the message
client.println(); // Empty line
client.println(); // Empty line
client.stop(); // Closing connection to server
}
else {
// If Arduino can't connect to the server (your computer or web page)
Serial.println("--> connection failed\n");
}
// Give the server some time to recieve the data and store it. I used 0.3 seconds here. Be advised when delaying. If u use a short delay, the server might not capture data because of Arduino transmitting new data too soon.
delay(200);
}
What I see in the Apache access logs for what Serial.print shows as 63:
192.168.1.177 - - [07/Nov/2018:19:33:36 +1100] "GET /darts/insert_arduino.php?value=6#\xd0O HTTP/1.1" 400 966 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:36 +1100] "GET /darts/insert_arduino.php?value=6.\xa9P HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:36 +1100] "GET /darts/insert_arduino.php?value=6${Q HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:36 +1100] "GET /darts/insert_arduino.php?value=6\x1aMR HTTP/1.1" 400 966 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:37 +1100] "GET /darts/insert_arduino.php?value=6\x1c#S HTTP/1.1" 400 966 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:37 +1100] "GET /darts/insert_arduino.php?value=6\x1b\xf8S HTTP/1.1" 400 966 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:37 +1100] "GET /darts/insert_arduino.php?value=6D\xdbT HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:37 +1100] "GET /darts/insert_arduino.php?value=6O\xb4U HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:38 +1100] "GET /darts/insert_arduino.php?value=6H\x87V HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:38 +1100] "GET /darts/insert_arduino.php?value=6AZW HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:38 +1100] "GET /darts/insert_arduino.php?value=6R5X HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:38 +1100] "GET /darts/insert_arduino.php?value=6N Y" 400 966 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:38 +1100] "GET /darts/insert_arduino.php?value=6J\xddY HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:39 +1100] "GET /darts/insert_arduino.php?value=6F\xb1Z HTTP/1.1" 200 858 "-" "-"
192.168.1.177 - - [07/Nov/2018:19:33:39 +1100] "GET /darts/insert_arduino.php?value=6\x16\xa1[ HTTP/1.1" 400 966 "-" "-"