Community Tip - New to the community? Learn how to post a question and get help from PTC and industry experts! X
Hi everyone, i chose Thingworx platfom for my study project. In general, there is should be robot which sends and also recieves data from Thingworx mashup via Nodemcu ESP8266.
I followed this tutorial, but it didnt work for me https://developer.thingworx.com/en/resources/guides/connect-adafruit-feather
I tried to launch it on next 2 platforms with different UI for some reason
https://ptcu-thingworx83-fundamentals.portal.ptc.io/Thingworx/Composer
and
https://academic.cloud.thingworx.com/Thingworx/Composer/
Got only some messages from port monitor like state: 0 -> 2 (b0)
I actually confused about this, so is it possible to connect my ESP to one of these platforms and what the difference here? Can anyone help with some advice or even working example? Actually some simple operation of sending any number from ESP to Thing property would be enough for me.
Thanks a lot in advance.
Solved! Go to Solution.
Solved! Some sample code for future researchers, you want to change wifi parameters and your Thingworx platform parameters. Here you just sending ++number to the first property (one )and getting another from the second (otvet1) they both have type number. Contents of service u can find in the screenshot below.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <SimpleTimer.h>
// Wi-Fi parameters
char ssid[] = "MTSRouter-302000";
char pass[] = "zxcqwe123";
#define IOT_UPDATE_TIME 2000
SimpleTimer timer_iot;
// Thingworx parameters
char iot_server[] = "academic.cloud.thingworx.com";
IPAddress iot_address(52, 71, 6, 169);
char thingName[] = "thing123";
char serviceName[] = "service1";
char appKey[] = "xxxxxxxx";
// Sensors
char sensorNames[] = "one";
float sensorValues = 0;
// Max timeout
#define IOT_TIMEOUT1 1000
#define IOT_TIMEOUT2 100
long timer_iot_timeout = 0;
#define BUFF_LENGTH 256
int led_control = 0;
// buffer
char buff[BUFF_LENGTH] = "";
void setup()
{
Serial.begin(115200);
delay(512);
// Init .Wi-Fi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
timer_iot.setInterval(IOT_UPDATE_TIME, sendDataIot);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
timer_iot.run();
}
void sendDataIot()
{
// Server connect
WiFiClientSecure client;
Serial.println("Connecting to IoT server...");
if (client.connect(iot_address, 443))
{
// Checking connection
if (client.connected())
{
// POST request
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");
// Data from sensors
Serial.print("&");
client.print("&");
Serial.print(sensorNames);
client.print(sensorNames);
Serial.print("=");
client.print("=");
Serial.print(sensorValues++);
client.print(sensorValues++);
// Close packet
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(iot_server);
client.println(iot_server);
Serial.println("Content-Type: application/json");
client.println("Content-Type: application/json");
Serial.println();
client.println();
// Waiting response
timer_iot_timeout = millis();
while ((client.available() == 0) && (millis() < timer_iot_timeout + IOT_TIMEOUT1));
// Show response or dc if connection lost
int iii = 0;
bool currentLineIsBlank = true;
bool flagJSON = false;
timer_iot_timeout = millis();
while ((millis() < timer_iot_timeout + IOT_TIMEOUT2) && (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();
// Parsing response
StaticJsonBuffer<BUFF_LENGTH> jsonBuffer;
JsonObject& json_array = jsonBuffer.parseObject(buff);
if (json_array.success())
{
led_control = json_array["otvet1"];
Serial.println("OTVET1: " + String(led_control));
Serial.println();
}
else
{
Serial.println("Fail to parse packet from Thingworx!");
Serial.println();
}
led();
Serial.println("Packet successfully sent!");
Serial.println();
}
}
}
void led()
{
if (led_control == 0){
digitalWrite(LED_BUILTIN, HIGH);
}
else{
digitalWrite(LED_BUILTIN, LOW);
}
}
The guide you mentioned shows how to use ThingWorx REST API from a limited resource device. Before adding the complication of hardware, confirm you are able to use the REST API from a PC:
Some common things to check:
Do you have the correct URL and port?
Do you have a valid, un-expired appKey?
Does the Thing exist on the ThingWorx platform?
Does the Thing you are attempting to use have the property you are trying to access?
Thanks for your response, Postman is working fine and recieves data from property succesfully according to screenshot, but ESP returns "Error on sending GET Request: -1".I tried to make some simple GET request to property "one" as well.
#include "ESP8266WiFi.h"
#include <ESP8266HTTPClient.h>
const char* ssid = "MTSRouter-302000"; //Enter SSID
const char* password = "zxcqwe123"; //Enter Password
void setup(void)
{
Serial.begin(115200);
// Connect to WiFi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print("*");
}
Serial.println("");
Serial.println("WiFi connection Successful");
Serial.print("The IP Address of ESP8266 Module is: ");
Serial.print(WiFi.localIP());// Print the IP address
}
void loop()
{
if (WiFi.status() == WL_CONNECTED) { //Check WiFi connection status
HTTPClient http;
http.begin("ptcu-thingworx83-fundamentals.portal.ptc.io/Thingworx/Things/TEST_Biosphia/Properties/one"); //Specify request destination
http.addHeader("appKey","8de9f6e8-ca6c-4653-acad-915ba7d1c13a",false,false);
http.addHeader("Accept","text/csv",false,false);
int httpCode = http.GET(); //Send the request
if (httpCode > 0) { //Check the returning code
String payload = http.getString(); //Get the request response payload
Serial.println(httpCode);
Serial.println(payload); //Print the response payload
} else {
Serial.print("Error on sending GET Request: ");
Serial.println(httpCode);
}
http.end(); //Close connection
}
delay(1000);
}
Solved! Some sample code for future researchers, you want to change wifi parameters and your Thingworx platform parameters. Here you just sending ++number to the first property (one )and getting another from the second (otvet1) they both have type number. Contents of service u can find in the screenshot below.
#include <ESP8266WiFi.h>
#include <WiFiClientSecure.h>
#include <ArduinoJson.h>
#include <SimpleTimer.h>
// Wi-Fi parameters
char ssid[] = "MTSRouter-302000";
char pass[] = "zxcqwe123";
#define IOT_UPDATE_TIME 2000
SimpleTimer timer_iot;
// Thingworx parameters
char iot_server[] = "academic.cloud.thingworx.com";
IPAddress iot_address(52, 71, 6, 169);
char thingName[] = "thing123";
char serviceName[] = "service1";
char appKey[] = "xxxxxxxx";
// Sensors
char sensorNames[] = "one";
float sensorValues = 0;
// Max timeout
#define IOT_TIMEOUT1 1000
#define IOT_TIMEOUT2 100
long timer_iot_timeout = 0;
#define BUFF_LENGTH 256
int led_control = 0;
// buffer
char buff[BUFF_LENGTH] = "";
void setup()
{
Serial.begin(115200);
delay(512);
// Init .Wi-Fi
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED)
{
delay(100);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
Serial.println();
timer_iot.setInterval(IOT_UPDATE_TIME, sendDataIot);
pinMode(LED_BUILTIN, OUTPUT);
}
void loop()
{
timer_iot.run();
}
void sendDataIot()
{
// Server connect
WiFiClientSecure client;
Serial.println("Connecting to IoT server...");
if (client.connect(iot_address, 443))
{
// Checking connection
if (client.connected())
{
// POST request
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");
// Data from sensors
Serial.print("&");
client.print("&");
Serial.print(sensorNames);
client.print(sensorNames);
Serial.print("=");
client.print("=");
Serial.print(sensorValues++);
client.print(sensorValues++);
// Close packet
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(iot_server);
client.println(iot_server);
Serial.println("Content-Type: application/json");
client.println("Content-Type: application/json");
Serial.println();
client.println();
// Waiting response
timer_iot_timeout = millis();
while ((client.available() == 0) && (millis() < timer_iot_timeout + IOT_TIMEOUT1));
// Show response or dc if connection lost
int iii = 0;
bool currentLineIsBlank = true;
bool flagJSON = false;
timer_iot_timeout = millis();
while ((millis() < timer_iot_timeout + IOT_TIMEOUT2) && (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();
// Parsing response
StaticJsonBuffer<BUFF_LENGTH> jsonBuffer;
JsonObject& json_array = jsonBuffer.parseObject(buff);
if (json_array.success())
{
led_control = json_array["otvet1"];
Serial.println("OTVET1: " + String(led_control));
Serial.println();
}
else
{
Serial.println("Fail to parse packet from Thingworx!");
Serial.println();
}
led();
Serial.println("Packet successfully sent!");
Serial.println();
}
}
}
void led()
{
if (led_control == 0){
digitalWrite(LED_BUILTIN, HIGH);
}
else{
digitalWrite(LED_BUILTIN, LOW);
}
}