Components and supplies
Solar Panel 12W
Arduino MKR WiFi 1010
Battery 3.7V 3.700mAh
Switch for the Roof
DC/DC Stepdaown Converter
Potentiometer 970 Ohm
Tools and machines
The open-source Arduino Software (IDE)
Apps and platforms
Google Sheets
Maker service
Project description
Code
Code for Squirrel Access Control
cpp
You need to fill in your credentials
1#include <WiFiNINA.h> 2#include <ArduinoLowPower.h> 3#include <RTCZero.h> 4 5//For IFTTT webhook 6WiFiClient IftttClient; 7const char* MY_HOST = "maker.ifttt.com"; 8int MY_PORT = 80; 9const char* MY_EVENT = "Your Maker Event Name"; 10const char* MY_API_KEY = "Your Maker Event API Key"; 11 12char ssid[] = "Your Network Name"; // your network SSID (name) 13char pass[] = "Your Network Password"; // your network password (use for WPA, or use as key for WEP) 14 15// Values shown in Google Sheet 16const char* BatteryStat; 17int WiFi_SignalStrenght; 18String BatteryPowerLevel; 19 20RTCZero rtc; // Create an rtc object 21int minutesStatus; 22 23#define MONITOR_PIN A0 // Pin used to monitor battery voltage 24int percentage; 25 26 27void setup() { 28 pinMode(LED_BUILTIN, OUTPUT); 29 analogReference(AR_INTERNAL1V0); // Sets the reference voltage to 1V 30 pinMode(MONITOR_PIN, INPUT); // Set input on pin used to monitor the voltage 31 BatteryStat = "Battery_OK"; 32 33 // Attach a wakeup interrupt on pin 8, calling dummyLoop when the Arduino is woken up 34 pinMode(8, INPUT_PULLUP); 35 LowPower.attachInterruptWakeup(8, dummyLoop, FALLING); 36 37 //Initialize serial and wait for port to open: 38 Serial.begin(9600); 39 while (!Serial) 40 ; 41 delay(5000); 42 43 // initialize RTC 44 rtc.begin(); 45 46 // Show, that system is working on Build-in LED after power-up 47 digitalWrite(LED_BUILTIN, HIGH); 48 delay(500); 49 digitalWrite(LED_BUILTIN, LOW); 50 delay(500); 51 digitalWrite(LED_BUILTIN, HIGH); 52 delay(500); 53 digitalWrite(LED_BUILTIN, LOW); 54 delay(500); 55} 56 57void loop() { 58 59 // Go to deep sleep until Squirrel is coming (Interupt on pin 8) 60 Serial.println("Go to deep sleep until Squirrel is comming :-)"); 61 LowPower.deepSleep(); 62 63 // When a Squirrel approaches, wake up Arduino and start WiFi 64 StartWiFi(); 65 if (WiFi.status() == WL_CONNECTED) Serial.println("WiFi is on"); 66 67 // Hold WiFi for 10 minutes active to save power 68 rtc.setMinutes(0); 69 minutesStatus = rtc.getMinutes(); 70 71 // In order to get the first Squirrel attempt, make an entry into Google Sheets and then starting the 10 minutes loop 72 // WiFi signal strenght: -50 is still ok. Everything nearer to 0 is better 73 WiFi_SignalStrenght = WiFi.RSSI(); 74 Serial.print("Signal strength (RSSI) = "); 75 Serial.println(WiFi_SignalStrenght); 76 77 // Get the current Battery voltage and send it to Google Sheets via ifttt 78 float voltage = BatteryVoltage(); 79 percentage = int(voltage); 80 if (percentage <= 30) BatteryStat = "Low_Battery"; 81 BatteryPowerLevel = String(percentage) + "%"; 82 send_values_to_ifttt(BatteryStat, WiFi_SignalStrenght, BatteryPowerLevel); 83 84 // Starting the 10 minutes loop... 85 while (minutesStatus < 10) { 86 if (digitalRead(8) == LOW) { 87 float voltage = BatteryVoltage(); 88 percentage = int(voltage); // convert float to integer 89 Serial.print("Voltage: "); 90 Serial.print(voltage); 91 Serial.print("V / Percentage: "); 92 Serial.println(percentage); 93 if (percentage <= 30) BatteryStat = "Low_Battery"; 94 95 // Send the current Battery voltage to Google Sheets via ifttt 96 BatteryPowerLevel = String(percentage) + "%"; // convert percentage to string and add % sign 97 send_values_to_ifttt(BatteryStat, WiFi_SignalStrenght, BatteryPowerLevel); 98 } 99 minutesStatus = rtc.getMinutes(); 100 } 101 102 // Close down WiFi connection for battery saving after 10 minutes active 103 WiFi.end(); 104 if (WiFi.status() != WL_CONNECTED) Serial.println("WiFi is off"); 105} 106 107 108float BatteryVoltage() { // Read Battery Power Level 109 // Divider: (Reference Voltage (1V) - Minimal Voltage (0.75V)) / Refernce Voltage (1V) => (1024-750)/1024 = 3.74 (Correction: 2,5) 110 float divider = 2.5; 111 112 // Battery voltage between 0-100% => (1024-((1024-x) * Divider)) / 10 113 float reading = analogRead(MONITOR_PIN); 114 reading = 1024 - reading; 115 reading = reading * divider; 116 reading = 1024.0 - reading; 117 reading = reading / 10; 118 return reading; 119} 120 121 122void send_values_to_ifttt(const char* BatteryStat, int WiFi_SignalStrenght, String BatteryPowerLevel) { 123 if (IftttClient.connect(MY_HOST, MY_PORT)) { // Connect with server and send message 124 Serial.println("Connected to IFTTT, sending... "); 125 126 IftttClient.println(String("GET ") + "/trigger/" + String(MY_EVENT) + "/with/key/" + String(MY_API_KEY) + "?value1=" 127 + BatteryStat + "&value2=" + WiFi_SignalStrenght + "&value3=" + BatteryPowerLevel 128 + " HTTP/1.1"); 129 130 IftttClient.println("Host: " + String(MY_HOST)); 131 IftttClient.println("Connection: close"); 132 IftttClient.println(); 133 Serial.println("----------------------------------------"); 134 delay(5000); 135 IftttClient.stop(); // Disconnect from the server 136 } else { 137 Serial.println("Failed to connect to client"); 138 } 139} 140 141 142void StartWiFi() { 143 // attempt to connect to Wifi network: 144 while (WiFi.status() != WL_CONNECTED) { 145 Serial.print("Attempting to connect to network: "); 146 Serial.println(ssid); 147 148 // Connect to WPA/WPA2 network: 149 WiFi.begin(ssid, pass); 150 Serial.println(" connected!"); 151 Serial.println(""); 152 153 // wait 10 seconds for connection: 154 delay(10000); 155 } 156} 157 158 159void dummyLoop() {} //do nothing - just for the interupt
Comments
Only logged in users can leave comments