Community Tip - Visit the PTCooler (the community lounge) to get to know your fellow community members and check out some of Dale's Friday Humor posts! X
I have a ESP8266 NodeMCU module making POST request to a Service connected to a Thing but the properties aren't getting refreshed. The properties are updated only when the module is first plugged in.
Here is the code to send trigger the service from the NodeMCU.
#include <DHT.h>
#define DHTTYPE DHT11
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#define dht_dpin 0
DHT dht(dht_dpin, DHTTYPE);
const char* ssid = "Cl";
const char* password = "xxx";
const char* host = "PP-1804050928FD.devportal.ptc.io";
const int httpsPort = 8080;
WiFiClient client;
void setup() {
dht.begin();
// put your setup code here, to run once:
Serial.begin(115200);
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());;
Serial.println(host);
if (!client.connect(host, httpsPort)) {
Serial.println("connection failed");
return;
}else{
Serial.println("Connected to ThingWorx.");
}
Serial.println("Humidity and temperature\n\n");
delay(700);
}
void loop() {
// put your main code here, to run repeatedly:
//Getting Temp and Humidity
int h = dht.readHumidity();
int t = dht.readTemperature();
String tem = String(t);
String hum = String(h);
Serial.print("Current humidity = ");
Serial.print(h);
Serial.print("% ");
Serial.print("temperature = ");
Serial.print(t);
Serial.println("C ");
// Setting service
String thvals = "{\"Temp\":\"" + tem + "\", \"Humid\":\"" + hum + "\"}";
String tourl = "http://PP-1804050928FD.devportal.ptc.io:8080/Thingworx/Things/DHT11Thing/Services/setTempAndHumid";
Serial.print("Setting Service url: ");
Serial.println(tourl);
client.print(String("POST ") + tourl + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"appKey: xxxx" + "\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(thvals.length()) + "\r\n\r\n" +
thvals + "\r\n\r\n");
while (client.connected()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
delay(1000);
}
I have a service that updates the properties but does not get refreshed periodically. Any help would be appreciated. Thanks.
From what I could I could understand you have a specific service running for this thing which takes rest post inputs and update properties accordingly(Please correct me if I am wrong.)
Could you please check the logs to see if there are entries with different timestamps?
Also try hard coding a value array from ESP to see if that is updated to make sure there is no issue with the DHT sensor.
Yes that is what I have currently. I checked the script log but I don't see anything, even the logger.info I've added to the script doesn't show up in the script log. The DHT11 works because I am able to receive the temperature and humidity and print it locally. Just can't seem to figure out what the issue could be.