Arduino Ethernet Reading from PHP File - arduino

I am writing a simple Arduino Ethernet program. The program sends an HTTP GET Request to the server and then the server echos "Hello World" and I should be able to receive it through the Arduino Ethernet and print it on the Serial monitor of the Arduino 1.0.4 IDE. Here are some useful information. I am using a XAMPP Server on Windows Server 2003. I have placed my PHP file in /xampp/htdocs/xampp and the file name is rec.php. The contents of rec.php is
<?php
echo "Hello World";
?>
This is the file content of the Arduino program
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0x90, 0xA2, 0xDA, 0x00, 0x7E, 0xAE}
IPAddress server { 192, 168, 1, 223 };
IPAddress ipAddress { xxx,xxx,xxx,xxx };
IPAddress myDNS {8,8,8,8};
IPAddress myGateway{192,168,1,1};
IPAddress mySubnet{255,255,255,0};
EthernetClient client;
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ipAddress, myDNS, myGateway, mySubnet);
delay(1000);
Serial.println("connecting");
if(client.connect(server, 80))
{
Serial.println("Connected");
client.println("GET /rec.php HTTP/1.1");
}
else
Serial.println("Not Connected");
}
void loop()
{
if(client.available())
{
char c = client.read();
Serial.println(c);
delay(1000);
}
else
{
Serial.println("Not Available");
delay(1000);
}
}
After I load the program on the Arduino I get this message on the Serial Monitor "HTTP/1.1 400 Bad Request". Any suggestion on how to solve that problem? and please keep your answers simple.

You are not sending the necessary line ends. The protocol requires a CR-LF at the end of the request method and another CR-LF at the end of the full request that can include other header lines. See:
HTTP requests
This means in your case you need two CR-LF's to terminate the request. Don't rely on default println function. Take control of the line ends in your code with print:
client.print("GET /rec.php HTTP/1.1\r\n\r\n");

Related

How to send data from Arduino to Odoo?

I have set up an Arduino with some sensor and I am reading those data. Now I want to send it to odoo. My best guess is that I did something wrong with this,
client.connect(server, 4404)
My current arduino code,
#include <SPI.h>
#include <Ethernet.h>
// all vars are dummy
byte mac[] = { 0xAA, 0xAA, 0xAA, 0xAA, 0xAA, 0xAA };
IPAddress server(198, 11, 11, 11);
IPAddress ip(192, 11, 11, 11);
char HOST_NAME[] = "https://xyz-test200.odoo.com";
EthernetClient client;
String PostData = "https://xyz-test200.odoo.com/xmlrpc/2/common/POST/'x_1'='5.00'&'x_2'='myfvrtstring'";
void setup() {
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect.
}
Ethernet.begin(mac, ip);
delay(1000);
Serial.println("connecting...");
// we need server and port
// in odoo doc the server is mentioned, but I am not sure about port number
// I put 4404 as it should be the default for xmlrpc
if (client.connect(server, 4404)) {
Serial.println("connected");
client.println("POST /xmlrpc/common HTTP/1.1");
client.println("Host: 192.168.1.2:8169");
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");
}
}
Here is the documentation for Odoo Rest API if anyone is interested and I tried it with python script, it works like a charm!
When connecting to an HTTPS server, you need an HTTPS-ready client.
HTTPS has port 443. You are currently using 4404.
If you host odoo yourself, you could create an HTTP endpoint for internal use. And then, I would also recommend creating a custom controller for more uncomplicated communication because RPC is a bit hard to do in a microcontroller.
https://forum.arduino.cc/t/ethernet-post-data-via-https/584139/3

Unstable Arduino Web Server

