#ifdef ESP32 #include #else #include #endif //input pin 34 const int analogIn = 34; #include // Replace with your SSID const char* ssid = "OSU_Unsecured"; // Replace with your unique IFTTT URL resource const char* resource = "/trigger/ESP32_R/json/with/key/bXMAIh1gE3u4dSFCYe1JWK"; // this is the key to access the directory of the //const char* resource = "/trigger/bme280_readings/with/key/nAZjOphL3d-ZO4N3k64-1A7gTlNSrxMJdmqy3"; // Maker Webhooks IFTTT const char* server = "maker.ifttt.com"; // Time to sleep uint64_t uS_TO_S_FACTOR = 1000000; // Conversion factor for micro seconds to seconds // 540 seconds = 9 minutes so that it can spend a minute testing for 10 minute intervals uint64_t TIME_TO_SLEEP = 540; void setup() { Serial.begin(115200); delay(2000); initWifi(); makeIFTTTRequest(); #ifdef ESP32 // enable timer deep sleep esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * uS_TO_S_FACTOR); Serial.println("Going to sleep now"); // start deep sleep for 3600 seconds (60 minutes) esp_deep_sleep_start(); #else // Deep sleep mode for 3600 seconds (60 minutes) Serial.println("Going to sleep now"); ESP.deepSleep(TIME_TO_SLEEP * uS_TO_S_FACTOR); #endif } void loop() { // sleeping so wont get here } // Establish a Wi-Fi connection with your router void initWifi() { Serial.print("Connecting to: "); Serial.print(ssid); WiFi.begin(ssid); int timeout = 10 * 4; // 10 seconds while(WiFi.status() != WL_CONNECTED && (timeout-- > 0)) { delay(250); Serial.print("."); } Serial.println(""); if(WiFi.status() != WL_CONNECTED) { Serial.println("Failed to connect, going back to sleep"); } Serial.print("WiFi connected in: "); Serial.print(millis()); Serial.print(", IP address: "); Serial.println(WiFi.localIP()); } void DataFinder(double& Averg,double& HA,double& LA){ int i; double Value= 0; double ACSoffset = 2500; // ofset for the ACS712 double mVAmp = 100;// mv per amp for your sensor you can find this on its data sheet double Amps = 0; double Voltage = 0; double AbsAmps; double total; int tests = 10; //this makes sure that the code runs for a full 60 interval whether it be be seconds or minutes for(i = 0; i < tests; i++){ Value = analogRead(34); Voltage = (Value / 1024) * 5000; // converts the signal in to mA Amps = ((Voltage - ACSoffset) / mVAmp); Serial.print("\t Amps = "); // displays the current measured Serial.println(Amps,3); // sets the number of digits displayed after the decimal point AbsAmps = abs(Amps); Serial.print("\t AbsAmps = "); Serial.println(AbsAmps,3); //This is how values are recorded in a readable data format total = total + AbsAmps; delay(1000); //else if statement for lowest and highest value recorded over the period if(AbsAmps > HA){ HA = AbsAmps; } else if(AbsAmps < LA){ LA = AbsAmps; Serial.println(i); } } Averg = total / tests; //gets the average of the data Serial.println(Averg); Serial.println(LA); Serial.println(HA); return; } // Make an HTTP request to the IFTTT web service void makeIFTTTRequest() { //the pin the sensor is connected to double Average; double LAmps; double HAmps; Serial.print("Connecting to "); Serial.print(server); WiFiClient client; int retries = 5; while(!!!client.connect(server, 80) && (retries-- > 0)) { Serial.print("."); } Serial.println(); if(!!!client.connected()) { Serial.println("Failed to connect..."); } Serial.print("Request resource: "); Serial.println(resource); DataFinder(Average,LAmps,HAmps); Serial.println(Average); Serial.println(LAmps); Serial.println(HAmps); //Jsonstring that is sent to the 3rd party source String jsonObject = String("{\"value1\":\"") + Average + "\",\"value2\":\"" + LAmps + "\",\"value3\":\"" + HAmps + "\"}"; Serial.println(jsonObject); String url = "/trigger/ESP32_R/with/key/bXMAIh1gE3u4dSFCYe1JWK"; client.print("POST " + url + " HTTP/1.1\r\n"); client.print("Host: maker.ifttt.com\r\n"); client.print("Content-Type: application/json\r\n"); client.print("Content-Length: " + String(jsonObject.length()) + "\r\n"); client.print("\r\n"); client.print(jsonObject); int timeout = 5 * 10; // 5 seconds while(!!!client.available() && (timeout-- > 0)){ delay(100); } if(!!!client.available()) { Serial.println("No response..."); } while(client.available()){ Serial.write(client.read()); } Serial.println("\nclosing connection"); client.stop(); }