I would like my arduino to send a get request to a uri I have on my local network. I can open a browser and manually paste the request in and it works. I have also made the same request on a webpage using jQuery again no problem.
I have followed the tutorials for the Arduino and I can't see what I am doing wrong if some could help would be very grateful.
#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, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress server(192,168,0,34);
// Set the static IP address
IPAddress ip(192,168,0,177);
// Initialize the Ethernet client library
EthernetClient client;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
Ethernet.begin(mac, ip);
// give the Ethernet shield a second to initialize:
delay(1000);
}
void loop()
{
Serial.println("connecting...");
if (client.connect(server, 81)) {
Serial.println("connected");
client.print("GET /tenHsServer/tenHsServer.aspx?t=ab&f=ToggleDevice&d=");
client.print("E3");
client.println(" HTTP/1.1");
client.println("Host: 192.168.0.34");
client.println(""); //mandatory blank line
}
}
For this kind of issue you should learn about web debbugging tools. My favourite is fiddler2. However it is only available for windows. For other plattforms search the net for http debug proxy. Besides that there is always wireshark or ncat.
Once you have such a tool in place just have a look at the generated traffic and compare it with traffic that actually worked. This usually allows to pinpoint the issue rather quickly.
Related
So I’ve got an Arduino Uno and the Ethernet Shield V2 and connected these to a temperature sensor. Everything is working fine, the temperature is shown as desired, the problem is that I don’t seem to be able to save the results in my influxDB database.
Here my sketch:
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
const int sensorPin = A0;
int sensorVal;
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED
};
byte host = {192, 168, 0, 153};
unsigned int port = 8089; // local port to listen on
EthernetUDP Udp;
void setup(){
//////////////////////
// PUT YOUR SETUP CODE HERE TO RUN ONCE
//////////////////////
Serial.begin(9600); // open serial port
Ethernet.begin(mac, host);
// 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
}
// Check for Ethernet hardware present
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
Serial.println("Ethernet shield was not found. Sorry, can’t run without hardware. :(");
while (true) {
delay(1); // do nothing, no point running without Ethernet hardware
}
}
if (Ethernet.linkStatus() == LinkOFF) {
Serial.println(“Ethernet cable is not connected.”);
}
// start UDP
Udp.begin(port);
}
float getTemperature() {
sensorVal = analogRead(sensorPin);
float voltage = (sensorVal/1024.0) * 5.0;
float temperatureC = (voltage - 0.5)*100;
return temperatureC;
}
void loop(){
//////////////////////
// PUT YOUR MAIN CODE HERE; TO RUN REPEATEDLY
//////////////////////
String line, temperature;
delay(1000);
temperature = String(getTemperature(), 2);
Serial.println(temperature);
line = String(“temperature value=” + temperature);
Serial.println(line);
Serial.println(“Sending UDP packet…”);
Udp.beginPacket(host, port);
Udp.print(“temperature value=”);
Udp.print(temperature);
Udp.endPacket();
}
These are the setting from the config file of the influxDB:
[[udp]]
enabled = true
bind-address = “:8089”
database = “arduino”
retention-policy = “”
InfluxDB precision for timestamps on received points ("" or “n”, “u”, “ms”, “s”, “m”, “h”)
precision = “s”
I would appreciate if somebody could give me some clues about what I’m doing wrong.
Cheers
According to the documentation, you want to pass a remote IP (not local IP) when you call Udp.beginPacket.
Does host represent a remote IP address? Looks like you are using host as a local IP to begin Ethernet as well. It is likely that you are not sending packets to a remote host. Make sure you pass a local IP to Ethernet.begin() and pass a remote IP to Udp.beginPacket().
I've recently bought an Arduino ethernet shield but couldn't get it work.
I've tried to use the example's code but it did not work. I have even tried to get a static IP without DHCP and it's always the same problem .
I've used this code :
#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[] = { 0x90, 0xA2, 0xDA, 0x0F, 0xE1, 0xBF };
// if you don't want to use DNS (and reduce your sketch size)
// use the numeric IP instead of the name for the server:
//IPAddress server(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.google.com"; // name address for Google (using DNS)
// Set the static IP address to use if the DHCP fails to assign
IPAddress ip(192,168,1,10);
// 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);
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, 80)) {
Serial.println("connected");
// Make a HTTP request:
client.println("GET /search?q=arduino HTTP/1.1");
client.println("Host: www.google.com");
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);
}
}
and I got this answer:
Failed to configure Ethernet using DHCP
connecting...
connected
disconnecting.
How can I be sure that my ethernet shield is working correctly and how can I resolve this problem?
With a failed DHCP you don't have DNS address to resolve "www.google.com".
Modify your code like this and try again :
IPAddress server(74,125,232,128);
//char server[] = "www.google.com";
I have been playing around with an arduino ethernet shield, trying to get basic examples to work, to no avail. Here is my setup:
The Arduino Mega 2560 is connected to the computer via usb and the ethernet shield is stacked upon it. I have tried many variations of the examples that come with the arduino software, and none seemed to work properly.After lots of debugging with wireshark, I figured that:
I can't use DHCP, because it just hangs at the Ethernet.begin(mac) call.
When I try with a static ip, the Ethernet.localIP() function returns 0.0.0.0. However, I can ping my device from my computer using the ip I have set and the device seems to receive and send packets properly.The problem now is that for some reason it drops the tcp connections.E.g here is the code I run that comes the closest to working:
#include <SPI.h>
#include <Ethernet.h>
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,2,27);
IPAddress server(192,168,2,52);
EthernetClient client;
void setup() {
// start the Ethernet connection:
Ethernet.begin(mac, ip);
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("a");
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 23)) {
Serial.println("connected");
}
else {
Serial.println("connection failed");
}
Serial.println(Ethernet.localIP());
}
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);
}
// as long as there are bytes in the serial queue,
// read them and send them out the socket if it's open:
while (Serial.available() > 0) {
char inChar = Serial.read();
if (client.connected()) {
client.print(inChar);
}
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing:
while(true);
}
}
Its basically the Ethernet/TelnetClient example.
I have set up a telnet server on my computer. Now this is the arduino/computer exchange:
The arduino sends a RST packet, but my server goes on to send it the greeting and login prompt.
I have tried the same with an arduino uno, and have also tried disconnecting the usb and using another power supply.
So, what could be the issue?
The problem is in the connection to the shield, sometimes this shield if it is chinese version, It might have small short-circuit.
I tried disconnect the shield and connect with wire as like as arduino website indicate for arduino uno (the connections with arduino mega aren't correct, so you need connect like arduino uno)
If it don't work , try change the arduino shield. I have a similar problem with the same shield , and normally the problem is the connection to the arduino.
The example should work if the arduino is correctly connected
Strangely, I found that Ethernet fails to initialize if an SD card is inserted and not initialized. So, either take the SD card out, or initialize the SD card:
Sd2Card card;
card.init(speed, pinSelect);
I have some Chinese (SunFounder) version of Ethernet Shield, so it might not be relevant to cards from other manufacturers.
I have been searching around for this problem for a couple of days but still do not find an answer.
I am trying to make a simple Webclient connection with the arduino shield based on the sample code provided by Arduino IDE. Here is a simplified version of what I am trying to execute:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte server[] = { 173 ,194, 46, 34 }; // Google
EthernetClient client;
void setup()
{
Ethernet.begin(mac);
Serial.begin(9600);
delay(1000);
Serial.println(Ethernet.localIP());
Serial.println("connecting...");
if (client.connect(server, 80)) {
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(;;)
;
}
}
and always get the answer:
192.168.0.103
connecting...
connection failed
disconnecting.
That means that client.connect(server, 80) is failing. I have tried several IP addresses and same results. The shield is working properly as I have tried the WebServer example and that seems to work flawlessly.
PS on hardware: I am using Arduino UNO R3 and ethernet shield based on W5100
Any suggestions?
I took a look at the source code of the Ethernet library, assuming that you have a recent version of the libraries. It seems to me that Arduino EthernetClient connect() function wants either an IPAddress object or a string (char *) with the name of the remote host. You are passing a byte array to it, and my guess is that it probably interprets it as a string.
Try to declare the server global variable as follows instead:
IPAddress server(173 ,194, 46, 34);
If it works, then it is an indication that the official documentation, from which you probably took the code, is obsolete.
Also, you could try giving to the begin() function all the other parameters as IPAddress objects, so that DHCP is not used and you can rule out problems of automatic configuration. The prototype is:
void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet);
sorry for the late reply.
Just add delay of 6 seconds before call to client.connect()
so it takes time to initialize ethernet shield....
-by experience
Well, my solution was to put all the configuration by myself; google dns(8.8.8.8), gateway, subnet, ip. and I think that the main trick was to have a sweet delay after the Ethernet.begin. I give a 3000 delay so the connection could be established fine and hands'on...
I managed to come with a workaround. It seems client.connect only fails the first time it is called. So I added a dummy call after the 1 second delay (before the real call is made).
This does not answer the question, but it does solve the problem. Any feedback on why this is happening is welcome.
delay(1000);
client.connect(server, 80); // Dummy call
I faced the similar problem with my client code until i figured out that it was my antivirus's firewall which was blocking arduino's client to connect.
I added an exception in my antivirus and now it works fine.
My Arduino web server sketch sporadically fails on:
EthernetClient client = server.available();
if (client)
This morning, it connected just fine on the first run. Now, it can't connect to the client again. A couple of days ago, it worked several times, but failed several time as well. I have the shield connected via an Ethernet cable to my home router. I've verified the IP address assigned to the Arduino. I've tried ports 80 and 8080. What could be going wrong and what else can I try? Could my ISP be blocking something here? Please don't be afraid to suggest the obvious, since I know almost nothing about networks.
If relevant, here is a larger piece of the code, which loops on
Serial.println("Listening");
Code:
#include <SPI.h>
#include <Ethernet.h>n
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = { 0x90, 0xA2, 0xDA, 0x00, 0xF7, 0x99 };
IPAddress ip(192,168,2,5);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String roundOpenTag = "";
String roundCloseTag = "";
void setup()
{
// Start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
int ledPin = 8;
// Initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
Serial.println("Setting up");
}
void loop()
{
// Listen for incoming clients
EthernetClient client = server.available();
Serial.println("Listening");
if (client)
{
Serial.println("Server available");
// An HTTP request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
Serial.println("Client connected");
if (client.available())
{
char c = client.read();
I don't see the purpose in including the rest of the sketch. I really appreciate your help.
You have a empty Seiral.begin() in your setup() function. Try removing it.
Edit:
When you call Serial.begin() you have to provide the baut rate(speed) at which you want to communicate. You can read more about the function at Arduino library page.
You had two problems in your code
You had a empty Serial.begin() function call, without any parameter
You had duplicate Serial.begin() function. You had already specified it in the beginning of the setup() function.