Dear Forum users, I have a problem related to MKR1010 programming. It can be also a problem related to my poor knowledge of HTML, but at this point I am not sure.
Context
I am setting up a domotic system which is based on a ARDUINO UNO and a Arduino MKR WiFi 1010. The MKR1010 connects to the home LAN and from there to a Unix WebServer.
The WebServer will receive some data from the MKR1010 and will update a webpage accordingly.
Problem
I am trying to connect to the database, send a HTTP command and receive the answer back from the server. Well, the server does respond, but with a HTTP/1.1 400 Bad Request.
Anyhow, if I submit the same code to the server by online service like https://reqbin.com/, which sends command to server and receive the reply back, everything is ok and I receive a HTTP/1.1 200 OK reply.
Hypothesis
Since I get a good reply when using reqbin.com and a bad reply when I send the HTTP command by the MKR1010, I assume that the problem is in the way I write the code in the MKR sketch. Of course, I checked and it seems I write the commands the same way in the two environments, but apparently something is not clear to me, in fact the server's replies are different. I can't figure out what's wrong in the sketch anyway.
Other useful information
The exact HTML code sent by the reqbin.com to my webserver is:
GET / HTTP/1.1
Host: www.myserver.it
Content-Type: text/html;charset=UTF-8
so it is extremely simple and it is the same I (think) am sending to the server by MKR1010 too.
The server reply to the call from reqbin.com is
HTTP/1.1 200 OK
Server: aruba-proxy
Date: Sat, 11 Apr 2020 12:54:20 GMT
Content-Type: text/html;charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Vary: Accept-Encoding
Expires: Thu, 19 Nov 1981 08:52:00 GMT
Cache-Control: no-store, no-cache, must-revalidate
Pragma: no-cache
Set-Cookie: PHPSESSID=5cb91d8b077e4b43604e9141d4bac552; path=/
X-ServerName: ipvsproxy51.ad.aruba.it
Content-Encoding: gzip
Code
Here below I have posted the relevant code, where I kept the connection code lines only.
Sketch on MKR 1010:
#include <SPI.h>
#include <Wire.h>
#include <WiFiNINA.h>
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// use the numeric IP instead of the name for the server:
char server[] = "www.myserver.it";
WiFiClient client;
// --------------------------------- SETUP -----------------------------------------------
void setup() {
//Initialize serial and wait for port to open:
#ifdef SERIAL_MONITOR
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
#endif
// check for the WiFi module:
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("Communication with WiFi module failed!");
// don't continue
while (true);
}
String fv = WiFi.firmwareVersion();
if (fv < WIFI_FIRMWARE_LATEST_VERSION) {
Serial.println("Please upgrade the firmware");
}
// attempt to connect to Wifi network:
while (status != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
delay(6000);
}
// you're connected now, so print out the status:
printWifiStatus();
}
void loop() {
// listen for incoming clients
request_to_server(client);
delay(10000);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
void request_to_server(WiFiClient client){
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
int conn_res = client.connect(server, 443);
Serial.println("Connection result code: ");
Serial.println(conn_res);
if (conn_res) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET / HTTP/1.1");
client.println("Host: https://www.myserver.it");
client.println("Content-Type: text/html;charset=UTF-8");
client.println();
client.println();
}
int maxChars = 5000;
char server_answer[maxChars];
int server_answer_length = 0;
while (client.connected()){
if (client.available()){
int r = client.read();
if (r>=0){
server_answer[server_answer_length] = char(r);
server_answer_length++;
}
}
} // end of while loop
server_answer[server_answer_length] = NULL;
server_answer_length++;
Serial.println("SERVER REPLY **************");
Serial.println();
Serial.println(server_answer);
Serial.println();
Serial.println("****************************");
client.stop();
Serial.println("Client disconnected.");
delay(10000);
}
Question
What I am getting wrong in my MKR1010 sketch? It is supposed to send the same commands I send with reqbin.com!
Thank you very much.
Tommaso