SHARE
TWEET

hms_iot_plant_waterer

adamundefined Apr 29th, 2018 (edited) 59 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Halifax Makerspace
  3. Sunday workshop
  4. April 2018
  5. -----
  6. Moisture sensing plant waterer with Internet Of Things
  7.  
  8. */
  9.  
  10. #include <SPI.h>
  11. #include <Ethernet.h>
  12. #include <PubSubClient.h>
  13.  
  14. // Replace UNIQUE with the number you received in the workshop
  15. #define MQTTid "arduinoClient-PW-UNIQUE"
  16.  
  17. // If you do this project on your own you will need to create an account
  18. // on Adafruit.IO. You will populate your own username and AIO key
  19. // It's generally not good to share your key. I will be revoking this key
  20. // after the workshop.
  21. #define MQTTuser "REDACTED"
  22. #define MQTTpsw "REDACTED"
  23.  
  24. // MAC address for the Ethernet Shield. This needs to be unique on the network.
  25. // Replace 'UNIQUE' with the number you were given at the workshop
  26. byte mac[]    = {  0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xUNIQUE };
  27.  
  28. // How many milliseconds between updates to Adafruit.IO
  29. int publishDelay = 20000;
  30. // Pin of moisture sensor
  31. int sensorPin = 0;
  32. // Trigger threshold for moisture sensor
  33. int moistureThreshold = 1023;
  34.  
  35.  
  36. // This function is called when the Arduino receives a message from Adafruit.IO
  37. void callback(char* topic, byte* payload, unsigned int length) {
  38.   Serial.print("Message arrived [");
  39.   Serial.print(topic);
  40.   Serial.print("] ");
  41.   for (int i=0;i<length;i++) {
  42.     Serial.print((char)payload[i]);
  43.   }
  44.   Serial.println();
  45. }
  46.  
  47. EthernetClient ethClient;
  48. PubSubClient client(ethClient);
  49.  
  50. // This function makes the connection to Adafruit.IO
  51. void reconnect() {
  52.   // Loop until we're reconnected
  53.   while (!client.connected()) {
  54.     Serial.print("Attempting MQTT connection...");
  55.     // Attempt to connect
  56.     //
  57.     if (client.connect(MQTTid, MQTTuser, MQTTpsw)) {
  58.       Serial.println("connected");
  59.       // Once connected, publish an announcement...
  60.       // Replace UNIQUE with the number you received in the workshop
  61.       client.publish("REDACTED/feeds/plant-waterer-sensors.pw-messagereceive","PW-UNIQUE connected");
  62.       // ... and resubscribe
  63.       client.subscribe("REDACTED/feeds/plant-waterer-sensors.pw-messagepublish");
  64.     } else {
  65.       Serial.print("failed, rc=");
  66.       Serial.print(client.state());
  67.       Serial.println(" try again in 5 seconds");
  68.       // Wait 5 seconds before retrying
  69.       delay(5000);
  70.     }
  71.   }
  72. }
  73.  
  74. void setup()
  75. {
  76.   Serial.begin(9600);
  77.  
  78.   client.setServer("io.adafruit.com", 1883);
  79.   client.setCallback(callback);
  80.  
  81.   Ethernet.begin(mac);
  82.   // Allow the hardware to sort itself out
  83.   delay(1500);
  84. }
  85.  
  86. void loop()
  87. {
  88.   if (!client.connected()) {
  89.     reconnect();
  90.   }
  91.  
  92.   // Read the moisture value
  93.   int sensorValue = analogRead(sensorPin);
  94.   // Compare moisture value to trigger threshold
  95.   if (sensorValue <  moistureThreshold) {
  96.     Serial.println("Moisture threshold triggered");
  97.   }
  98.   // Convert the integer value to a char array
  99.   char sensorbuf [4];
  100.   sprintf (sensorbuf, "%03i", sensorValue);
  101.   // This line publishes to a specific feed on your Adafruit.IO account.
  102.   // Change 'UNIQUE' to the number you received in the workshop
  103.   client.publish("REDACTED/feeds/plant-waterer-sensors.pw-UNIQUE", sensorbuf);
  104.   Serial.print("Moisture level: ");
  105.   Serial.println(sensorValue);
  106.   delay(publishDelay);
  107.  
  108.   client.loop();
  109. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top