I have been trying to setup a web server using Arduino. I have an UNO and a HanRun HR91105A I got off the internet, and I am using a modified version of the WebServer example to test my code. It did in fact work at first. But after setting up port forwarding, the connection suddenly became unstable. It connects and works for a few minutes, then suddenly I can't even ping it. Trying to ping the Arduino results in request time out. Research online suggests 2 possibilities:
1.) All the RAM is used up
2.) Ethernet shield is faulty
Below is my code
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x44, 0x00, 0x10, 0x20, 0x8C, 0x0A
};
IPAddress ip(192,168,1,90);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(8081);
void setup() {
// 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 and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
if (c == '\n' && currentLineIsBlank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println("Refresh: 2");
client.println();
client.println("<!DOCTYPE HTML>");
//-----------------Type in outputs below-------------------------------------
client.println("<html>");
client.print("Hello World!");
client.print("<p id='Header'>");
client.print("Sensor Data");
client.println("</p>");
client.print("<p id='Pressure'>");
client.print("Pressure:");
client.println("</p>");
client.print("<p id='Acceleration'>");
client.print("Acceleration:");
client.println("</p>");
client.println("<br /)");
client.println("</html>");
break;
//-----------------End of outputs--------------------------------------------
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
}
}
Additionally, the arduino does have a static IP, so I'm pretty sure it is not an issue of DHCP lease expiry.
I highly suspect that the shield is faulty as it gets very hot while in operation. Plus it IS a knockoff. But I can't dismiss the possibility that my coding is just inefficient as I am not very experienced. Any help would be appreciated. Thanks.
Your Arduino will opperate unpredictably when RAM is not enough.
In this code, there are many constant string. you should store those strings in FLASH memory to save RAM. In order to to so, use F() macro. For example.
client.println(F("HTTP/1.1 200 OK"));
If your program is big, I recommend to use the shield that equips an embedded web sever (e.g PHPoC Shield)

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 Ethernet parsing XML

I'm using an Arduino Uno and ethernet shield to pull xml data from OpenWeatherMap.org. I have the api link and a key, and it all works fine on the web browser. My code is below, however when I get to reading the response from the GET command, all i get is "ΓΏ", not anything I would expect.
I can connect to the arduino when using it as a server, and I can ping it from command prompt so I know it is on the network and the board is not at fault, so it must be something with my code. I have followed the tutorial on the arduino site and loaded the example to GET data, but this also does not work, all I get is a "did not connect" message.
Can someone please look over my code and see what I am doing wrong?
EDIT: I have added the extra loop to print all the xml data, however the program still gets stuck on the while(!client.available());. If i comment it out, I get to the "waiting for server response" but never any further than that. I have checked that the arduino si on the same subnet mask as all the other devices in the network.
// Based on:
// Read Yahoo Weather API XML
// 03.09.2012
// http://forum.arduino.cc/index.php?topic=121992.0
//
#include <SPI.h>
#include <Ethernet.h>
#include <TextFinder.h>
int cityID=2644487; //Lincoln, UK
byte mac[] = {0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02};
byte ip[] = {192, 168, 1, 89};
byte gateway[] = {192, 168, 1, 254};
byte subnet[] = {255, 255, 255, 0};
//Open weather map xml
char server[] = "http://api.openweathermap.org";
int port = 80; //usually 80 for http.
char APIkey[33] = "HIDDENAPIKEY";
EthernetClient client;
char temperature[30];
void setup()
{
pinMode(10, OUTPUT);
digitalWrite(10,HIGH);
Serial.begin(9600);
Serial.println("Initialising...");
// Start Ethernet
if(!Ethernet.begin(mac)){//if DHCP does not automatically connect
Serial.println("Invalid Connection");
}
Serial.println("");
Serial.print("Connecting to OWM server using cityID: ");
Serial.println(cityID);
Serial.print("Using API key: ");
Serial.println(APIkey);
if (client.connect(server,port))
{
client.println("GET /data/2.5/weather?id=2644487&appid=HIDDENAPIKEY&mode=xml&units=metric HTTP/1.1");
client.println("HOST: api.openweathermap.org");
client.println();
Serial.println("Connected to XML data.");
while(!client.available()); //wait for client data to be available
Serial.println("Waiting for server response...");
while(client.available()){
char c = client.read();
Serial.println(c);
}
}
}
void loop()
{
}
For start, your Host header is wrong, use only domain name:
client.println("HOST: api.openweathermap.org");
Second problem I see here is that you do not read entire response from server but only one char.
You have to wait for response data to be available, after that read all the data and parse it or print at start.
This part is wrong:
char c = client.read();
Serial.println(c);
Should be something similar to:
while (!client.available()); //Wait client data to be available
while (client.available()) { //Print all the data
char c = client.read();
Serial.println(c);
}

