Farm-Data-Relay-System/Sensors/TippingBucket/TippingBucket.ino
2021-11-26 12:29:34 -06:00

68 lines
1.8 KiB
C++

// FARM DATA RELAY SYSTEM
//
// TIPPING BUCKET RAINFALL SENSOR MODULE
//
// Developed by Timm Bogner (bogner1@gmail.com) for Sola Gratia Farm in Urbana, Illinois, USA.
// Each reading is assigned a two-byte identifier along with a one-byte sensor type
//
#define READING_ID 45 //Unique integer for each data reading
#define GTWY_MAC 0x00 //Gateway MAC
#define REED_PIN 2
#include <ESP8266WiFi.h>
#include <espnow.h>
#include "DataReading.h"
unsigned int theCount = 0;
unsigned long lastTrigger = 0;
boolean clicked = false;
uint8_t broadcastAddress[] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, GTWY_MAC};
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("Last Packet Send Status: ");
if (sendStatus == 0) {
Serial.println("Delivery success");
}
else {
Serial.println("Delivery fail");
}
}
// Checks if motion was detected, sets LED HIGH and starts a timer
ICACHE_RAM_ATTR void detectsMovement() {
clicked = true;
lastTrigger = millis();
}
void setup() {
// Serial port for debugging purposes
Serial.begin(115200);
WiFi.mode(WIFI_STA);
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_COMBO, 0, NULL, 0);
pinMode(REED_PIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(REED_PIN), detectsMovement, FALLING);
}
void loop() {
if (clicked && millis() - lastTrigger > 100) {
theCount++;
Serial.print("CLICK.");
Serial.println(theCount);
clicked = false;
DataReading Rain;
Rain.d = theCount;
Rain.id = READING_ID;
Rain.t = 7;
esp_now_send(broadcastAddress, (uint8_t *) &Rain, sizeof(Rain));
}
}