Created
February 18, 2018 07:58
-
-
Save shazin/f30f41977d801873c5b331287c2240ea to your computer and use it in GitHub Desktop.
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 <SPI.h> | |
#include <Ethernet.h> | |
#include <PubSubClient.h> | |
#include <dht.h> | |
// Update these with values suitable for your network. | |
byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xED }; | |
IPAddress ip(192, 168, 1, 5); | |
IPAddress server(192, 168, 1, 2); | |
// YL-39 + YL-69 humidity sensor | |
byte humidity_sensor_pin = A1; | |
byte humidity_sensor_vcc = 7; | |
#define DHT11_PIN 6 | |
#define AUTH_TOKEN "1a3XZV0Tc17lkA7DkdKO" // ThingsBoard Device Auth Token | |
dht DHT; | |
void callback(char* topic, byte* payload, unsigned int length) { | |
Serial.print("Message arrived ["); | |
Serial.print(topic); | |
Serial.print("] "); | |
for (int i=0;i<length;i++) { | |
Serial.print((char)payload[i]); | |
} | |
Serial.println(); | |
} | |
EthernetClient ethClient; | |
PubSubClient client(ethClient); | |
void reconnect() { | |
// Loop until we're reconnected | |
while (!client.connected()) { | |
Serial.print("Attempting MQTT connection..."); | |
// Attempt to connect | |
if (client.connect("arduinoClient", AUTH_TOKEN, NULL)) { | |
Serial.println("connected"); | |
// Once connected, publish an announcement... | |
client.publish("v1/devices/me/attributes","{\"firmware_version\":\"1.0.0\", \"serial_number\":\"ThingsBoardClient-001\"}"); | |
// ... and resubscribe | |
//client.subscribe("inTopic"); | |
} else { | |
Serial.print("failed, rc="); | |
Serial.print(client.state()); | |
Serial.println(" try again in 5 seconds"); | |
// Wait 5 seconds before retrying | |
delay(5000); | |
} | |
} | |
} | |
void setup() | |
{ | |
Serial.begin(57600); | |
// Init the humidity sensor board | |
pinMode(humidity_sensor_vcc, OUTPUT); | |
digitalWrite(humidity_sensor_vcc, LOW); | |
client.setServer(server, 1883); | |
client.setCallback(callback); | |
Ethernet.begin(mac, ip); | |
// Allow the hardware to sort itself out | |
delay(1500); | |
} | |
int read_humidity_sensor() { | |
digitalWrite(humidity_sensor_vcc, HIGH); | |
delay(500); | |
int value = analogRead(humidity_sensor_pin); | |
digitalWrite(humidity_sensor_vcc, LOW); | |
return 1023 - value; | |
} | |
void loop() | |
{ | |
if (!client.connected()) { | |
reconnect(); | |
} | |
int chk = DHT.read11(DHT11_PIN); | |
switch (chk) | |
{ | |
case DHTLIB_OK: | |
Serial.print("OK,\t"); | |
break; | |
case DHTLIB_ERROR_CHECKSUM: | |
Serial.print("Checksum error,\t"); | |
break; | |
case DHTLIB_ERROR_TIMEOUT: | |
Serial.print("Time out error,\t"); | |
break; | |
case DHTLIB_ERROR_CONNECT: | |
Serial.print("Connect error,\t"); | |
break; | |
case DHTLIB_ERROR_ACK_L: | |
Serial.print("Ack Low error,\t"); | |
break; | |
case DHTLIB_ERROR_ACK_H: | |
Serial.print("Ack High error,\t"); | |
break; | |
default: | |
Serial.print("Unknown error,\t"); | |
break; | |
} | |
String json = "{\"temperature\":"+String(DHT.temperature, 2)+", \"humidity\":"+String(DHT.humidity, 2)+", \"moisture\":"+read_humidity_sensor()+", \"active\": false}"; | |
char buf[json.length()+1]; | |
json.toCharArray(buf, json.length()+1); | |
client.publish("v1/devices/me/telemetry", buf); | |
Serial.print("Data Sent : "); | |
Serial.println(buf); | |
delay(5000); | |
client.loop(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment