Connecting to localhost from Arduino - arduino

I want to send data from arduino to my php file running in the xampp server. But it can't connect to localhost. Shortly, client.connect() gives error.
My code:
#include<Ethernet.h>
#include<SPI.h>
byte mac[]= { 0x74, 0x12, 0xB3, 0x1D, 0xF3, 0xC7 };
IPAddress ip(192, 168, 1, 35);
IPAddress server(127, 0, 0, 1);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac,ip);
}
void send(int data) {
char outbuf[128];
if(client.connect(server,80)) { // Problem
Serial.println("Connected successfully");
sprintf(outbuf,"POST /dir/get.php?data=%u' HTTP/1.1", data);
client.print("Host: ");
client.println(server);
client.println("Connection: close");
client.println();
Serial.println();
while(client.connected()) {
while(client.available()) {
Serial.write(client.read());
}
}
}
else {
Serial.println("Cannot connected :(");
}
if(client.connected()) {
client.stop();
}
}
void loop() {
send(1);
delay(2000);
}
I tried some absurd things but they didn't work. (such as making server ip same with client ip, writing 'localhost' instead of 127.0.0.1, and more) I researched it in the arduino forums but couldn't find the solution I want.
What should I do? or Where is my fault?

Related

How to connect to loaclhost with Arduino via ethernet shield?

I need to send data from Arduino to a local server that will run on my computer. The computer is connected to Wi-Fi, Arduino Uno with Eternet Shield via a cable to the router.
I took a standard sketch from the WebClient examples and changed my IPv4 address, which I found through ipconfig. This should fix the problem(as I read here), but the port monitor gives "connection failure". What could be the problem?
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192, 168, 1, 4); // numeric IP for Google (no DNS)
//char server[] = "localhost"; // name address for Google (using DNS)
IPAddress ip(192, 168, 1, 229);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.init(4);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection:
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
// no point in carrying on, so do nothing forevermore:
// 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, 3000)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: localhost");
client.println("Connection: close");
client.println();
}
else {
// kf you didn't get a connection to the server:
Serial.println("connection failed");
}
}
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();
// do nothing forevermore:
while (true);
}
}
Should have disabled the Windows Firewall

Arduino http client not connecting

I have copied from several places portions of code and wrote this program that should connect to a server which I'm 100% sure is working, the IP address and the port are right, however client.connect(server, 8000) returns false, I'm new to networking so it's probably because of something basic
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 192, 168, 1, 100 };
IPAddress ip(192,168,1,127);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
Serial.print("client is at ");
Serial.println(Ethernet.localIP());
if (client.connect(server, 8000)) { //false returned here, of course then it doesn't work
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
} else {
Serial.println("connection failed");
}
}
void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
client.stop();
}
After making it work I forgot about having asked it here, but here's how I made it work, no idea of what was wrong before.
Of course be sure to turn off the firewall, so, for debian based systems:
sudo ufw disable
sudo ufw reload
Then I messed up with the code until I managed to do a POST request, I know I've asked for a GET, but the error mentioned in the question is now gone (I've notice the port changed, but that's because I've changed it on the server as well):
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,1,100);
IPAddress ip(192,168,1,104);
IPAddress gateway(192,168,1,1);
IPAddress DNSServer(192,168,1,1);
IPAddress subnet(255, 255, 255, 0);
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip, gateway, DNSServer, subnet);
delay(5000);
Serial.println("connecting...");
}
void loop()
{
int correctValue;
String PostData = "foo";
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
if (client.connect(server, 5000)) {
Serial.println("connected");
client.println("POST / HTTP/1.1");
// Works either with or without any of the commented lines
// guess it's a great idea to un-comment them though
//client.println("Host: 192.168.1.100");
//client.println("User-Agent: Arduino/1.0");
//client.println("Connection: close");
client.print("Content-Length: ");
client.println(PostData.length());
client.println();
client.println(PostData);
client.println();
}
else {
Serial.println("connection failed");
}
delay(10000);
}
}
If you wanted to do a GET, it SHOULD be enough to change
client.println("POST / HTTP/1.1");
with
client.println("GET / HTTP/1.1");
then remove all the client.println(), the block with
if (client.available()) {
char c = client.read();
Serial.print(c);
}
should print it, since I'm not sure about it and can't confirm it I'll be more than happy to update the code if someone finds out the right way to do this

There are no GPS data after connecting to the server

