I assemble some code to the points I can control a relay and a buzzer. What I am trying to do is the relay ON when the temperature sensor higher than X fahrenheit, along with making the buzzer beeping (like "beep… beep… beep…’ and so on). I am ware there Y seconds delay, which I want to keep.
What I have now is the buzzer beep for 1 time only just when the relay start to ON.
I believe I just don’t understand how to have looping on top of looping, or not looping.
Could you please help me to make the relay to ‘beep…beep… beep…(and so on)’ when the relay ON continuously?
Here is the code
#include <ESP8266WiFi.h>
#include "HTTPSRedirect.h"
#include <SPI.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <OneWire.h>
#define OLED_RESET 16
Adafruit_SSD1306 display(OLED_RESET);
OneWire ds(0);
// WIFI thing
const char* ssid = "xxxxxxxxx";
const char* password = "qazwsxedcrfv";
// The ID below comes from Google Sheets.
// Towards the bottom of this page, it will explain how this can be obtained
const char *GScriptId = "qwertyuiopasdfghjkl";
// Push data on this interval
const int dataPostDelay = 300000; // 5 minutes = 5 * 60 * 1000 = 300000
const char* host = "script.google.com";
const char* googleRedirHost = "script.googleusercontent.com";
const int httpsPort = 443;
HTTPSRedirect client(httpsPort);
// Prepare the url (without the varying data)
String url = String("/macros/s/") + GScriptId + "/exec?";
const char* fingerprint = "F0 5C 74 77 3F 6B 25 D7 3B 66 4D 43 2F 7E BC 5B E9 28 86 AD";
// We will take input from GPIO0 pin
byte data[15];
void setup() {
Serial.begin(115200);
pinMode(13, OUTPUT);
pinMode(15, OUTPUT);
display.begin(SSD1306_SWITCHCAPVCC, 0x3C);
display.clearDisplay();
display.setTextColor(WHITE);
display.print("ABCDEF"); display.print("GHIJKLMN");
display.display();
Serial.println("Connecting to wifi: ");
Serial.println(ssid);
Serial.flush();
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.print(" IP address: ");
Serial.println(WiFi.localIP());
Serial.print(String("Connecting to "));
Serial.println(host);
bool flag = false;
for (int i = 0; i < 5; i++) {
int retval = client.connect(host, httpsPort);
if (retval == 1) {
flag = true;
break;
}
else
Serial.println("Connection failed. Retrying...");
}
}
void postData(String tag, float value) {
if (!client.connected()) {
Serial.println("Connecting to client again...");
client.connect(host, httpsPort);
}
String urlFinal = url + "tag=" + tag + "&value=" + String(value);
client.printRedir(urlFinal, host, googleRedirHost);
}
void loop() {
byte i;
byte present = 0;
byte type_s;
byte data[12];
byte addr[8];
float fahrenheit; // float celsius, fahrenheit;
if ( !ds.search(addr)) {
ds.reset_search();
delay(250);
return;
}
for ( i = 0; i < 8; i++) {
}
if (OneWire::crc8(addr, 7) != addr[7]) {
return;
}
ds.reset();
ds.select(addr);
ds.write(0x44, 1); // start conversion, with parasite power on at the end
delay(1000); // maybe 750ms is enough, maybe not
present = ds.reset();
ds.select(addr);
ds.write(0xBE); // Read Scratchpad
for ( i = 0; i < 9; i++) { // we need 9 bytes
data[i] = ds.read();
}
int16_t raw = (data[1] << 8) | data[0];
if (type_s) {
raw = raw << 3; // 9 bit resolution default
if (data[7] == 0x10) {
raw = (raw & 0xFFF0) + 12 - data[6];
}
} else {
byte cfg = (data[4] & 0x60);
if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
}
fahrenheit = ((float)raw / 16.0) * 1.8 + 32.0;
display.clearDisplay();
display.setTextSize(2);
display.setCursor(0, 0);
display.setTextColor(WHITE);
display.println("Probe 1");
display.print(fahrenheit);
display.print(" F");
display.display();
display.clearDisplay();
// Post these information
postData("Temperature", fahrenheit);
delay (dataPostDelay);
//88 Fahrenheit = 31.1 Celcius
//85 Fahrenheit = 29.4 Celcius
if (fahrenheit > 88) {
Serial.println("Fan ON");
digitalWrite(13, LOW); //fan
digitalWrite(15, HIGH); //buzzer
delay(500);
digitalWrite(15, LOW);
}
if (fahrenheit < 85) {
Serial.println("Fan OFF");
digitalWrite(13, HIGH);
digitalWrite(15, LOW);
}
}