mirror of
https://github.com/letscontrolit/ESPEasy.git
synced 2026-07-28 04:07:47 +00:00
ready to use commit
This commit is contained in:
@@ -1,108 +0,0 @@
|
||||
/***************************************************************************
|
||||
This is a library example for the HMC5883 magnentometer/compass
|
||||
|
||||
Designed specifically to work with the Adafruit HMC5883 Breakout
|
||||
http://www.adafruit.com/products/1746
|
||||
|
||||
*** You will also need to install the Adafruit_Sensor library! ***
|
||||
|
||||
These displays use I2C to communicate, 2 pins are required to interface.
|
||||
|
||||
Adafruit invests time and resources providing this open source code,
|
||||
please support Adafruit andopen-source hardware by purchasing products
|
||||
from Adafruit!
|
||||
|
||||
Written by Kevin Townsend for Adafruit Industries with some heading example from
|
||||
Love Electronics (loveelectronics.co.uk)
|
||||
|
||||
This program is free software: you can redistribute it and/or modify
|
||||
it under the terms of the version 3 GNU General Public License as
|
||||
published by the Free Software Foundation.
|
||||
|
||||
This program is distributed in the hope that it will be useful,
|
||||
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||
GNU General Public License for more details.
|
||||
|
||||
You should have received a copy of the GNU General Public License
|
||||
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
|
||||
***************************************************************************/
|
||||
|
||||
#include <Wire.h>
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_HMC5883_U.h>
|
||||
|
||||
/* Assign a unique ID to this sensor at the same time */
|
||||
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
|
||||
|
||||
void displaySensorDetails(void)
|
||||
{
|
||||
sensor_t sensor;
|
||||
mag.getSensor(&sensor);
|
||||
Serial.println("------------------------------------");
|
||||
Serial.print ("Sensor: "); Serial.println(sensor.name);
|
||||
Serial.print ("Driver Ver: "); Serial.println(sensor.version);
|
||||
Serial.print ("Unique ID: "); Serial.println(sensor.sensor_id);
|
||||
Serial.print ("Max Value: "); Serial.print(sensor.max_value); Serial.println(" uT");
|
||||
Serial.print ("Min Value: "); Serial.print(sensor.min_value); Serial.println(" uT");
|
||||
Serial.print ("Resolution: "); Serial.print(sensor.resolution); Serial.println(" uT");
|
||||
Serial.println("------------------------------------");
|
||||
Serial.println("");
|
||||
delay(500);
|
||||
}
|
||||
|
||||
void setup(void)
|
||||
{
|
||||
Serial.begin(9600);
|
||||
Serial.println("HMC5883 Magnetometer Test"); Serial.println("");
|
||||
|
||||
/* Initialise the sensor */
|
||||
if(!mag.begin())
|
||||
{
|
||||
/* There was a problem detecting the HMC5883 ... check your connections */
|
||||
Serial.println("Ooops, no HMC5883 detected ... Check your wiring!");
|
||||
while(1);
|
||||
}
|
||||
|
||||
/* Display some basic information on this sensor */
|
||||
displaySensorDetails();
|
||||
}
|
||||
|
||||
void loop(void)
|
||||
{
|
||||
/* Get a new sensor event */
|
||||
sensors_event_t event;
|
||||
mag.getEvent(&event);
|
||||
|
||||
/* Display the results (magnetic vector values are in micro-Tesla (uT)) */
|
||||
Serial.print("X: "); Serial.print(event.magnetic.x); Serial.print(" ");
|
||||
Serial.print("Y: "); Serial.print(event.magnetic.y); Serial.print(" ");
|
||||
Serial.print("Z: "); Serial.print(event.magnetic.z); Serial.print(" ");Serial.println("uT");
|
||||
|
||||
// Hold the module so that Z is pointing 'up' and you can measure the heading with x&y
|
||||
// Calculate heading when the magnetometer is level, then correct for signs of axis.
|
||||
float heading = atan2(event.magnetic.y, event.magnetic.x);
|
||||
|
||||
// Once you have your heading, you must then add your 'Declination Angle', which is the 'Error' of the magnetic field in your location.
|
||||
// Find yours here: http://www.magnetic-declination.com/
|
||||
// Mine is: -13* 2' W, which is ~13 Degrees, or (which we need) 0.22 radians
|
||||
// If you cannot find your Declination, comment out these two lines, your compass will be slightly off.
|
||||
float declinationAngle = 0.22;
|
||||
heading += declinationAngle;
|
||||
|
||||
// Correct for when signs are reversed.
|
||||
if(heading < 0)
|
||||
heading += 2*PI;
|
||||
|
||||
// Check for wrap due to addition of declination.
|
||||
if(heading > 2*PI)
|
||||
heading -= 2*PI;
|
||||
|
||||
// Convert radians to degrees for readability.
|
||||
float headingDegrees = heading * 180/M_PI;
|
||||
|
||||
Serial.print("Heading (degrees): "); Serial.println(headingDegrees);
|
||||
|
||||
delay(500);
|
||||
}
|
||||
+115
-121
@@ -1,6 +1,7 @@
|
||||
#ifdef PLUGIN_BUILD_DEV //at the very beginning
|
||||
|
||||
#include "_Plugin_Helper.h"
|
||||
#ifdef PLUGIN_BUILD_DEV //at the very beginning
|
||||
|
||||
#ifdef USES_P121
|
||||
|
||||
// #######################################################################################################
|
||||
@@ -17,148 +18,141 @@
|
||||
* Used P106 BME680 as starting point
|
||||
/******************************************************************************/
|
||||
|
||||
# include <Adafruit_Sensor.h>
|
||||
# include <Adafruit_HMC5883_U.h>
|
||||
#include "src/PluginStructs/P121_data_struct.h"
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_HMC5883_U.h>
|
||||
|
||||
#define PLUGIN_121
|
||||
#define PLUGIN_ID_121 121
|
||||
#define PLUGIN_NAME_121 "Environment - HCM5883L"
|
||||
#define PLUGIN_VALUENAME1_121 "x"
|
||||
#define PLUGIN_VALUENAME2_121 "y"
|
||||
#define PLUGIN_VALUENAME3_121 "z"
|
||||
#define PLUGIN_VALUENAME4_121 "dir"
|
||||
|
||||
# define PLUGIN_121
|
||||
# define PLUGIN_ID_121 121
|
||||
# define PLUGIN_NAME_121 "Environment - HCM5883L"
|
||||
# define PLUGIN_VALUENAME1_121 "x"
|
||||
# define PLUGIN_VALUENAME2_121 "y"
|
||||
# define PLUGIN_VALUENAME3_121 "z"
|
||||
|
||||
|
||||
boolean Plugin_121(uint8_t function, struct EventStruct *event, String& string)
|
||||
boolean Plugin_121(uint8_t function, struct EventStruct *event, String &string)
|
||||
{
|
||||
boolean success = false;
|
||||
boolean initialized = false;
|
||||
|
||||
Adafruit_HMC5883_Unified mag; // I2C
|
||||
|
||||
switch (function)
|
||||
{
|
||||
case PLUGIN_DEVICE_ADD:
|
||||
{
|
||||
Device[++deviceCount].Number = PLUGIN_ID_121;
|
||||
Device[deviceCount].Type = DEVICE_TYPE_I2C;
|
||||
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_QUAD;
|
||||
Device[deviceCount].Ports = 0;
|
||||
Device[deviceCount].PullUpOption = false;
|
||||
Device[deviceCount].InverseLogicOption = false;
|
||||
Device[deviceCount].FormulaOption = true;
|
||||
Device[deviceCount].ValueCount = 3;
|
||||
Device[deviceCount].SendDataOption = true;
|
||||
Device[deviceCount].TimerOption = true;
|
||||
Device[deviceCount].GlobalSyncOption = true;
|
||||
break;
|
||||
}
|
||||
case PLUGIN_DEVICE_ADD:
|
||||
{
|
||||
Device[++deviceCount].Number = PLUGIN_ID_121;
|
||||
Device[deviceCount].Type = DEVICE_TYPE_I2C;
|
||||
Device[deviceCount].VType = Sensor_VType::SENSOR_TYPE_QUAD;
|
||||
Device[deviceCount].Ports = 0;
|
||||
Device[deviceCount].PullUpOption = false;
|
||||
Device[deviceCount].InverseLogicOption = false;
|
||||
Device[deviceCount].FormulaOption = true;
|
||||
Device[deviceCount].ValueCount = 4;
|
||||
Device[deviceCount].SendDataOption = true;
|
||||
Device[deviceCount].TimerOption = true;
|
||||
Device[deviceCount].GlobalSyncOption = true;
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_GET_DEVICENAME:
|
||||
{
|
||||
string = F(PLUGIN_NAME_121);
|
||||
break;
|
||||
}
|
||||
case PLUGIN_GET_DEVICENAME:
|
||||
{
|
||||
string = F(PLUGIN_NAME_121);
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_GET_DEVICEVALUENAMES:
|
||||
{
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_121));
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_121));
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_121));
|
||||
break;
|
||||
}
|
||||
case PLUGIN_GET_DEVICEVALUENAMES:
|
||||
{
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_121));
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_121));
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[2], PSTR(PLUGIN_VALUENAME3_121));
|
||||
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[3], PSTR(PLUGIN_VALUENAME4_121));
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
case PLUGIN_WEBFORM_SHOW_I2C_PARAMS:
|
||||
{
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
case PLUGIN_I2C_HAS_ADDRESS:
|
||||
{
|
||||
success = (event->Par1 == 0x1E);
|
||||
break;
|
||||
}
|
||||
case PLUGIN_WEBFORM_LOAD:
|
||||
{
|
||||
addFormFloatNumberBox(F("DeclinationAngle"), F("plugin_121_HMC5883L_decl"), PCONFIG(0), -2.0f,2.0f ,5, 0.00001f);
|
||||
addUnit(F("degree"));
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_I2C_HAS_ADDRESS:
|
||||
case PLUGIN_WEBFORM_SHOW_I2C_PARAMS:
|
||||
{
|
||||
break;
|
||||
}
|
||||
case PLUGIN_WEBFORM_SAVE:
|
||||
{
|
||||
PCONFIG(0) = getFormItemFloat(F("plugin_121_HMC5883L_decl"));
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_WEBFORM_LOAD:
|
||||
{
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_WEBFORM_SAVE:
|
||||
{
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
case PLUGIN_INIT:
|
||||
{
|
||||
initPluginTaskData(event->TaskIndex);
|
||||
getPluginTaskData(event->TaskIndex);
|
||||
|
||||
mag = Adafruit_HMC5883_Unified(12345);
|
||||
|
||||
if (!mag.begin())
|
||||
{
|
||||
addLog(LOG_LEVEL_ERROR, F("HCM5883L : Ooops, no HMC5883 detected ... Check your wiring!"));
|
||||
success = false;
|
||||
break;
|
||||
}
|
||||
|
||||
sensor_t sensor;
|
||||
mag.getSensor(&sensor);
|
||||
|
||||
|
||||
String sensorStr = F("Sensor: ");
|
||||
sensorStr += sensor.name;
|
||||
|
||||
String driverStr = F("Driver Ver: ");
|
||||
driverStr += sensor.version;
|
||||
|
||||
String uniqueIdStr = F("Unique ID: ");
|
||||
uniqueIdStr += sensor.sensor_id;
|
||||
|
||||
String maxValStr = F("Max Value: ");
|
||||
maxValStr += sensor.max_value;
|
||||
|
||||
String minValStr =F("Min Value: ");
|
||||
minValStr += sensor.min_value;
|
||||
|
||||
String resolutionStr =F("Resolution: ");
|
||||
resolutionStr += sensor.resolution;
|
||||
|
||||
addLog(LOG_LEVEL_DEBUG, F("------------------------------------"));
|
||||
addLog(LOG_LEVEL_DEBUG, sensorStr);
|
||||
addLog(LOG_LEVEL_DEBUG, driverStr);
|
||||
addLog(LOG_LEVEL_DEBUG, uniqueIdStr);
|
||||
addLog(LOG_LEVEL_DEBUG, maxValStr);
|
||||
addLog(LOG_LEVEL_DEBUG, minValStr);
|
||||
addLog(LOG_LEVEL_DEBUG, resolutionStr);
|
||||
addLog(LOG_LEVEL_DEBUG, F("------------------------------------"));
|
||||
break;
|
||||
}
|
||||
case PLUGIN_READ:
|
||||
{
|
||||
P121_data_struct *P121_data =
|
||||
case PLUGIN_INIT:
|
||||
{
|
||||
initPluginTaskData(event->TaskIndex, new (std::nothrow) P121_data_struct());
|
||||
P121_data_struct *P121_data =
|
||||
static_cast<P121_data_struct *>(getPluginTaskData(event->TaskIndex));
|
||||
|
||||
if (nullptr != P121_data)
|
||||
{
|
||||
P121_data->initialized = false; // Force re-init just in case the address changed.
|
||||
P121_data->begin();
|
||||
success = P121_data->initialized;
|
||||
}
|
||||
break;
|
||||
}
|
||||
case PLUGIN_READ:
|
||||
{
|
||||
P121_data_struct *P121_data =
|
||||
static_cast<P121_data_struct *>(getPluginTaskData(event->TaskIndex));
|
||||
|
||||
|
||||
sensors_event_t s_event;
|
||||
if (!mag.getEvent(&s_event))
|
||||
addLog(LOG_LEVEL_ERROR, F("HCM5883L : Failed to perform reading!"));
|
||||
success = false;
|
||||
if (nullptr != P121_data)
|
||||
{
|
||||
P121_data->begin();
|
||||
|
||||
if (!P121_data->initialized)
|
||||
{
|
||||
addLog(LOG_LEVEL_ERROR, "Could not initialize HCM5883L");
|
||||
break;
|
||||
}
|
||||
|
||||
String values = "HCM5883L : x: " + String(s_event.magnetic.x) + ", y: " + String(s_event.magnetic.y) + ", z: " + String(s_event.magnetic.z);
|
||||
addLog(LOG_LEVEL_DEBUG, values);
|
||||
|
||||
sensors_event_t s_event;
|
||||
P121_data->mag.getEvent(&s_event);
|
||||
UserVar[event->BaseVarIndex + 0] = s_event.magnetic.x;
|
||||
UserVar[event->BaseVarIndex + 1] = s_event.magnetic.y;
|
||||
UserVar[event->BaseVarIndex + 2] = s_event.magnetic.z;
|
||||
UserVar[event->BaseVarIndex + 2] = s_event.magnetic.z;
|
||||
|
||||
float heading = atan2(s_event.magnetic.y, s_event.magnetic.x);
|
||||
|
||||
const float decl = PCONFIG(0);
|
||||
addLog(LOG_LEVEL_DEBUG, "Declination Degree:" + String(decl));
|
||||
if (decl != 0)
|
||||
{
|
||||
heading += decl;
|
||||
}
|
||||
|
||||
if (heading < 0)
|
||||
heading += 2*PI;
|
||||
|
||||
if (heading > 2*PI)
|
||||
heading -= 2*PI;
|
||||
|
||||
float degreeHeading = heading * 180/M_PI;
|
||||
UserVar[event->BaseVarIndex + 3] = degreeHeading;
|
||||
|
||||
|
||||
success = true;
|
||||
break;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
}
|
||||
return success;
|
||||
}
|
||||
|
||||
#endif // ifdef USES_P121
|
||||
#endif //at the very end
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
#include "../PluginStructs/P121_data_struct.h"
|
||||
|
||||
#ifdef USES_P121
|
||||
|
||||
// Needed also here for PlatformIO's library finder as the .h file
|
||||
// is in a directory which is excluded in the src_filter
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_HMC5883_U.h>
|
||||
|
||||
bool P121_data_struct::begin(bool initSettings)
|
||||
{
|
||||
if (!initialized)
|
||||
{
|
||||
initialized = mag.begin();
|
||||
|
||||
if (initialized)
|
||||
{
|
||||
// Set up oversampling and filter initialization
|
||||
sensor_t sensor;
|
||||
mag.getSensor(&sensor);
|
||||
addLog(LOG_LEVEL_DEBUG, F("------------------------------------"));
|
||||
String log = F("Sensor: ");
|
||||
log += sensor.name;
|
||||
log += "\nDriver Ver: " + sensor.version;
|
||||
log += "\nUnique ID: " + sensor.sensor_id;
|
||||
log += "\nMax Value: " + String(sensor.max_value);
|
||||
log += "\nMin Value: " + String(sensor.min_value);
|
||||
log += "\nResolution: " + String(sensor.resolution);
|
||||
addLog(LOG_LEVEL_DEBUG, log);
|
||||
addLog(LOG_LEVEL_DEBUG, F("------------------------------------"));
|
||||
}
|
||||
}
|
||||
|
||||
return initialized;
|
||||
}
|
||||
|
||||
#endif // ifdef USES_P121
|
||||
@@ -0,0 +1,20 @@
|
||||
#ifndef PLUGINSTRUCTS_P121_DATA_STRUCT_H
|
||||
#define PLUGINSTRUCTS_P121_DATA_STRUCT_H
|
||||
|
||||
#include "../../_Plugin_Helper.h"
|
||||
#ifdef USES_P121
|
||||
|
||||
#include <Adafruit_Sensor.h>
|
||||
#include <Adafruit_HMC5883_U.h>
|
||||
|
||||
struct P121_data_struct : public PluginTaskData_base
|
||||
{
|
||||
bool begin(bool initSettings = true);
|
||||
|
||||
Adafruit_HMC5883_Unified mag = Adafruit_HMC5883_Unified(12345);
|
||||
|
||||
bool initialized = false;
|
||||
};
|
||||
|
||||
#endif // ifdef USES_P121
|
||||
#endif // ifndef PLUGINSTRUCTS_P121_DATA_STRUCT_H
|
||||
Reference in New Issue
Block a user