I have a uvicorn fastapi server that runs a file on port 8000 at address "example.com/sub/subsub" that returns either a 0 or a 1.
I also have an arduino running that tries to connect to this url and work with this binary value. I can't figure out why my code isn't working. The arduino keeps printing '0' to indicate that the client is not available, while my server keeps printing "Invalid HTTP request received". Can anyone help me as to what goes wrong here?
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,y);
byte serverIP[] = {x, x, x, x};
EthernetClient client;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
delay(1000);
int res = client.connect(serverIP,8000);
Serial.println(res);
}
void loop(){
if (client.connected() == true) {
Serial.println("connected:)");
client.println("GET /sub/subsub HTTP/1.0");
if (client.available()) {
char c = client.read();
Serial.println(c);
} else {
Serial.println(client.available());
}
} else {
Serial.println("connection failed in loop: diconnecting...");
client.stop();
delay(1000);
client.connect(serverIP,8000);
}
delay(5000);
}
Related
I'm want to send data using ethernet on https, i'm read exmpale EthernetHttpClient_SSL but not work. How to Solve this?
This my code:
char server = "https://jsonplaceholder.typicode.com";
#define SSL_PORT 443
int port = 8080;
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetHttpClient_SSL.h>
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
EthernetClient client;
EthernetHttpClient httpClient(client, server, SSL_PORT);
void setup() {
Serial.begin(115200);
while (!Serial) {
;
}
Ethernet.init(5);
if (Ethernet.begin(mac) == 0) {
Serial.println("Failed to configure Ethernet using DHCP");
for (;;)
;
}
}
void loop() {
Serial.println("making GET request");
httpClient.get("/posts/1");
int statusCode = httpClient.responseStatusCode();
String response = httpClient.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
// Serial.print("Response: ");
// Serial.println(response);
Serial.println("Wait five seconds");
delay(5000);
}
I'm get respon code: -2
Is there a simple and easy library to access https using Ethernet?
If i'm access local server "localhost" success, i'm use client.print()
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
Simple Ethernet communication between two Arduino boards. I used two Arduino UNO boards, Two Arduino Ethernet Shields.
Here is my Server Code
//Server
#include <SPI.h>
#include <Ethernet.h>
// network configuration. gateway and subnet are optional.
// the media access control (ethernet hardware) address for the shield:
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
//the IP address for the shield:
byte ip[] = { 192,168,1,180 };
// the router's gateway address:
byte gateway[] = { 192, 168, 1, 1 };
// the subnet:
byte subnet[] = { 255, 255, 255, 0 };
EthernetServer server = EthernetServer(10001);
void setup()
{
// initialize the ethernet device
Ethernet.begin(mac, ip, gateway, subnet);
// start listening for clients
server.begin();
}
void loop()
{
// if an incoming client connects, there will be bytes available to read:
EthernetClient client = server.available();
if (client == true) {
// read bytes from the incoming client and write them back
// to any clients connected to the server:
server.write(client.read());
}
}
Here is my Client Code
//Client
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE,0xAD,0xBE,0xEF,0xFE,0xEC};
IPAddress ip(192,168,1,177);
IPAddress server(192,168,1,180);
EthernetClient client(10001);
void setup()
{
Ethernet.begin(mac, ip);
Serial.begin(9600);
delay(1000);
Serial.println("connecting...");
if (client.connect(server, 10001)) {
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(;;)
;
}
}
I input those two codes to two Arduino boards. Wiring and other connection are correct and reliable.
But I got this output from client serial monitor
connecting...
connection failed
disconnecting.
According to my view, error may happen in client code. Can you help me to find the error?
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");
I bought an Ethernet shield and I wrote a code, but I saw that there is .available() method. I don't know where to use it. So guys, do you know where and when I should use it? Here is my sample code:
#include <Ethernet.h>
#include <SPI.h>
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 107);
EthernetServer server = EthernetServer(80);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
}
void loop() {
EthernetClient client = server.available();
if(client) {
if(client.available()) {
char c = client.read();
Serial.print(c);
}
}
}
Thank you.
Client.available() returns the number of bytes that a client (remote client) may have written. If you're writing an HTTP server, the first client data would be: GET /URL HTTP/1.0.
You then write back to that client with Client.write(). For example:
c.write("HTTP/1.0 200 OK")