Community Tip - Did you get an answer that solved your problem? Please mark it as an Accepted Solution so others with the same problem can find the answer easily. X
HI,
I have no problem in sending Sensor data to my Thingworx composer using port 80.,
But while i am trying to send data to Thingworx composer in Studio, i cant publish values rather getting error 400.
Here is my code:
#include "DHTesp.h"
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include<arduino.h>
#include <ESP8266HTTPClient.h>
#include<string.h>
#define ACCEPT_TYPE "text/csv"
DHTesp dht;
const char* ssid = "Akshay";
const char* password = "asdfghjkl";
const int pingPin = D7; // Trigger Pin of Ultrasonic Sensor
const int echoPin = D8; // Echo Pin of Ultrasonic Sensor
const char* host = "https://Thingworx_studio_url"; // ThingWorx Server IP Address
const int httpsPort = 8443; //and also tried with port 3000 , 443
WiFiClient client;
void setup()
{
Serial.begin(115200);
dht.setup(D6); // Connect DHT sensor to GPIO 17
pinMode(pingPin, OUTPUT);
pinMode(echoPin, INPUT);
Serial.println();
Serial.print("connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());;
}
long microsecondsToInches(long microseconds)
{
return microseconds / 74 / 2;
}
long microsecondsToCentimeters(long microseconds)
{
return microseconds / 29 / 2;
}
void putreq(String propertyname,float propertyvalue)
{
Serial.println(host);
if (!client.connect(host, httpsPort))
{
Serial.println("connection failed");
return;
} else
{
Serial.println("Connected to ThingWorx.");
}
String convstr = String(propertyvalue);
Serial.print("convstr= ");
Serial.println(convstr);
String url = "/Thingworx/Things/kthing/Properties/"+propertyname;
Serial.print("URL= ");
Serial.println(url);
String strPUTReqVal = "{\""+ propertyname+"\":\""+convstr+"\"}";
Serial.print("strPUTReqVal= ");
Serial.println(strPUTReqVal);
client.print(String("PUT ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"appKey:xxxxxxxxxxxxxxxxxxxxxxxx" + "\r\n" +
"x-thingworx-session: false" + "\r\n" +
"Accept: application/json" + "\r\n" +
"Connection: close" + "\r\n" +
"Content-Type: application/json" + "\r\n" +
"Content-Length: " + String(strPUTReqVal.length()) + "\r\n\r\n" +
strPUTReqVal + "\r\n\r\n");
while (client.connected())
{
String line = client.readStringUntil('\r');
Serial.print(line);
}
client.stop();
delay(100);
}
void loop()
{
delay(dht.getMinimumSamplingPeriod());
float humidity = dht.getHumidity();
float temperature = dht.getTemperature();
Serial.print(dht.getStatusString());
Serial.print("Humidity: ");
Serial.print(humidity, 1);
Serial.print("Temperature: ");
Serial.print(temperature, 1);
long duration, inches, cm;
digitalWrite(pingPin, LOW);
delayMicroseconds(2);
digitalWrite(pingPin, HIGH);
delayMicroseconds(10);
digitalWrite(pingPin, LOW);
duration = pulseIn(echoPin, HIGH);
inches = microsecondsToInches(duration);
cm = microsecondsToCentimeters(duration);
Serial.print("Distance: ");
Serial.print(cm);
Serial.print("cm");
Serial.println();
putreq("kt",dht.getTemperature());
putreq("kh",dht.getHumidity());
putreq("kd",cm);
}
Please Help,
Regards,
Akshay
Solved! Go to Solution.
Hi Durgesh,
I achieved it with python(Raspberrypi), should check with NodeMCU (ESP8266).
Here is the code which i have written:
import time
import requests
import json
headers = { 'Content-Type': 'application/json', 'appKey': 'xxxxxxxxxxxxxxxxxxxxxx' }
payload = {'Prop_Temperature' : 1234}
response = requests.put('https://xxxxxxxx.studio-trial.vuforia.io:443/Thingworx/Things/RaspberryPi1(2aebd169b221f5f0)/Properties/Prop_Temperature', headers=headers, json=payload, verify=False)
Hope this helps to someone who is trying it!!
Thanks,
Regards,
Akshay
@AkshayKumar Could you please confirm the Studio URL and is Trial instance that you are trying to connect ?
-Durgesh
@AkshayKumar You are observing the issue which could be due to the fact that Trial instance has restriction in accessing ThingWorx port & URL externally. Studio associated ThingWorx has been configured and granted access to work from within Studio.
-Durgesh
Hi Durgesh,
I achieved it with python(Raspberrypi), should check with NodeMCU (ESP8266).
Here is the code which i have written:
import time
import requests
import json
headers = { 'Content-Type': 'application/json', 'appKey': 'xxxxxxxxxxxxxxxxxxxxxx' }
payload = {'Prop_Temperature' : 1234}
response = requests.put('https://xxxxxxxx.studio-trial.vuforia.io:443/Thingworx/Things/RaspberryPi1(2aebd169b221f5f0)/Properties/Prop_Temperature', headers=headers, json=payload, verify=False)
Hope this helps to someone who is trying it!!
Thanks,
Regards,
Akshay