arduino Ethernet ENC28J60

Hi I am absolute newbee using ENC28J60, I want to upload some data to my seerver (in php) :
I take a php hosting from 0fees and now I can send the data to my server using : http://ashutest123.0fees.us/dataupload1.php?data=somedata and check the uploaded list of data in the table as http://ashutest123.0fees.us/showdata.php β€”β€”β€” u can take a view yourself.
I wrote an arduino code (using Aruino Uno and ENC28J60 module from ebay.in) using UIPEthernet lib
#include <UIPEthernet.h> // Used for Ethernet
// **** ETHERNET SETTING ****
// Arduino Uno pins: 10 = CS, 11 = MOSI, 12 = MISO, 13 = SCK
// Ethernet MAC address - must be unique on your network - MAC Reads T4A001 in hex (unique in your network)
byte mac[] = { 0x54, 0x34, 0x41, 0x30, 0x30, 0x31 };
// For the rest we use DHCP (IP address and such)
EthernetClient client;
char server[] = "ashutest123.0fees.us"; // IP Adres (or name) of server to dump data to
int interval = 5000; // Wait between dumps
void setup() {
Serial.begin(9600);
Ethernet.begin(mac);
Serial.println("Tweaking4All.com - Temperature Drone - v2.0");
Serial.println("-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-\n");
Serial.print("IP Address : ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask : ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway IP: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS Server IP : ");
Serial.println(Ethernet.dnsServerIP());
}
void loop() {
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("-> Connected");
// Make a HTTP request:
client.print( "GET /dataupload1.php?");
client.print("data=");
client.print( "somedata" );
client.println( " HTTP/1.1");
client.print( "Host: " );
client.println(server)
client.println( "Connection: close" );
client.println();
client.println();
client.stop();
}
else {
// you didn't get a connection to the server:
Serial.println("--> connection failed/n");
}
delay(interval);
}
when I run it in serial monitor it shows all ip, dns, gateway etc addresses as 0.0.0.0 ---- seems no dhcp abtained
then shows "connected"
no data goes to my server
Please help me I am in a need of it
Thanks in advance
You might want to try this example code, it might help you alot to understand your problem and where it come from.
#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[] = {
0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02
};
// 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() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
// this check is only needed on the Leonardo:
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");
// no point in carrying on, so do nothing forevermore:
for (;;)
;
}
// print your local IP address:
printIPAddress();
}
void loop() {
switch (Ethernet.maintain())
{
case 1:
//renewed fail
Serial.println("Error: renewed fail");
break;
case 2:
//renewed success
Serial.println("Renewed success");
//print your local IP address:
printIPAddress();
break;
case 3:
//rebind fail
Serial.println("Error: rebind fail");
break;
case 4:
//rebind success
Serial.println("Rebind success");
//print your local IP address:
printIPAddress();
break;
default:
//nothing happened
break;
}
}
void printIPAddress()
{
Serial.print("My IP address: ");
for (byte thisByte = 0; thisByte < 4; thisByte++) {
// print the value of each byte of the IP address:
Serial.print(Ethernet.localIP()[thisByte], DEC);
Serial.print(".");
}
Serial.println();
}
Then I let you edit your code with your custom functions to get all informations you might need.
Note that if you set an hard-coded IP address even in a DHCP range it will be working.
V.

Resources