-2

I'm using an arduino and an Ethernet shield to upload data to a server. Lately i changed from using a local database to use a web hosting service (000webhost) but i can't make it work, no errors are shown in the Arduino IDE but it just stops in the line where it says "MAKING INSERTION".

Everything was working fine when i had the database locally.

When i enter the url directly into the browser mythesisinacap.000webhostapp.com/writemydata.php?value=0 it works fine inserting the apropriate value into the database...so that means that there's nothing wrong with the php file in the server.

Here's my code.

#include <Ethernet.h>

int isparked;

byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};

// Enter the IP address for Arduino
// Be careful to use , insetead of . when you enter the address here
IPAddress ip(192, 168, 0, 170);

int vcc = 5; //attach pin 2 to vcc
int trig = 6; // attach pin 3 to Trig
int echo = 7; //attach pin 4 to Echo
int gnd = 8; //attach pin 5 to GND

char server[] = "mythesisinacap.000webhostapp.com";

// Initialize the Ethernet server library
EthernetClient client(80);

void setup() 
{

isparked=0;

// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
}

void loop() {

if (client.connect(server, 80))
      {
        Serial.print("CONNECTED");

        Serial.println();
        Serial.print("MAKING INSERTION");
        Serial.println();
        client.print("GET /writemydata.php?value=");
        client.print(isparked5);

        client.println(" HTTP/1.1");
        client.println("Host: mythesisinacap.000webhostapp.com"); 
        client.println("Connection: close"); 
        client.println();
        client.println(); 
        client.stop();    
      }
      else
      {
        Serial.print("NO CONNECTION");
      }
    }
  }
}
}
Serial.println();
Serial.print("FINNISH LOOPING");
Serial.println();
}
  • Please read [Why is "Can someone help me?" not an actual question?](https://meta.stackoverflow.com/questions/284236/why-is-can-someone-help-me-not-an-actual-question) and [ask]. Try to do it yourself. Once you hit a wall, you can explain what you are struggling with and you'll most likely get help. But no one will just help you translate some code on demand – Piglet Aug 13 '17 at 08:32
  • you're right sorry – Medina Nualart Martin Aug 15 '17 at 00:41
  • does the mac address need to be changed maybe with the one on my ethernet shield?, even though it worked with no problem without changing the mac address when i had a local database. – Medina Nualart Martin Aug 15 '17 at 16:06
  • Please provide a [mcve]. No one wants to crawl through code of which 95% is not related to the actual problem. and of course you should use the MAC of your ethernet shield. – Piglet Aug 15 '17 at 16:15
  • Piglet i don't know how can i provide a more complete code than what its shown in my example, please tell me what kind of information you would need to help me troubleshoot the problem, i have no problem to provide the information needed, about the MAC thing: why it worked without changing the MAC address when i had a local database? – Medina Nualart Martin Aug 15 '17 at 16:30
  • not more complete,the focus is on minimal -> less code. that's also how you should debug your code. remove everything that is not related to your problem. no one cares about your calculations and 1000 else if statements. if your client.print doesn't do what you expect it to do, create a simple program that only initializes the ethernet stuff and calls that line straight away. you don't have to calculate isparked5. just send a hardcoded value... you should also learn how to structure your code by using functions. – Piglet Aug 15 '17 at 16:38
  • Done, i edited the code and delete all of the sensor and calculation part. – Medina Nualart Martin Aug 15 '17 at 17:00

1 Answers1

0

Ok i finally got it to work with my web hosted database, i used this example from github and adapted it to my case, now i'll have to add my sensor logic and calculation.

https://github.com/adafruit/Ethernet2/blob/master/examples/WebClient/WebClient.ino

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

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = { 0xDE, 0xDD, 0xBE, 0xEF, 0xFE, 0xED };

char server[] = "mythesis2017.000webhostapp.com";    // name address for Google (using DNS)

// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192, 168, 0, 177);

// Initialize the Ethernet client library
// with the IP address and port of the server
// that you want to connect to (port 80 is default for HTTP):
EthernetClient client;

void setup()
{

}

void loop()
{
// if there are incoming bytes available
// from the server, read them and print them:
if (client.available()) {
char c = client.read();
Serial.print(c);
}

// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();

delay(10000);
insert();
}
}

void insert()
{
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}

// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// try to congifure using IP address instead of DHCP:
Ethernet.begin(mac, ip);
}
// give the Ethernet shield a second to initialize:
delay(1000);
Serial.println("connecting...");

// if you get a connection, report back via serial:
if (client.connect(server, 80)) 
{
Serial.println("connected");
// Make a HTTP request:
client.println("GET /writemydata.php?val=1 HTTP/1.1");
client.println("Host: mythesis2017.000webhostapp.com");
client.println("Connection: close");
client.println();
}
else 
{
// if you didn't get a connection to the server:
Serial.println("connection failed");
}
}
  • `if (client.connect(server, 80))` is still non-sense as the function only returns numbers 1, -1, -2, -3 or -4. So you will always enter that if-block which should only be entered if the function returns 1. All other return values are connection errors. I suggest you make a little function that will print the different error messages. – Piglet Aug 16 '17 at 16:11