Hello, I´m not fully experienced in English so sorry for possible bad language. I have an Arduino Uno + Ethernet Shield W5100 and my code doesn´t do what I want it to do: remote control ON/OFF of two leds from anywhere through Internet, I mean an IoT application.
For do this, I designed an application in Android Studio which has 4 buttons, one for turn ON the first led, one for turn OFF the same led, one for turn ON the second led, and one for turn OFF this second led.
My idea is turn ON or turn OFF two leds at will from this application. First, I press one of the four buttons and send a character to MySQL database hosted in a web hosting (for now, 000webhost, for tests), this character will be:
1 if I press first button
2 if I press second button
3 if I press third button
4 if I press fourth button
I achieve this using a php file uploaded to the hosting.
Then, Arduino retrieve this character and do the action.
My actual code is based in example WebClientRepeating, with this I see in the serial that Arduino can retrieve the character but it doesn´t do the action.
This is my code:
#include <SPI.h>
#include <Ethernet.h>
char valor;
char ledPin5 = 5;
char ledPin6 = 6;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;
char server[] = "creaycontrola.000webhostapp.com";
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
const unsigned long postingInterval = 10L * 1000L; // delay between updates, in milliseconds
// the "L" is needed to use long type numbers
void setup() {
pinMode(ledPin5,OUTPUT);
pinMode(ledPin6,OUTPUT);
// start serial port:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
// give the ethernet module time to boot up:
delay(1000);
Ethernet.begin(mac);
// print the Ethernet board/shield's IP address:
Serial.print("My IP address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
// if there's incoming data from the net connection.
// send it out the serial port. This is for debugging
// purposes only:
if (client.available()) {
char c = client.read();
Serial.write(c);
if (Serial.available()>0){
valor = Serial.read();
if (valor=='1'){
digitalWrite(ledPin5,HIGH);
}
else if (valor=='2'){
digitalWrite(ledPin5,LOW);
}
else if (valor=='3'){
digitalWrite(ledPin6,HIGH);
}
else if (valor=='4'){
digitalWrite(ledPin6,LOW);
}
}
}
// if ten seconds have passed since your last connection,
// then connect again and send data:
if (millis() - lastConnectionTime > postingInterval) {
httpRequest();
}
}
// this method makes a HTTP connection to the server:
void httpRequest() {
// close any connection before send a new request.
// This will free the socket on the WiFi shield
client.stop();
// if there's a successful connection:
if (client.connect(server, 80)) {
Serial.println("connecting...");
// send the HTTP GET request:
client.println("GET /pruebaz.php HTTP/1.0");
client.println("Host: creaycontrola.000webhostapp.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// note the time that the connection was made:
lastConnectionTime = millis();
} else {
// if you couldn't make a connection:
Serial.println("connection failed");
}
}
And this is the serial response (after I pulse the second button in my app):
My IP address: 192.168.1.14
connecting...
HTTP/1.1 200 OK
Date: Fri, 03 Aug 2018 00:37:19 GMT
Content-Type: text/html; charset=UTF-8
Connection: close
Server: awex
X-Xss-Protection: 1; mode=block
X-Content-Type-Options: nosniff
X-Request-ID: afc7fabddc65a7f50c0bcefefab98b5f
2
The another php file I use to recover the character is pruebaz.php:
<?PHP
$conexion = mysqli_connect("xxxxxxxx","xxxxxxxx","xxxxxxxxx","xxxxxxxx");
$consulta = "SELECT valor FROM botones WHERE tiempo=(SELECT max(tiempo) FROM botones)";
$resultado = mysqli_query($conexion,$consulta);
if (mysqli_num_rows($resultado)>0) {
$row = mysqli_fetch_assoc($resultado);
echo "" .$row["valor"]. "";
}
?>
When I execute it in the browser (https://creaycontrola.000webhostapp.com/pruebaz.php), it shows me just the character: 2
Looks like the problem is in the Arduino code. Hope you can help me. Thanks for your time reading this.