ESP8266 and MQTT pulse counter for a beehive
- more notes for backup and a bad memory - -
These notes will be upgraded at random times
This first hack is WORKING but not yet tidy, , , ,
Objective - measure the activity of a bee hive.
Perhaps receive an alarm when they swarm.
Method - count the bees in a time window passing in front of an infra-red proximity detector
send to my MQTT broker then grab the data for other programs to process
This gives out pulses when the receive LED sees a reflection from an object.
The diodes will be placed near a beehive entrance to detect bees coming and going.
It can detect a (dead!) bee at up to about 7 cm - plenty near the hive entrance - but not tested yet in the field!
I have an MQTT broker (Mosquitto) running on a Raspberry Pi on my LAN
This sketch will run on an ESP8266 -01 I have the output from the IR detector connected to GPIO2
I hoped to use GPIO0 as well but it has yet to work - later!
The sketch is based on
https://github.com/latonita/esp8266-mqtt-pulsecounter/blob/master/simple_mqtt_pulsecounter.ino
(many thanks)
It is hacked here and there and has yet to be cleaned up, but it works and this is my latest working backup - more later!!
published to Mosquitto as sensors/beecount
NB! There are differentl versions of pubsubclient - this is a backup of the one I used - see - (http://pubsubclient.knolleary.net/ - - - thanks!)
/***************************************************************************************** simple_mqtt_pulsecounterfgm02 ORIGINAL TEXT HEADER #include <ESP8266WiFi.h> extern "C" { const char CompileDate[] = __DATE__ " " __TIME__; #define WIFI_LED_PIN 5 // LED on when connected to Wifi #define SAMPLE_MINUTES 1 #define SENSOR_FAMILY "ESP8266" // final sensor name for MQTT will be SENSOR_FAMILY-SENSOR_NAME-SENSOR_ID //#define MAX_MQTT_FAILURES_BEFORE_REBOOT 10 //not used #define DEBOUNCE_MS 20 //debouncing for interrupts const char *Ssid = "BTHub6-xxxx"; // cannot be longer than 32 characters! there also might be an issue with connection if SSID is less than 8 chars char MqttTopic[] = "sensors/" SENSOR_NAME; volatile unsigned int Pulses1 = 0; volatile unsigned int Pulses2 = 0; volatile unsigned int PulsesPeriods = 0; unsigned int MqttFailures = 0; void mqttCallback(char* topic, byte* payload, unsigned int length) { WiFiClient WifiClient; volatile unsigned long LastMicros1; volatile unsigned long LastMicros2; // TIMER void timerCallback(void *pArg) { PulsesPeriods++; tickOccured = true; void timerInit(void) { void setup() { WiFi.mode(WIFI_STA); // pinMode(4, INPUT_PULLUP); //pinMode(WIFI_LED_PIN, OUTPUT); pinMode(PULSE_PIN1, INPUT); pinMode(PULSE_PIN2, INPUT); tickOccured = false; void loop() { String payload = "{\"d\":{\"N\":\"" SENSOR_NAME "-" SENSOR_ID "\""; */
String payload = ""; Serial.println(payload);
// MqttFailures++; // not sure why I wanted to do this :) so I commented this out later tickOccured = false; digitalWrite(WIFI_LED_PIN, 0); if (WiFi.status() != WL_CONNECTED) { if (WiFi.waitForConnectResult() != WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) { if (MqttClient.connected()) yield(); |
I found few examples of SIMPLE code to record MQTT output data to a file for further processing.
This works. Blassic basic is used to glue various scripts together and capture the data.
mosquitto_sub -v -t 'sensors/beecount' -C 1
grabs the data once and then (because it includes -C 1) closes
A ramdisk is set up at boot time and used to save the SD card
#!/usr/sbin/blassic LABEL GoAgain ' remove the last stored data ' grab the data from the MQTT broker on the computer running this program OPEN "/var/www/html/ramdisk/mqtt_beecount_out.txt" FOR INPUT AS #1 : INPUT #1, mqtt_beecount_out$ : CLOSE #1 PRINT "MQTT beecount is - ", mqtt_beecount_out$ PAUSE 1000 GOTO GoAgain ' (then wait till new data arrives) SYSTEM ' note the # means get all the MQTT data received - for testing |
The printout looks like
MQTT beecount is - sensors/beecount 0 0
MQTT beecount is - sensors/beecount 0 21
MQTT beecount is - sensors/beecount 0 9
MQTT beecount is - sensors/beecount 0 35
So now I will be able to feed it into graph in a webpage - - - - - -soon.
Please email me if you want to swap notes