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

Community Tip - Have a PTC product question you need answered fast? Chances are someone has asked it before. Learn about the community search. X

ESP8266 Communicate with Thingworx

Telles
13-Aquamarine

ESP8266 Communicate with Thingworx

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.

 

ACCEPTED SOLUTION

Accepted Solutions
Telles
13-Aquamarine
(To:Telles)

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

View solution in original post

7 REPLIES 7

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

 

 

Telles
13-Aquamarine
(To:Rick-Stanley)

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.

 

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

Telles
13-Aquamarine
(To:Rick-Stanley)

These code of link works as very well!
But it created a Thing em GET the informations.
I try to PUT too.

When I creating a Thing by MCU I acepte the risk to all my Thing depends of power supply and not of TW. My Mashups has to online! Just the informations can be off.

slangley
23-Emerald II
(To:Telles)

Hi @Telles

 

I'm not fully understanding your last post.  Did you find a solution with the assistance of a previous response?  If so, please mark the appropriate response as the Accepted Solution for the benefit of others with the same issue.  If you still have questions, please provide more information.

 

Regards.

 

--Sharon 

Telles
13-Aquamarine
(To:slangley)

Hi, @slangley !
I still working with it.
Follow my code.
Also If I copy and past the URL generated in Serial Monitors of Arduino´s IDE, the TW has updated.
I can´t input values in TW.

// ############# LIBRARIES ############### //

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

#define LED 2
// ############# VARIABLES ############### //

const char* SSID = ""; // rede wifi
const char* PASSWORD = ""; // senha da rede wifi

String BASE_URL = "";

char server[] = "https://pp-210616174386.portal.ptc.io";
char appKey[] = "&4ba65138-1325-4377-a41d-692ded85b6fd";

char thingName[] = "Box";

char property1[]="pressao";
char property2[]="temperatura";

int i;

// ############# PROTOTYPES ############### //

void initSerial();
void initWiFi();
void httpRequest(String path);

// ############### OBJECTS ################# //

WiFiClient client;
HTTPClient http;

// ############## SETUP ################# //

void setup() {
initSerial();
initWiFi();
}

// ############### LOOP ################# //

void loop() {

pinMode(LED, OUTPUT);

Serial.println("[GET] /sensors - sending request...");
Serial.println("");

httpRequest("sensors");

Serial.println("");
delay(1000);

}

// ############# HTTP REQUEST ################ //

void httpRequest(String path)
{
String payload = makeRequest(path);

if (!payload) {
return;
}

Serial.println("##[RESULT]## ==> " + payload);

}

String makeRequest(String path)
{
String pressao = String(i);
i++;

digitalWrite(LED,HIGH);delay(700);
http://<SERVER>:<PORT>/Thingworx/Things/<THING_NAME>/Properties/<PROPERTY_NAME>?method=PUT&value=<VALUE>&appKey=<APP_KEY>
http.begin(BASE_URL + "/Thingworx/Things/" + thingName + "/Properties/" + property1 + "?method=PUT&value=" + pressao + appKey);
String URL_MONTADA = (BASE_URL + "/Thingworx/Things/" + thingName + "/Properties/" + property1 + "?method=PUT&value=" + pressao + appKey);
Serial.println(URL_MONTADA);digitalWrite(LED,LOW);delay(700);

int httpCode = http.GET();

if (httpCode < 0) {
Serial.println("request error - " + httpCode);
return "";
}

if (httpCode != HTTP_CODE_OK) {
return "";
}

String response = http.getString();
http.end();

return response;
}

// ###################################### //

// implementacao dos prototypes

void initSerial() {
Serial.begin(115200);
}

void initWiFi() {
delay(10);
Serial.println("Conectando-se em: " + String(SSID));

WiFi.begin(SSID, PASSWORD);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}

Serial.println();
Serial.print("Conectado na Rede " + String(SSID) + " | IP => ");
Serial.println(WiFi.localIP());
}

Regards.
Telles.

Telles
13-Aquamarine
(To:Telles)

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

Announcements


Top Tags