Pass data From Arduino to PHP

Hi, I hope you can help me with my project, I am working with a coin slot and I managed to monitored the inserted coins in my Serial Monitor but I have a problem on how to transfer the data to a PHP localhost. I hope you can help me. Thanks.

Here's the Arduino Code:

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

LiquidCrystal_I2C lcd(0x3F,16,2);
// Constants

const int coinpin = 2;
const int ledpin = 8;

// Variables

volatile int pulse = 0;
volatile unsigned long timelastPulse = 0;

boolean bInserted = false;
int credits = 0;i
nt price = 20;

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = {192, 168, 1, 11 }; //Enter the IP of ethernet shield
byte serv[] = {192, 168, 1, 10} ; //Enter the IPv4 address
EthernetClient cliente;

void setup() {
Serial.begin(9600);
lcd.begin();
lcd.backlight;
lcd.clear();
Ethernet.begin(mac, ip);
attachInterrupt(digitalPinToInterrupt(coinpin), coinInterrupt, RISING);
displayCredit();

}



void loop() {

unsigned long lastTime = millis() - timelastPulse;

if( (pulse > 0 ) && ( lastTime > 200) ){ // for show LED ON when Inserted
bInserted = false;
credits += pulse;
pulse = 0;
displayCredit();

}

if( credits >= price ){
digitalWrite(ledpin, HIGH);

}
if (cliente.connect(serv, 80)) { //Connecting at the IP address and port we saved before
Serial.println("connected");
cliente.print("GET /sample/data.php?"); //Connecting and Sending values to database

cliente.print("Coins Inserted:");
cliente.print(credits);


cliente.stop(); //Closing the connection
}
else {
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
delay(5000);
}

void coinInterrupt(){

// Each time a pulse is sent from the coin acceptor,

// interrupt main loop to add 1 and flip on the LED

pulse++ ;

timelastPulse = millis();

// digitalWrite(ledpin, HIGH);

// Serial.println( pulse );

}

void displayCredit()

{

 lcd.clear();

 lcd.setCursor(0,0);

 lcd.print("Credit:"); 



 lcd.setCursor(0,1);

 lcd.print( credits );

}

Now here's My PHP code named data.php I haven't figured how can it receive the data from arduino

Coins Inserted:


<p value="<?php $row['credits'] ?> </p> </body> </html> <p>Hope you can help me guys I am trying my best and still can't get the results that I wanted. Thanks.</p>

Hello, do yourself a favour and please read How to get the best out of this forum and modify your post accordingly (including code tags and necessary documentation of your ask).

➜ please edit your post, select the code part and press the </> icon in the tool bar to mark it as code. It's barely readable as it stands. (also make sure you indented the code in the IDE before copying, that's done by pressing ctrlT on a PC or cmdT on a Mac)

1 Like

Yes so sorry I'm new in Arduino Forum also I will edit my post. Thanks

There are two ways to pass data to a PHP web page (well, there are lots of ways, but lets keep it simple)

In your code you are doing a GET on data.php. That is going to request the page from the server and return it the aurdino. So you could add a querystring to the GET. , i.e. data.php?coins=13

Then in the PHP code use $_GET["coins"] to get the value in the PHP code.

The other way is to do a POST and put the variables in the body. This is a better way of doing it if you have a lot of data, but the above should work for you

1 Like