Hackster is hosting Hackster Holidays, Finale: Livestream & Giveaway Drawing. Watch previous episodes or stream live on Tuesday!Stream Hackster Holidays, Finale on Tuesday!
Istvan Sipka
Published © GPL3+

Alarm Cube for Greenhouse

Battery-powered, ESP8266-based Alarm Cube warns when there is no water pressure in the main water pipe or electricity!

IntermediateFull instructions provided18 hours2,695
Alarm Cube for Greenhouse

Things used in this project

Hardware components

ESP8266 ESP-01
Espressif ESP8266 ESP-01
×1
18650 sized Li-Ion battery
×1
Microchip MCP1700 LDO
×1
Varistor/Capacitor Combination for EMI/Surge Suppression - CG21AS26F105MR
×1
TP4056 battery charger board with protection
×1
Photocoupler
×2
5V AC-DC PSU
×1
SparkFun FTDI Basic Breakout - 3.3V
SparkFun FTDI Basic Breakout - 3.3V
×1

Software apps and online services

Arduino IDE
Arduino IDE
ThingSpeak API
ThingSpeak API

Hand tools and fabrication machines

Soldering iron (generic)
Soldering iron (generic)

Story

Read more

Schematics

Alarm Cube for Greenhouse

alarmcube02_bb_WYsWMkYpfe.jpg

Code

alarmcube02hackster.ino

Arduino
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include <Metro.h>

unsigned long myChannelNumber = "*****";
const char * myWriteAPIKey = "******";

const long ThingspeakDelay = 300000; //waits 5 minutes between uploading status
const int blinkValue = 300;
const int timeValue = 2000;
const long checkValue = 3600000; //1 hour

Metro upload = Metro(ThingspeakDelay);
Metro ledBlink = Metro(blinkValue);
Metro statusTime = Metro(timeValue);
Metro checkTime = Metro(checkValue);



const char* ssid     = "************";
const char* password = "************";
int status = WL_IDLE_STATUS;
WiFiClient client;

//pin numbers:
const int pressureSwitch = 2;     
const int ledPin = 0;


int switchState =HIGH;         // variable for reading the pressure switch
int ledState =LOW;          // An ON status for LED
int TempcheckTime = LOW;    //

void setup() {
  
  pinMode(ledPin, OUTPUT);
  pinMode(pressureSwitch, INPUT);
  Serial.begin(9600);
  delay(10);
  ThingSpeak.begin(client);
  
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  
  
  WiFi.mode(WIFI_STA);
  WiFi.disconnect(true);  // Erases SSID/password
  WiFi.setPhyMode(WIFI_PHY_MODE_11B);
  WiFi.begin(ssid, password);
  int tries = 0;
  while (WiFi.status() != WL_CONNECTED && tries < 20) {
    Serial.print("Connecting due to checking: ");
        Serial.println(ssid);
    // wait 10 seconds for connection:
    delay(500);
    tries++;
  }
  Serial.println("");
  Serial.println("WiFi connected"); 
  digitalWrite(ledPin, ledState); 
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);
  int switchState = digitalRead(pressureSwitch);
  Serial.println("Pressure state is: ");
  Serial.println(switchState);
  if (switchState = LOW){
  Serial.print("Uploading to ThingSpeak the current state of pressure switch.");
  ThingSpeak.writeField(myChannelNumber, 1, switchState, myWriteAPIKey);
  Serial.print("Upload is complete");
  }
  WiFi.disconnect(true);  // Erases SSID/password
  Serial.println("Wifi disconnected");
  Serial.println("IP address: ");
  Serial.println(WiFi.localIP());
  if (switchState == LOW) {
    switchState=HIGH;
    }
  upload.reset();
  statusTime.reset();
}

void loop() {//*****************************************************************************************************
  
  int TempswitchState = digitalRead(pressureSwitch);
  if(TempswitchState == LOW){ //If there is no water
    switchState=HIGH;
  }
  else{
    switchState=LOW;
  }
  
  if((checkTime.check() == 1 && TempswitchState == HIGH) || (TempcheckTime == HIGH && TempswitchState == HIGH)){
    delay(1000);
    Serial.println("RESET software state in ThingSpeak");
    TempcheckTime=LOW;
    digitalWrite(ledPin, LOW); 
    int wifitries = 0;
      while ((WiFi.status() != WL_CONNECTED) && wifitries < 2 ) {
        Serial.print("Attempting to connect to WPA SSID to reset: ");
        Serial.println(ssid);
        //Connect to WPA/WPA2 network:
        status = WiFi.begin(ssid, password);

    // wait 10 seconds for connection:
    delay(10000);
    wifitries++;
  }
  
  
  if(WiFi.status() == WL_CONNECTED){
    long rssi = WiFi.RSSI();
    Serial.println("RSSI: ");
    Serial.println(rssi);
    Serial.println("Uploading RESET status to Thingspeak");
    ThingSpeak.writeField(myChannelNumber, 1, switchState, myWriteAPIKey);
    WiFi.disconnect(true);  // Erases SSID/password
    delay(500);
    Serial.println("Wifi disconnected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
  }
  }
  
  
  if(switchState == LOW){ //LOW means water pressure is OK
    digitalWrite(ledPin, HIGH); // HIGH means LED is off
  }
  if(statusTime.check()==1){
    Serial.println("Reading switchState: ");
    if(switchState == LOW){ //LOW means ok
      Serial.println("PRESSURE is OK");
  }
  else{
    Serial.println("NO WATER or CHECK MAINS!");
  }
  }
  
  
  // check if the pushbutton is pressed. If it is, the buttonState is HIGH:
  if (upload.check() == 1 && switchState == HIGH) {
    digitalWrite(ledPin, LOW); 
    int wifitries = 0;
      while ((WiFi.status() != WL_CONNECTED) && wifitries < 2 ) {
        Serial.print("Attempting to connect to WPA SSID: ");
        Serial.println(ssid);
        //Connect to WPA/WPA2 network:
        status = WiFi.begin(ssid, password);

    // wait 10 seconds for connection:
    delay(10000);
    wifitries++;
  }
  
  
  if(WiFi.status() == WL_CONNECTED){
    long rssi = WiFi.RSSI();
    Serial.println("RSSI: ");
    Serial.println(rssi);
    Serial.println("Uploading status to Thingspeak");
    ThingSpeak.writeField(myChannelNumber, 1, switchState, myWriteAPIKey);
    WiFi.disconnect(true);  // Erases SSID/password
    delay(500);
    Serial.println("Wifi disconnected");
    Serial.println("IP address: ");
    Serial.println(WiFi.localIP());
    TempcheckTime = HIGH;
  }
  }
  
  if(switchState == HIGH && ledBlink.check() == 1){ // blinking the LED without delay() when no water
      if (ledState==HIGH)  { 
        ledState=LOW;
        ledBlink.interval(blinkValue);
      } 
      else {
        ledBlink.interval(blinkValue);
        ledState=HIGH;
      }
    digitalWrite(ledPin,ledState);
  }
}

Credits

Istvan Sipka
4 projects • 33 followers
Heart of an engineer mind of an economist. Actually an entreprenuer at the field of growing sweet pepper. My goal is: IoT for Plants!

Comments