How to ESP32 Ethernet access HTTPS? - arduino

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()

Related

Arduino get request results in FastAPI invalid HTTP request

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

Simple Arduino Client Server communication via Ethernet, IP

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?

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

RabbitMQ with Arduino Uno

I'm using RabbitMQ with Arduino for the first time and I need to publish data. So I've used the PubSubCLient class. This is the code:
#include <SPI.h>
#include <PubSubClient.h>
#include <Dhcp.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <Dns.h>
#include <EthernetServer.h>
#include <EthernetClient.h>
//declare variables
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xDE, 0xDE, 0xDD };
byte server[] = { 127, 0, 0, 1 };
byte ip[] = { 192, 168, 1, 22 };
String stringone = "localhost";
void callback(char* topic, byte* payload, unsigned int length) {
Serial.println(topic);
//convert byte to char
payload[length] = '\0';
String strPayload = String((char*)payload);
Serial.println(strPayload);
int valoc = strPayload.lastIndexOf(',');
String val = strPayload.substring(valoc+1);
Serial.println(val);
}
EthernetClient ethClient;
PubSubClient client(server, 5672, callback, ethClient);
void setup() {
// client is now configured for use
Serial.begin(9600);
Serial.println("==STARTING==");
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...");
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(".");
}
boolean con = client.connect("arduinoMQTT123");
while(con != 1) {
Serial.println("no con-while");
con = client.connect("arduinoMQTT123");
}
if(con) {
Serial.println("got con");
client.subscribe("/v2/feeds/FEED_ID.csv");
} else Serial.println("no con");
}
void loop() {
client.loop();
}
I keep getting an error, no connection. I think that's because I don't know how to use Arduino with RabbitMQ.
These two lines are the source of your troubles:
byte server[] = { 127, 0, 0, 1 };
...
PubSubClient client(server, 5672, callback, ethClient);
server[] must specify the address of your RabbitMQ server. 127.0.0.1 is the address for localhost. This is never routable.
Additionally, PubSubClient is an MQTT client, not a RabbitMQ (AMQP) client. Therefore, the AMQP port you specified, 5672, will not work.
You need to enable and configure the MQTT adapter in RabbitMQ and then use the appropriate MQTT port, typically 1883.

When I should use client.available() in Arduino?

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

Resources