Last active
July 18, 2019 21:23
-
-
Save justind000/5c8da0345fc5eb0c5b5beb3d7118a035 to your computer and use it in GitHub Desktop.
IoT Hydroponics - Watson
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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