Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Halifax Makerspace
- Sunday workshop
- April 2018
- -----
- Moisture sensing plant waterer with Internet Of Things
- */
- #include <SPI.h>
- #include <Ethernet.h>
- #include <PubSubClient.h>
- // Replace UNIQUE with the number you received in the workshop
- #define MQTTid "arduinoClient-PW-UNIQUE"
- // If you do this project on your own you will need to create an account
- // on Adafruit.IO. You will populate your own username and AIO key
- // It's generally not good to share your key. I will be revoking this key
- // after the workshop.
- #define MQTTuser "REDACTED"
- #define MQTTpsw "REDACTED"
- // MAC address for the Ethernet Shield. This needs to be unique on the network.
- // Replace 'UNIQUE' with the number you were given at the workshop
- byte mac[] = { 0xDE, 0xED, 0xBA, 0xFE, 0xFE, 0xUNIQUE };
- // How many milliseconds between updates to Adafruit.IO
- int publishDelay = 20000;
- // Pin of moisture sensor
- int sensorPin = 0;
- // Trigger threshold for moisture sensor
- int moistureThreshold = 1023;
- // This function is called when the Arduino receives a message from Adafruit.IO
- 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);
- // This function makes the connection to Adafruit.IO
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.print("Attempting MQTT connection...");
- // Attempt to connect
- //
- if (client.connect(MQTTid, MQTTuser, MQTTpsw)) {
- Serial.println("connected");
- // Once connected, publish an announcement...
- // Replace UNIQUE with the number you received in the workshop
- client.publish("REDACTED/feeds/plant-waterer-sensors.pw-messagereceive","PW-UNIQUE connected");
- // ... and resubscribe
- client.subscribe("REDACTED/feeds/plant-waterer-sensors.pw-messagepublish");
- } 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(9600);
- client.setServer("io.adafruit.com", 1883);
- client.setCallback(callback);
- Ethernet.begin(mac);
- // Allow the hardware to sort itself out
- delay(1500);
- }
- void loop()
- {
- if (!client.connected()) {
- reconnect();
- }
- // Read the moisture value
- int sensorValue = analogRead(sensorPin);
- // Compare moisture value to trigger threshold
- if (sensorValue < moistureThreshold) {
- Serial.println("Moisture threshold triggered");
- }
- // Convert the integer value to a char array
- char sensorbuf [4];
- sprintf (sensorbuf, "%03i", sensorValue);
- // This line publishes to a specific feed on your Adafruit.IO account.
- // Change 'UNIQUE' to the number you received in the workshop
- client.publish("REDACTED/feeds/plant-waterer-sensors.pw-UNIQUE", sensorbuf);
- Serial.print("Moisture level: ");
- Serial.println(sensorValue);
- delay(publishDelay);
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment