Skip to Content
0

ESP-Timeout error, while connecting to HCP via Arduino and ESP8266wifi sheild.

Aug 28, 2017 at 11:38 AM

135

avatar image
Former Member

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

iotcircuit.png (86.6 kB)
10 |10000 characters needed characters left characters exceeded
* Please Login or Register to Answer, Follow or Comment.

2 Answers

Anton Levin
Aug 30, 2017 at 06:09 AM
1

Hello Rajat,

I would recommend to try the ESP8266 sample form the IoT StarterKit [1] out and compare it to your implementation.

Regards, Anton

[1] https://github.com/SAP/iot-starterkit/blob/master/neo/hardware/ESP8266/Arduino/HTTPSRequest_HCP_POST.ino

Show 2 Share
10 |10000 characters needed characters left characters exceeded
Former Member

Hi Anton,

What should be circuit diagram for this program, it seems we are flashing the esp8266 in this one.

What if, i dont want to flash the esp8266 and communicate with arduino only, connected to esp.

Regards,

Rajat

0

That one is for an ESP8266 board running Arduino / programmed with the Arduino IDE. It's not one of the original Arduino branded boards.

0
Jan Reichert
Aug 30, 2017 at 05:30 AM
0

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

Share
10 |10000 characters needed characters left characters exceeded