Go Down

Topic: sending data from arduino to database (Read 421 times) previous topic - next topic

jack13

Jan 12, 2017, 07:28 am Last Edit: Jan 12, 2017, 08:25 am by jack13
I am getting data from arduino and stored in database through ethernet, but only in part (only "buttonState1"). I didn't received data from "buttonState2". Where is my mistake. This is my code:

Code: [Select]
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,4,125);
IPAddress server(169, 254, 4, 124);
EthernetClient client;
const int ledPin1 = 2;
const int ledPin2 = 3;
const int buttonPin1 = 6;
const int buttonPin2 = 7;
int buttonState1 = 0;
int buttonState2 = 0;

void setup(){
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);          
Ethernet.begin(mac, ip); }

void loop(){
buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
    digitalWrite(ledPin1, HIGH); }
  else {
    digitalWrite(ledPin1, LOW); }
buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin2, HIGH); }
  else {
    digitalWrite(ledPin2, LOW); }
    
if (client.connect(server, 80)) {
client.print("GET /get/index.php?");
data(client);
client.println(" HTTP/1.1");
client.print("HOST: ");
client.println(sever);
client.println("Connection: close");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println(); }
while (client.connected()) {
  if (client.available()) {
    char c = client.read(); }
delay(5);
client.stop(); }}
  
void data(EthernetClient cl) {
if (buttonState1 == HIGH) {
  cl.println("id=1&status=1"); }
if (buttonState1 == LOW) {
  cl.println("id=1&status=0"); }
if (buttonState2 == HIGH) {
  cl.println("id=2&status=1"); }
if (buttonState2 == LOW){
  cl.println("id=2&status=0"); }}


Can somebody help me?
Thank you and sorry for my english

Chagrin

#1
Jan 12, 2017, 05:45 pm Last Edit: Jan 12, 2017, 05:46 pm by Chagrin
In your data() subroutine you have "cl.println" but these should be "cl.print".

Make these changes as well: drop the "Host:" and "Connection:" lines and change to HTTP/1.0.

Code: [Select]

client.print("GET /get/index.php?");
data(client);
client.println(" HTTP/1.0");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println();

jack13

Not worked
Code: [Select]
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,4,125);
IPAddress server(169, 254, 4, 124);
EthernetClient client;
const int ledPin1 = 2;
const int ledPin2 = 3;
const int buttonPin1 = 6;
const int buttonPin2 = 7;
int buttonState1 = 0;
int buttonState2 = 0;

void setup(){
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);         
Ethernet.begin(mac, ip); }

void loop(){
buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
    digitalWrite(ledPin1, HIGH); }
  else {
    digitalWrite(ledPin1, LOW); }
buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin2, HIGH); }
  else {
    digitalWrite(ledPin2, LOW); }
   
if (client.connect(server, 80)) {
client.print("GET /get/index.php?");
data(client);
client.println(" HTTP/1.0");
client.println("Content-Type: application/x-www-form-urlencoded");
client.println(); }
while (client.connected()) {
  if (client.available()) {
    char c = client.read(); }
delay(5);
client.stop(); }}
 
void data(EthernetClient cl) {
if (buttonState1 == HIGH) {
  cl.print("id=1&status=1"); }
if (buttonState1 == LOW) {
  cl.print("id=1&status=0"); }
if (buttonState2 == HIGH) {
  cl.print("id=2&status=1"); }
if (buttonState2 == LOW){
  cl.print("id=2&status=0"); }}


PHP code
Code: [Select]
<?php
$con=mysqli_connect('localhost','root','','led');
$id=(isset($_GET['id']) ? $_GET['id']: '');
$status=(isset($_GET['status']) ? $_GET['status']: '');

$sql="UPDATE `mytable1` SET `status`= '$status' WHERE `id`='$id'";
$query=mysqli_query($con,$sql);
$result=mysqli_query($con,"SELECT * FROM `mytable1`") or die ("FAILLED");
mysqli_close($con);
?>


Any idea?
Thanks

Chagrin

Sorry I didn't read your question correctly. But your problems are still in the data() subroutine.

If buttonState1 is high and buttonState2 is high then the output from that routine will be "id=1&status=1id=2&status=2". So already that's a problem; there's no ampersand between "status=1id=2".

