Skip to main content
1-Visitor
September 14, 2021
Solved

ESP8266 Communicate with Thingworx

  • September 14, 2021
  • 1 reply
  • 4066 views

Hi, Team!

  I work with these sketch but the TW don´t receive any data.
  Follow two printscreens.

#include "ESP8266WiFi.h"
#include<ESP8266HTTPClient.h>

const char* ssid = "InfoAxis_NET1";
const char* password = "InfoNet1";

char server[] = "xxxx";
char appKey[] = "xxxx";


// ThingWorx details
char thingName[] = "Box";
char serviceName[] = "setValue"; //optional
char property1[]="pressao";
char property2[]="temperatura";

#define ACCEPT_TYPE "text/csv"

void setup() {

Serial.begin(115200);
WiFi.begin(ssid, password);

while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.print(".");
}
}

void loop() {
if(WiFi.status() == WL_CONNECTED)
{
HTTPClient http;
String t ="{\"pressao\":\"40\"}";
int a=t.length();
Serial.println("TELLES");
Serial.println(a);
String URL = String(server) + "/Thingworx/Things/" + thingName + "/Properties/" + property1 + "?appKey=" + String(appKey);
Serial.println(URL);
http.begin(URL);

http.addHeader("Accept",ACCEPT_TYPE,false,false);
int httpcode=http.sendRequest("PUT",t);
Serial.println(httpcode);

if(httpcode > 0)
{
String payload = http.getString();

Serial.println(payload);

String t1="{\"temperatura\":\"56\"}";
int a1=t1.length();
Serial.println(a1);
String URL = String(server) + "/Thingworx/Things/" + thingName +"/Properties/" + property2 + "?appKey=" + String(appKey);
http.begin(URL);
http.addHeader("Accept",ACCEPT_TYPE,false,false);
http.addHeader("Content-Type","application/json",false,false);
int httpcode=http.sendRequest("PUT",t1);

if(httpcode > 0)
{
String payload = http.getString();
Serial.println(payload);
}

http.end();
}

delay(1000);
}
}

Perhaps I´m skipping some steps.

RT_10003402_0-1631645496209.pngRT_10003402_1-1631645508293.png

Regards.
Telles.

 

Best answer by Telles

Unfortunately, I have to chance the microcontroller to ESP32 and solved the problem!

1 reply

5-Regular Member
September 15, 2021

I see a couple issues:

1. You are not using the HTTPS library. This library is required for making https connections to the free trial servers

2. You are sending a string value in  your JSON to a Number type property

 

 

Telles1-VisitorAuthor
1-Visitor
September 15, 2021
Hi, Rick.
Now I´m using these libs:
 
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <ArduinoHttpClient.h>
#include <WiFiClientSecureBearSSL.h>
 
Follow printscreen, and changed the Properties to String.

RT_10003402_2-1631716590363.pngRT_10003402_3-1631716610353.png

But the TW still off.

Maybe I need a specicly library. Do you any URL for it?

Regards.
Telles.

 

5-Regular Member
September 15, 2021

Did you ever get the code here to work:

https://developer.thingworx.com/en/resources/guides/connect-adafruit-feather/set-arduino-ide-and-board

 

If that code does not work, you will not be able to get your code to work.

 

It looks like you are missing these lines:

std::unique_ptr<BearSSL::WiFiClientSecure>client(new BearSSL::WiFiClientSecure);
client->setInsecure();
https.begin(*client,URL);