Well, it’s still doing it.
Its a simple program, sending data to thingsboard most successfully I might add - thanks to another contributor.
However - this starter bit of code requires real data and not the random generated stuff.
So - I added a bit of serial UART code, which didn’t seem to be working ( removed from this snippet ) and put some particle variables in to monitor stuff.
There are two issues -
-
The Console now ( still ) does not show the variables, where as it did a few hours ago. It suddenly stopped displaying.
-
There’s a third variable that has been commented out - it causes a compile error :-
./wiring/inc/spark_wiring_cloud.h:83:25: call of overloaded ‘_variable(const char [7], const unsigned int&)’ is ambiguous
My code, less the token is attached.
I’ve restarted the photon several times, recompiled, reloaded and restarted the console.
It’s been a struggle today!
//
// Last update: 04/15/18
//
#include <PubSubClient.h>
#define TOKEN "MYTOKEN-OF-COURSE"
#define LED 7
#define MQTTPORT 1883
char aquabuffr[128];
char aquabuf[128] = "aquabuf";
char* aquabufptr;
unsigned int aquabufindx;
//unsigned int countrry; // THIS GIVES A COMPILE ERROR - see below
char serdata;
char thingsboardServer[] = "demo.thingsboard.io";
char attributes[] = "{\"firmware_version\":\"0.7.0\",\"software_version\":\"1.0\"}";
char mqttPub[128];
TCPClient pubsub;
PubSubClient psClient((Client &) pubsub);
void setup(void)
{
// Particle.variable("countr", countrry); // THIS GIVES A COMPILE ERROR - see above
Particle.variable("buff", aquabuf);
Particle.variable("serial", serdata);
Serial.begin(115200);
pinMode(LED, OUTPUT);
psClient.setServer(thingsboardServer, MQTTPORT);
serverConnect();
snprintf(mqttPub, sizeof(mqttPub), "%s", attributes);
Serial.println(mqttPub);
psClient.publish("v1/devices/me/attributes", mqttPub);
}
void loop(void)
{
if (!psClient.connected())
serverConnect();
sendTemp();
delay(5000);
psClient.loop();
}
void sendTemp(void)
{
float t = 630;//sensor.readTemperature() * 1.8 + 32.0;
float ec = 8.5; //sensor.readHumidity();
float ph = 8.5; //sensor.readHumidity();
float flow = 8.5; //sensor.readHumidity();
t = randoma (17,25);
ec = randoma (0,2000);
ph = randoma (1,15);
flow = randoma (3,40);
// Prepare JSON payload
// snprintf(mqttPub, sizeof(mqttPub), "{\"temperature\":%.2f,\"humidity\":%.1f}", t, h);
// Prepare JSON payload
snprintf(mqttPub, sizeof(mqttPub), "{\"temperature\":%.2f,\"conductivity\":%.1f,\"pH\":%.1f,\"flow\":%.1f}", t, ec,ph,flow);
Serial.println(mqttPub);
// Send payload
psClient.publish("v1/devices/me/telemetry", mqttPub); // Topic, payload
blinkLED();
}
//TOKEN QJ0ZlNI7RuyXP8c3aY8z
void serverConnect(void)
{
while (!psClient.connected())
{
Serial.println("serverConnect(): Attempting to connect to Thingsboard server");
if (psClient.connect("Photon", TOKEN, NULL)) // ClientID, User, PW
{
Serial.println("serverConnect(): Connected");
return;
}
else
Serial.println("serverConnect(): Connection failed, retry in 3 seconds");
delay(3000);
}
}
void blinkLED(void)
{
digitalWrite(LED,HIGH);
delay(5);
digitalWrite(LED,LOW);
delay(5);
}
// returns a random integar between minVal and maxVal
int randoma(int minVal, int maxVal)
{
// int rand(void); included by default from newlib
return rand() % (maxVal-minVal+1) + minVal;
}