Skip to content

Instantly share code, notes, and snippets.

@justind000
Last active July 18, 2019 21:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save justind000/5c8da0345fc5eb0c5b5beb3d7118a035 to your computer and use it in GitHub Desktop.
Save justind000/5c8da0345fc5eb0c5b5beb3d7118a035 to your computer and use it in GitHub Desktop.
IoT Hydroponics - Watson
#include "Watson.h"
#include "ISE_pH.h"
#include "uFire_EC.h"
ISE_pH ISE(19, 23);
uFire_EC EC(19, 23);
void setup(void)
{
WatsonMQTT_Init();
}
void loop(void)
{
String payload;
JsonObject& root = jsonBuffer.createObject();
JsonObject& d = root.createNestedObject("d");
d["mS"] = String(EC.measureEC());
d["pH"] = String(ISE.measurepH());
d["C"] = String(ISE.measureTemp());
root.printTo(payload);
WatsonMQTT_Publish(payload);
}
#include <WiFiClientSecure.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>
// your network information
const char *ssid = "";
const char *password = "";
#define Organization_ID "" // your organization
#define Device_Type "" // your registered device type
#define Device_ID "" // your registered device id
#define Authentication_Token "" // your device token
char server[] = Organization_ID ".messaging.internetofthings.ibmcloud.com";
char topic[] = "iot-2/evt/status/fmt/json";
char authMethod[] = "use-token-auth";
char token[] = Authentication_Token;
char clientId[] = "d:" Organization_ID ":" Device_Type ":" Device_ID;
const size_t bufferSize = JSON_OBJECT_SIZE(1) + JSON_OBJECT_SIZE(3);
DynamicJsonBuffer jsonBuffer(bufferSize);
WiFiClientSecure wifiClient;
PubSubClient client(wifiClient);
void WatsonMQTT_Publish(String data) {
if (!client.connected()) {
while (!client.connect(clientId, authMethod, token)) {
delay(500);
}
}
client.publish(topic, (char *)data.c_str());
}
void WatsonMQTT_Init() {
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
}
client.setServer(server, 8883);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment