on 08-28-2017 12:38 PM - last edited on 02-04-2024 2:05 AM by postmig_api_4
Hi, i am trying to upload sensor's data to HCP, in this setup I am using Arduino and ESP8266 and LM35 temperature sensor. when i try to connect to HCP it works and when I try to do a POST to HCP it gives timeout error.
Below is my circuit and Code.
(plz ignore the led)
#include <SoftwareSerial.h>
#include <WiFiEsp.h>
// ========== start configuration ==========
// WiFi configuration
const char* ssid = "XXXX";
const char* pass = "XXXXXXXXXXX";
// SAP HCP specific configuration
const char* host1 = "iotmmsXXXXXXXXXXtrial.hanatrial.ondemand.com";
String device_id = "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX";
String message_type_id = "XXXXXXXXXXXXXXXXXXXX";
String oauth_token="XXXXXXXXXXXXXXXXXXXXXXXXXXXX";
float temperature = 0.0;
// ========== end configuration ============
String url = "/com.sap.iotservices.mms/v1/api/http/data/" + device_id;
<br>const int ledPin = 13;
const int TSPin = A0;
const int httpsPort = 8443; //80//443//13
int status = WL_IDLE_STATUS;
WiFiEspClient client;
SoftwareSerial Serial1(2,3); //RX,TX
void setup() {
Serial.begin(9600);
Serial1.begin(9600);
WiFi.init(&Serial1);
// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}
// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}
// I GET TO CONNECT THE NETWORK
Serial.println("You're connected to the network");
Serial.print("connecting to ");
Serial.println(host1);
if (!client.connect(host1, httpsPort)) {
Serial.println("connection failed");
return;
}
else if(client.connect(host1, httpsPort))
{
Serial.println("connected to host");
}
}
void loop() {
int sensorVal = analogRead(TSPin);
digitalWrite(ledPin,HIGH);
delay(200);
digitalWrite(ledPin,LOW);
float voltage = (sensorVal/1024.0)*5.0;
//print new value to serial monitor
Serial.print("Volts: ");
Serial.print(voltage);
//convert the voltage to temperature in degrees
temperature = (voltage)*100; //
Serial.print(", degrees C: ");
Serial.println(temperature);
Serial.println();
String post_payload = "{\"mode\":\"async\", \"messageType\":\"" + message_type_id + "\", \"messages\":[{\"Temprature\":"+temperature+"}]}";
int Payload_length = post_payload.length();
Serial.print("requesting URL: ");
Serial.println(url);
Serial.print("post_payload: ");
Serial.println(post_payload);
Serial.print("Payload_length:");
Serial.println(Payload_length);
if (client.connect(host1,httpsPort)) {
Serial.println("Connecting...");///////I GET THE CLIENT IS CONNECTED////
client.print(String("POST ") + url + " HTTP/1.0\r\n" +
"Host: " + host1 + "\r\n" +
"Connection: close\r\n" +
"Content-Type: application/json;charset=utf-8\r\n" +
"Authorization: Bearer " + oauth_token + "\r\n" +
"Content-Length: " + post_payload.length() + "\r\n\r\n" +
post_payload + "\r\n\r\n");
//////////// GET ERROR TIME OUT OVER HERE ON POST//////////////////////
}
else {
// if you couldn't make a connection
Serial.println("Connection failed");
}
Serial.println("request sent");
Serial.println("reply was:");
Serial.println("==========");
while(client.connected())
{
// There is a client connected. Is there anything to read?
while(client.available() > 0)
{
char c = client.read();
Serial.print(c);
}
Serial.println();
Serial.println("Waiting for more data...");
delay(100); // Have a bit of patience...
}
Serial.println("==========");
Serial.println("Closing connection and waiting 15 seconds");
delay(15000);
}
Hello Rajat,
I would recommend to try the ESP8266 sample form the IoT StarterKit [1] out and compare it to your implementation.
Regards, Anton
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
Hi Rajat,
Does the WiFiESP Library support HTTPS? At least in the documentation (https://github.com/bportaluri/WiFiEsp) I couldn't find a reference. On the other hand SAP Cloud Platform Internet of Things for the Neo Environment only supports HTTPS (https://help.sap.com/viewer/7436c3125dd5491f939689f18954b1e9/Cloud/en-US/8e1c277be0cd4854943a15f86188aaec.html).
In the past, we used ESP8266HTTPClient to connect to IoT Services. This library supports HTTPS. You can find it here: https://github.com/scanlime/esp8266-Arduino/tree/master/libraries/ESP8266HTTPClient
Regards
Jan
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.
User | Count |
---|---|
94 | |
11 | |
11 | |
10 | |
9 | |
8 | |
6 | |
5 | |
4 | |
4 |
You must be a registered user to add a comment. If you've already registered, sign in. Otherwise, register and sign in.