The second issue is that you re-use the "id" and "status" names in that query string. In your PHP script when you call $_GET['id'] you're only looking at one value. You need to do a "foreach ($_GET['id'] as $id) { .... }". For more information on this google how to handle "<select name="foo[]" multiple>" form types with PHP. Note the brackets in the name ("foo[]") which is a bit of PHP witchcraft.

I'd recommend restructuring your query string to look like "led1=1&led2=1" or such instead of trying to re-use "id" and "status".

mistergreen

#4
Jan 14, 2017, 05:54 pm Last Edit: Jan 14, 2017, 05:55 pm by mistergreen
You don't need this
client.println("Content-Type: application/x-www-form-urlencoded");

for GET. It's for POST only.
https://code.tutsplus.com/tutorials/http-headers-for-dummies--net-8039

jack13

I'm still having problems. When I'm using this code, no data is entered into the database. led 1 or 2 ON, but in my database still 0.

Code: [Select]
#include <Ethernet.h>
#include <SPI.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(169,254,4,125);
IPAddress server(169, 254, 4, 124);
EthernetClient client;
const int ledPin1 = 2;
const int ledPin2 = 3;
const int buttonPin1 = 6;
const int buttonPin2 = 7;
int buttonState1 = 0;
int buttonState2 = 0;

void setup(){
pinMode(buttonPin1, INPUT);
pinMode(buttonPin2, INPUT);
pinMode(ledPin1, OUTPUT);
pinMode(ledPin2, OUTPUT);         
Ethernet.begin(mac, ip); }

void loop(){
buttonState1 = digitalRead(buttonPin1);
  if (buttonState1 == HIGH) {
    digitalWrite(ledPin1, HIGH); }
  else {
    digitalWrite(ledPin1, LOW); }
buttonState2 = digitalRead(buttonPin2);
  if (buttonState2 == HIGH) {
    digitalWrite(ledPin2, HIGH); }
  else {
    digitalWrite(ledPin2, LOW); }
   
if (client.connect(server, 80)) {
client.print("GET /get/index.php?");
data(client);
client.println(" HTTP/1.0");
client.println(); }
while (client.connected()) {
  if (client.available()) {
    char c = client.read(); }
delay(5);
client.stop(); }}
 
void data(EthernetClient cl) {
if (buttonState1 == HIGH) {
  cl.print("id1=1&status1=1"); }
if (buttonState1 == LOW) {
  cl.print("id1=1&status1=0"); }
if (buttonState2 == HIGH) {
  cl.print("id2=2&status2=1"); }
if (buttonState2 == LOW){
  cl.print("id2=2&status2=0"); }}


Code: [Select]
<?php
$con=mysqli_connect('localhost','root','','led');

$id1=(isset($_GET['id1']) ? $_GET['id1']: '');
$id2=(isset($_GET['id2']) ? $_GET['id2']: '');
$status1=(isset($_GET['status1']) ? $_GET['status2']: '');
$status2=(isset($_GET['status2']) ? $_GET['status2']: '');

$sql="UPDATE tabel_status SET status = CASE id
WHEN 
$id1 THEN $status1
WHEN 
$id2 THEN $status2  
ELSE status END"
;

$query=mysqli_query($con,$sql);
$result=mysqli_query($con,"SELECT * FROM `tabel_status`");
mysqli_close($con);
                
?>



So, I'm using this code. the problem is: Array to string conversion in on line 10 "$status"
Code: [Select]
<?php
$con=mysqli_connect('localhost','root','','led');

$id1=(isset($_GET['id1']) ? $_GET['id1']: '');
$id2=(isset($_GET['id2']) ? $_GET['id2']: '');
$status1=(isset($_GET['status1']) ? $_GET['status1']: '');
$status2=(isset($_GET['status2']) ? $_GET['status2']: '');
$arr = array('$id1','$id2');
$status = array('$status1','$status2');

foreach($arr as $id) {
                
$sql="UPDATE `tabel_status` SET `status`= '$status' WHERE `id`='$id'"; } // line 10

$query=mysqli_query($con,$sql); 
$result=mysqli_query($con,"SELECT * FROM `tabel_status`");
mysqli_close($con);
?>


anyway thank you for your responses...

mistergreen

Also when you contact php pages you can echo out results for debugging. From the arduino, you can listen to the available client for those echo responses.


Go Up
 


Please enter a valid email to subscribe

Confirm your email address

We need to confirm your email address.
To complete the subscription, please click the link in the email we just sent you.

Thank you for subscribing!

Arduino
via Egeo 16
Torino, 10131
Italy