Hi all,
I apologize if this is not in the right format, right forum page for this, or I have not included enough information; this is my first time posting to the Arduino forums page. I am trying to use the MKR1000 to send information (POST) to an HTTPS server. Specifically, I am getting temperature data from a TMP35 sensor and sending a name along with that data. I’ve used Postman and the server is working fine and returning the correct response; in addition, I have updated certificates on the Arduino. I am using the WiFi101 client and the Arduino HTTP client. Every time I run the code, it takes a while to receive a response and gives me a Status code: -2 with no response. My code is shown at the bottom with some parts removed for confidentiality. I understand this is poorly coded as there’s been a lot of testing and edits made to get it running; would appreciate any help and explanations assuming basic knowledge. Thanks!
/*
Reading temperature sensor data and sending to a server
*/
#include <ArduinoHttpClient.h>
#include <WiFi101.h>
#include “arduino_secrets.h”
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid = SECRET_SSID;
char pass = SECRET_PASS;
const char serverAddress = “name of server address”; // server address name (left out for security)
int port = 8080;
WiFiClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
int statusCode = 0;
String response;
void setup() {
Serial.begin(9600);
while(!Serial);
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to Network named: ");
Serial.println(ssid); // print the network name (SSID);
// Connect to WPA/WPA2 network:
status = WiFi.begin(ssid, pass);
}
// print the SSID of the network you’re attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield’s IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
}
void loop() {
// assemble the path for the POST message:
float temperature = 0.0;
float voltage = 0.0;
float temperature_in_F = 0.0;
//String postData = “{temp: 0, robotName: Freddy}”;
String contentType = “application/json”;
String robotName = “Freddy”;
// assemble the body of the POST message:
int sensorValue = analogRead(A0);
voltage = sensorValue * (3300/1024); // in milliVolt
temperature = (voltage - 500 ) / 10;
temperature_in_F = (temperature*(9/5)) + 32;
//postData = “temp=” + String(round(temperature_in_F)) + “&” + “robotName=” + robotName;
//postData = “{“temp”: “54”,“robotName”: “freight71”}”;
char* postData = “POST (name of application in form /something/something/something/)HTTPS/1.1\r\nHost: nameofhost.io\r\napplication/json”\r\ncontent-length:198\r\n\t"{“temp”: 54,“robotName”: “freight71”}";
Serial.println(postData);
Serial.println(“making POST request”);
String path = “(name of application in form /something/something/something/)”;
client.post(postData);
// read the status code and body of the response
int statusCode = client.responseStatusCode();
String response = client.responseBody();
Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
Serial.println(“Wait ten seconds\n”);
delay(10000);
}