Screaming Plant
Struggle to care for your plants - you won't when they start screaming.
Components and supplies
1
Adafruit STEMMA Soil Sensor
1
DIY Magnetic Speaker 1W 8 Ohm 50mm
1
DFPlayer - A Mini MP3 Player
1
Seeed Studio XIAO ESP32S3
1
3.7V 1000mAh Lithium Battery
Tools and machines
1
Original Prusa MINI+
Apps and platforms
1
Arduino IoT Cloud
Project description
Code
Main
cpp
This file contains the main logic for the system.
1#include <SoftwareSerial.h> 2#include "Adafruit_seesaw.h" 3#include "thingProperties.h" 4 5 6Adafruit_seesaw ss; 7SoftwareSerial mySerial(1, 2); 8 9bool isSongPlaying = false, delayTimerOn = false; 10unsigned long oldMillisSensor = 0, songStartedMillis = 0, delayMillis = 0; 11 12 13void sendCmd(int cmd, int lb, int hb, bool reply = false) { 14 // standard format for module command stream 15 uint8_t buf[] = {0x7E, 0xFF, 0x00, 0x00, 0x00, 0x00, 0x00, 0xEF}; 16 int16_t chksum = 0; 17 int idx = 3; // position of first command byte 18 19 buf[idx++] = (uint8_t)cmd; // inject command byte in buffer 20 if (reply) buf[idx++] = 0x01; 21 else buf[idx++] = 0x00;// set if reply is needed/wanted 22 23 if (hb >= 0) // if there is a high byte data field 24 buf[idx++] = (uint8_t)hb; // add it to the buffer 25 26 if (lb >= 0) // if there is a low byte data field 27 buf[idx++] = (uint8_t)lb; // add it to the buffer 28 buf[2] = idx - 1; // inject command length into buffer 29 buf[idx++] = 0xEF; // place end-of-command byte 30 31 mySerial.write(buf, idx); // send the command to module 32} 33 34void settingsChangeStop() { 35 if (isSongPlaying) 36 isSongPlaying = false; 37 sendCmd(0x0E, 0, 0, false); 38} 39 40void onSongCountChange() { 41 Serial.print("Files in SD card: "); 42 Serial.println(songCount); 43 settingsChangeStop(); 44} 45 46void onSongVolumeChange() { 47 Serial.print("Volume changed to: "); 48 Serial.println(songVolume); 49 sendCmd(6, songVolume, 0, false); 50} 51 52void onSongLengthChange() { 53 Serial.print("Song length changed to: "); 54 Serial.println(songLength); 55 settingsChangeStop(); 56} 57 58void onSongDelayChange() { 59 Serial.print("Song delay changed to: "); 60 Serial.println(songDelay); 61 settingsChangeStop(); 62} 63 64void onMinCapacitanceChange() { 65 Serial.print("Minimum capacitance changed to: "); 66 Serial.println(minCapacitance); 67 settingsChangeStop(); 68} 69 70void onProbeDelayChange() { 71 Serial.print("Probe delay changed to: "); 72 Serial.println(probeDelay); 73 settingsChangeStop(); 74} 75 76void onTestSongPlaybackChange() { 77 if (testSongPlayback) { 78 sendCmd(0x0F, 2, 1, false); 79 delay(10000); 80 sendCmd(0x0E, 0, 0, false); 81 testSongPlayback = false; 82 } 83} 84 85void setup() { 86 Serial.begin(115200); 87 delay(1500); 88 89 initProperties(); 90 ArduinoCloud.begin(ArduinoIoTPreferredConnection); 91 92 setDebugMessageLevel(2); 93 ArduinoCloud.printDebugInfo(); 94 95 randomSeed(analogRead(0)); 96 97 Serial.println("Connecting To Sensor"); 98 if (!ss.begin(0x36)) { 99 while(1) delay(1); 100 digitalWrite(LED_BUILTIN, HIGH); 101 } else { 102 digitalWrite(LED_BUILTIN, LOW); 103 } 104 105 Serial.println("Connecting To Speaker"); 106 mySerial.begin(9600); //communicate with DF Player 107 delay(3000); //Allow SoftwareSerial to start up 108 109 sendCmd(0x0E, 0, 0, false); 110 delay(200); 111 112 sendCmd(6, 5, 0, false); //command code can be in decimal too. 113 114 Serial.println("Starting Player"); 115 sendCmd(0x01, 0, 0, false); 116 delay(1000); 117 118 // All time units are in seconds 119 probeDelay = 1; 120 songDelay = 5; 121 songLength = 15; 122 songVolume = 10; 123 songCount = 5; 124} 125 126void loop() { 127 ArduinoCloud.update(); 128 129 unsigned long currentMillis = millis(); 130 131 if (currentMillis - oldMillisSensor >= probeDelay * 1000) { 132 sensorReading = ss.touchRead(0); 133 oldMillisSensor = currentMillis; 134 135 Serial.println(sensorReading); 136 137 if ((sensorReading <= minCapacitance) && !delayTimerOn && !isSongPlaying) { 138 isSongPlaying = true; 139 songStartedMillis = currentMillis; 140 sendCmd(0x0F, random(1, songCount + 1), 1, false); 141 } 142 } 143 144 if (isSongPlaying) { 145 if (currentMillis - songStartedMillis >= songLength * 1000) { 146 isSongPlaying = false; 147 sendCmd(0x0E, 0, 0, false); 148 delayTimerOn = true; 149 delayMillis = currentMillis; 150 } 151 } 152 153 if (delayTimerOn) { 154 if (currentMillis - delayMillis >= songDelay * 1000) { 155 delayTimerOn = false; 156 } 157 } 158}
Secrets
cpp
Configure connection to Arduino IoT Cloud.
1// Code generated by Arduino IoT Cloud, DO NOT EDIT. 2 3#include <ArduinoIoTCloud.h> 4#include <Arduino_ConnectionHandler.h> 5 6const char DEVICE_LOGIN_NAME[] = "XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX-XXXXXXXXXXX"; 7 8const char SSID[] = "XXXXXXXXXXX"; // Network SSID (name) 9const char PASS[] = "XXXXXXXXXXX"; // Network password (use for WPA, or use as key for WEP) 10const char DEVICE_KEY[] = "XXXXXXXXXXX"; // Secret device password 11 12void onMinCapacitanceChange(); 13void onProbeDelayChange(); 14void onSongCountChange(); 15void onSongDelayChange(); 16void onSongLengthChange(); 17void onSongVolumeChange(); 18void onTestSongPlaybackChange(); 19 20int minCapacitance; 21int probeDelay; 22int sensorReading; 23int songCount; 24int songDelay; 25int songLength; 26int songVolume; 27bool testSongPlayback; 28 29void initProperties(){ 30 31 ArduinoCloud.setBoardId(DEVICE_LOGIN_NAME); 32 ArduinoCloud.setSecretDeviceKey(DEVICE_KEY); 33 ArduinoCloud.addProperty(minCapacitance, READWRITE, ON_CHANGE, onMinCapacitanceChange, 1); 34 ArduinoCloud.addProperty(probeDelay, READWRITE, ON_CHANGE, onProbeDelayChange, 1); 35 ArduinoCloud.addProperty(sensorReading, READ, ON_CHANGE, NULL); 36 ArduinoCloud.addProperty(songCount, READWRITE, ON_CHANGE, onSongCountChange, 1); 37 ArduinoCloud.addProperty(songDelay, READWRITE, ON_CHANGE, onSongDelayChange, 1); 38 ArduinoCloud.addProperty(songLength, READWRITE, ON_CHANGE, onSongLengthChange, 1); 39 ArduinoCloud.addProperty(songVolume, READWRITE, ON_CHANGE, onSongVolumeChange, 1); 40 ArduinoCloud.addProperty(testSongPlayback, READWRITE, ON_CHANGE, onTestSongPlaybackChange); 41 42} 43 44WiFiConnectionHandler ArduinoIoTPreferredConnection(SSID, PASS);
Downloadable files
Holder
Holds all the parts together.
box.stl
Holder with Clip
Holds all the parts together and lets you clip on.
boxWithClip.stl
Cover
Sensor is fed through the hole.
cover.stl
Speaker Grill
For the aesthetics.
speakerCover.stl
Comments
Only logged in users can leave comments