How to send data from Arduino to Odoo? - arduino

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

Related

Arduino send GET request to server on local network

I have a local network composed of 2 devices:
Raspberry Pi running Raspbian with an Apache server. (IP 169.254.90.117)
Arduino MEGA with an Arduino Ethernet Shield. (IP 169.254.90.110)
The 2 devices are directly connected via Ethernet cable.
What I want to do is pretty straight forward: be able to send a GET request from the Arduino to the server hosted on the Raspberry Pi in order to modify records on a database.
Unfortunately every time I try Arduino fails to connect to the server and the serial console shows this:
Initialising...
Trying to connect to server
connection failed
I know that the Raspberry server is working because I tried connecting the Raspberry to a PC from which I was able to navigate with a browser to the website hosted on the server without any issues.
I also tried pinging the Arduino from the Raspberry and the ping is successful.
After navigating through a few forums I tried changing the variable type on the Arduino where I saved the server's IP from a normal char variable to a byte and a IPAddress but with no success.
Here's the Arduino code I've been using:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 169, 254, 90, 110 };
char server[] = "169.254.90.117";
//byte server[] = { 169, 254, 90, 117 };
//IPAddress server(169,254,90,117);
String HTTP_METHOD = "GET";
String PATH_NAME = "/setNumber.php";
String queryString = "?test=26";
EthernetClient client;
void setup() {
Serial.begin(9600);
Serial.println("Initialising...");
Ethernet.begin(mac, ip);
// connect to web server on port 80:
Serial.println("Trying to connect to server");
if(client.connect(server, 80)>0) {
// if connected:
Serial.println("Connected to server");
// make a HTTP request:
// send HTTP header
client.println(HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
Serial.println("Sending GET: " + HTTP_METHOD + " " + PATH_NAME + queryString + " HTTP/1.1");
client.println("Host: " + String(server));
client.println("Connection: close");
client.println(); // end HTTP header
while(client.connected()) {
if(client.available()){
// read an incoming byte from the server and print it to serial monitor:
char c = client.read();
Serial.print(c);
}
}
// the server's disconnected, stop the client:
client.stop();
Serial.println();
Serial.println("disconnected");
} else {// if not connected:
Serial.println("connection failed");
}
}
void loop() {
}
EDIT:
I've updated the char variable to a char[] array but the result is still the same.
To verify that the Arduino Ethernet Shield was working properly I connected it to the internet and succesfully sent a request to www.google.com.
This is the workaround I ended up using:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Mac address of the Arduino board
EthernetClient client;
void setup(){
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to obtaining an IP address using DHCP");
while(true);
}
//Sending request to server
char ServerAddress[] = "domain.name.com";
if(client.connect(ServerAddress, 80)) {
client.println("GET /file.php?data=10 HTTP/1.1");
client.println("Host: " + String(ServerAddress));
client.println("Connection: close");
client.println(); // end HTTP header
client.stop();
} else {// if not connected:
Serial.println("Server connection failed");
}
}
void loop() {
}
Basically I initialised the Ethernet Shield by only declaring the MAC address.
Then I connected the board to a router, so that during the initalisation the router's DHCP would automatically assing an IP address to the Ethernet Shield.
In this way everything starts correctly and I'm able to send GET requests to remote servers.

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

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 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.

Arduino Ethernet Reading from PHP File

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

Resources