Howdy,
I'm new to, well... everything as far as code. I have working knowledge of SQL but that's about it.
I have a working weather monitor in RPi that I copied from their tutorial and am trying to recreate it in Arduino. The following code I pieced together from a couple of sources. Mainly from randomnertutorials. It successfully posts temp, pressure and humidity but I want to add the tipping bucket rain gauge.
The code below is registering multiple tips when only one actually occurred.
I've attempted adding a 10k resister on either the D32 or Ground pin. If I copy and run the code from here (Rain Gauge code and ESP32 question) the counter behaves as you'd desire. Not sure what part of that code to try incorporating into this code below.
Thank you for your time to look at this!
/*
Rui Santos
Complete project details at https://RandomNerdTutorials.com/esp32-esp8266-mysql-database-php/
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files.
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
*/
#ifdef ESP32
#include <WiFi.h>
#include <HTTPClient.h>
#else
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#endif
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
//-----------------------------------------------------------------------------------------------------------------
#define RAIN_PIN 32 // the input pin of the rain gauge sensor
unsigned int rainCount = 0; // the count of the number of tips
float rainTotal = 0.0; // the total amount of rainfall in millimeters
//-----------------------------------------------------------------------------------------------------------------
// Replace with your network credentials
const char* ssid = "SSID";
const char* password = "Password";
// REPLACE with your Domain name and URL path or IP address with path
const char* serverName = "http://192.168.1.251/weather/esp-post-data.php";
// Keep this API Key value to be compatible with the PHP code provided in the project page.
// If you change the apiKeyValue value, the PHP file /post-esp-data.php also needs to have the same key
String apiKeyValue = "mykey";
String sensorName = "BME280";
String sensorLocation = "Garden";
/*#include <SPI.h>
#define BME_SCK 18
#define BME_MISO 19
#define BME_MOSI 23
#define BME_CS 5*/
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
void setup() {
Serial.begin(9600);
//-----------------------------------------------------------------------------------------------------------------
pinMode(RAIN_PIN,INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RAIN_PIN),countRain, FALLING);
//-----------------------------------------------------------------------------------------------------------------
WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());
// (you can also pass in a Wire library object like &Wire2)
bool status = bme.begin(0x77);
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring or change I2C address!");
while (1);
}
}
//-----------------------------------------------------------------------------------------------------------------
void countRain() {
rainCount++;
rainTotal += 0.011; // each tip of the rain gauge sensor represents 0.011 inches of rainfall
}
//-----------------------------------------------------------------------------------------------------------------
void loop() {
//Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
WiFiClient client;
HTTPClient http;
// Your Domain name with URL path or IP address with path
http.begin(client, serverName);
// Specify content-type header
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
// Prepare your HTTP POST request data
String httpRequestData = "api_key=" + apiKeyValue + "&sensor=" + sensorName
+ "&location=" + sensorLocation + "&value1=" + String((bme.readTemperature() * 1.8) +32)
+ "&value2=" + String(bme.readHumidity()) + "&value3=" + String(bme.readPressure()/100.0F)
+ "&value4=" + String(rainCount) + "&value5=" + String(rainTotal) + "";
Serial.print("httpRequestData: ");
Serial.println(httpRequestData);
// You can comment the httpRequestData variable above
// then, use the httpRequestData variable below (for testing purposes without the BME280 sensor)
//String httpRequestData = "api_key=tPmAT5Ab3j7F9&sensor=BME280&location=Office&value1=24.75&value2=49.54&value3=1005.14";
// Send HTTP POST request
int httpResponseCode = http.POST(httpRequestData);
// If you need an HTTP request with a content type: text/plain
//http.addHeader("Content-Type", "text/plain");
//int httpResponseCode = http.POST("Hello, World!");
// If you need an HTTP request with a content type: application/json, use the following:
//http.addHeader("Content-Type", "application/json");
//int httpResponseCode = http.POST("{\"value1\":\"19\",\"value2\":\"67\",\"value3\":\"78\"}");
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
else {
Serial.println("WiFi Disconnected");
}
//Send an HTTP POST request every 30 seconds
rainCount = 0;
rainTotal = 0.0;
delay(3000);
}