I'm trying to get GPS data and send it to the server via socket.
I'm using Arduino Uno and GPS/GSM module A7 Ai-Thinker.
And for Internet I use Ethernet shield connected to Wi-Fi router.
But I got trouble when I joined getting GPS data and sending it.
Separately that functions work fine. How I start A7, there is a loop where I type AT commands and send it to A7. About socket, I want to set connection at start function and keep it alive all the time.
But the problem is that when I initialized A7 and set socket connection, I go to the loop function and after first iteration there are no data at A7.
If I try to set connection with server first, and start A7 after it, A7 doesn't react to AT commands.
How can I keep connection with my server and don't lost connection with A7?
Code:
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <SPI.h>
#include <Ethernet.h>
//-------------------------------------------------------
static const int RXPin = 10, TXPin = 11;
static const uint32_t GPSBaud = 4800;
const String START_GPS = "START_GPS";
TinyGPSPlus gps;
SoftwareSerial gps_serial(RXPin, TXPin);
//-------------------------------------------------------
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
char server[] = "192.168.0.100";
int port = 8090;
IPAddress ip(192, 168, 0, 177);
EthernetClient client;
//-------------------------------------------------------
void initGpsConnection();
void startGps();
void initSocketConnection();
void sendData();
void setup()
{
Serial.begin(9600);
Serial.println();
initSocketConnection();
initGpsConnection();
sendData();
}
void loop()
{
while (gps_serial.available() > 0) {
if (gps.encode(gps_serial.read())) {
displayInfo();
//delay(1000);
}
}
if (millis() > 120000 && gps.charsProcessed() < 10)
{
Serial.println(F("No GPS detected: check wiring."));
//while(true);
}
if (!client.connected()) {
Serial.println();
Serial.println("Socket connection has been lost.");
client.stop();
//while (true);
}
}
void displayInfo()
{
Serial.print(F("Location: "));
if (gps.location.isValid())
{
Serial.print(gps.location.lat(), 6);
Serial.print(F(","));
Serial.print(gps.location.lng(), 6);
}
else
{
Serial.print(F("INVALID"));
}
Serial.print(F(" Date/Time: "));
if (gps.date.isValid()) {
Serial.print(gps.date.month() + "/" gps.date.day() + "/" + gps.date.year();
} else {
Serial.print(F("INVALID"));
}
Serial.print(F(" "));
if (gps.time.isValid()) {
Serial.print(gps.time.hour() + ":" + gps.time.minute() + ":" + gps.time.second());
} else {
Serial.print(F("INVALID"));
}
Serial.println();
}
void initGpsConnection() {
Serial.println("In init gps");
gps_serial.begin(GPSBaud);
Serial.println("after gps begin");
delay(1000);
startGps();
Serial.println("GPS connection has been set");
}
void startGps() {
Serial.println("Enter at commands, after that enter `start_gps`");
String response = "";
while (true) {
if (gps_serial.available()) {
Serial.write(gps_serial.read());
}
if (Serial.available()) {
char c = Serial.read();
response += c;
if (response.indexOf(START_GPS) > 0)
break;
gps_serial.write(c);
}
}
}
void initSocketConnection() {
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
Ethernet.begin(mac, ip);
}
delay(1000);
Serial.println("connecting...");
if (client.connect(server, port)) {
Serial.println("Connection with server has been set");
} else {
Serial.println("Connection with server has been failed");
}
}
void sendData(){
client.print("TEst ard");
}
The problem was at A7 connection. It was connected via 10, 11 pins at Ethernet shield. But 10-13 pins are reserved by A7. It's just needed connect A7 vie 8, 9 or others pins.

This code should upload a value to a database through a php file

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();
}
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");
}
}

arduino project of conneting server to run php

he code above is running on sketch on arduino . I do not know if I set the ip and server address wrong
Please help !!!!!
I run on serial monitor to run the code. It is saying
Here is the result
connection....
connection failed
disconnected
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0x91, 0xA2, 0xEA, 0x0D, 0x2B, 0xB1 };
byte ip[] = { 196, 10, 10, 94};
byte server[] = { 196, 10, 10,63};
EthernetClient client;
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server,80)) {
Serial.println("connected");
client.print("GET http://196.10.10.63:89/calls/channel.php HTTP/1.0\n");
client.print("Host: http://196.10.10.63:89");
client.println();
}
else {
Serial.println("connection failed");
}
}
void loop()
{
if (client.available()) {
char c = client.read();
Serial.print(c);
}
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;;)
;
}
}
Your code mentions getting a page from port 89:
client.print("GET http://196.10.10.63:89/calls/channel.php HTTP/1.0\n");
but you connect to port 80:
if (client.connect(server,80)) {
Change the port in the connect() to 89, if that's the right one. Also in the GET request you don't need the full URL, try the path:
client.print("GET /calls/channel.php HTTP/1.0\n");
client.print("Host: 196.10.10.63");

Resources