cancel
Showing results for 
Search instead for 
Did you mean: 
cancel
Showing results for 
Search instead for 
Did you mean: 

Community Tip - You can subscribe to a forum, label or individual post and receive email notifications when someone posts a new topic or reply. Learn more! X

Arduino can't connect to SSL (Thingworx Studio trial server)

pphunnawut
1-Visitor

Arduino can't connect to SSL (Thingworx Studio trial server)

Arduino can't connect to SSL

I have no problem connecting using port 80  .After that I try to connect  SSL server (Thingworx Studio trial  server) , But not able to connect .

Please see the below for my Sketch in Arduino.

#include <SPI.h>

#include <Ethernet2.h>

#include <ArduinoJson.h>

#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to

#define DHTTYPE DHT22   // DHT 22  (AM2302), AM2321

// Enter a MAC address and IP address for your controller below.

// The IP address will be dependent on your local network:

byte mac[] = {0x2C, 0xF7, 0xF1, 0x08, 0x09, 0x94};

char server[] = "ggtydeu7.studio-trial.thingworx.io";

EthernetClient client;

//ThingWorx App key which replaces login credentials)

char appKey[] = "xxxxx-b2c7-46a1-baf8-d70d4c701c80";

// ThingWorx Thing name for which you want to set properties values

char thingName[] = "HTU21DThing";

//Interval of time at which you want the properties values to be sent to TWX server

int timeBetweenRefresh = 5000;

// ThingWorx service that will set values for the properties you need

// See the documentation for this tutorial for more information

char serviceName[] = "setTempAndHumid";

//How many values you will be pushing to ThingWorx

#define propertyCount 2

//Initialize Properties Names and Values Arrays

const char* propertyNames[] = {"Temp", "Humid"};

double propertyValues[propertyCount];

// last time you connected to the server, in milliseconds

unsigned long lastConnectionTime = 0;

// timer waiting for the arrival of characters from the server

unsigned long timer_iot_timeout = 0;

// Receive buffer size

#define BUFF_LENGTH 64

// temperature and humidity data from your connected sensor

DHT dht(DHTPIN, DHTTYPE);

void setup() {

  //shut down the SD Card pins

  pinMode(4, OUTPUT);

  digitalWrite(4, HIGH);

  //initialize HTU21D object to read values from sensors

   dht.begin();

  // start serial port:

  Serial.begin(9600);

  while (!Serial) {

    ; // wait for serial port to connect. Needed for Leonardo only

  }

  // start the Ethernet connection:

  Serial.println("Trying to get an IP address using DHCP");

  Ethernet.begin(mac);

  Serial.print("My IP address: ");

  Serial.print(Ethernet.localIP());

  Serial.println();

}

void loop() {

  // wait the established interval of time before

  // reading values from the sensor

  // and sending them to the TWX server again

  // delay(timeBetweenRefresh);

  if (millis() - lastConnectionTime > timeBetweenRefresh)

    updateServiceValues();

  else

    updateSensorsData();

}

void updateSensorsData()

{

  // Aquire sensor values

  propertyValues[0] = dht.readTemperature();

  propertyValues[1] = dht.readHumidity();

  

}

void updateServiceValues()

{

  // Connection to server Thingworx

  Serial.println("Connecting to IoT server...");

  if (client.connect(server,8443))

  {

    // Check connection

    if (client.connected())

    {

      // Sending a header of a network packet

      Serial.println("Sending data to IoT server...\n");

      Serial.print("POST /Thingworx/Things/");

      client.print("POST /Thingworx/Things/");

      Serial.print(thingName);

      client.print(thingName);

      Serial.print("/Services/");

      client.print("/Services/");

      Serial.print(serviceName);

      client.print(serviceName);

      Serial.print("?appKey=");

      client.print("?appKey=");

      Serial.print(appKey);

      client.print(appKey);

      Serial.print("&method=post&x-thingworx-session=true");

      client.print("&method=post&x-thingworx-session=true");

      // Sending data from sensors

      for (int idx = 0; idx < propertyCount; idx ++)

      {

        Serial.print("&");

        client.print("&");

        Serial.print(propertyNames[idx]);

        client.print(propertyNames[idx]);

        Serial.print("=");

        client.print("=");

        Serial.print(propertyValues[idx]);

        client.print(propertyValues[idx]);

      }

      // Close package

      Serial.println(" HTTP/1.1");

      client.println(" HTTP/1.1");

      Serial.println("Accept: application/json");

      client.println("Accept: application/json");

      Serial.print("Host: ");

      client.print("Host: ");

      Serial.println(server);

      client.println(server);

      Serial.println("Content-Type: application/json");

      client.println("Content-Type: application/json");

      Serial.println();

      client.println();

      // Waiting for a response from the server

      timer_iot_timeout = millis();

      while ((client.available() == 0) && (millis() < timer_iot_timeout + 5000));

      // Display the response on the server, and if a slow connection, wait for the timeout

      char buff[BUFF_LENGTH] = "";

      int iii = 0;

      bool flagJSON = false;

      timer_iot_timeout = millis();

      while ((millis() < timer_iot_timeout + 100) && (client.connected()))

      {

        while (client.available() > 0)

        {

          char symb = client.read();

          Serial.print(symb);

          if (symb == '{')

          {

            flagJSON = true;

          }

          else if (symb == '}')

          {

            flagJSON = false;

          }

          if (flagJSON == true)

          {

            buff[iii] = symb;

            iii ++;

          }

          timer_iot_timeout = millis();

        }

      }

      buff[iii] = '}';

      buff[iii + 1] = '\0';

      Serial.println(buff);

      // Close connection

      client.stop();

      // Decrypted parameters

      StaticJsonBuffer<BUFF_LENGTH> jsonBuffer;

      JsonObject& json_array = jsonBuffer.parseObject(buff);

      //Service result variable (return JSON)

      int result = json_array["result"];

      Serial.println("Result state:   " + String(result));

      Serial.println();

      // Device control

      //controlDevices();

      Serial.println("Packet successfully sent!");

      Serial.println();

      lastConnectionTime = millis();

    }

  }

  else {

    // if you didn't get a connection to the server:

    Serial.println("Connection could not be established");

    client.stop();

  }

}

Please see below for  the serial monitor

Trying to get an IP address using DHCP

My IP address: 192.168.1.8

Connecting to IoT server...

Sending data to IoT server...

POST /Thingworx/Things/HTU21DThing/Services/setTempAndHumid?appKey=xxxxx-46a1-baf8-d70d4c701c80&method=post&x-thingworx-session=true&Temp=31.50&Humid=49.00 HTTP/1.1

Accept: application/json

Host: ggtydeu7.studio-trial.thingworx.io

Content-Type: application/json

}

Result state:   0

Packet successfully sent!

1 REPLY 1

I too have such a problem. I do not know how to solve it.
Announcements

Top Tags