I have attempted to code a custom device but I'm receiving large numbers.

Code: Select all
//#######################################################################################################
//#################################### Plugin 129: TFmini LIDAR #########################################
//#######################################################################################################
#define PLUGIN_129
#define PLUGIN_ID_129 129
#define PLUGIN_NAME_129 "TFmini"
#define PLUGIN_VALUENAME1_129 "Distance"
#define PLUGIN_VALUENAME2_129 "Strength"
boolean Plugin_129(byte function, struct EventStruct *event, String& string)
{
boolean success = false;
switch (function)
{
case PLUGIN_DEVICE_ADD:
{
Device[++deviceCount].Number = PLUGIN_ID_129;
Device[deviceCount].Type = DEVICE_TYPE_TFMINI;
Device[deviceCount].VType = SENSOR_TYPE_SINGLE;
Device[deviceCount].Ports = 0;
Device[deviceCount].PullUpOption = false;
Device[deviceCount].InverseLogicOption = false;
Device[deviceCount].FormulaOption = false;
Device[deviceCount].DecimalsOnly = true;
Device[deviceCount].ValueCount = 2;
Device[deviceCount].SendDataOption = true;
Device[deviceCount].TimerOption = true;
Device[deviceCount].GlobalSyncOption = true;
break;
}
case PLUGIN_GET_DEVICENAME:
{
string = F(PLUGIN_NAME_129);
break;
}
case PLUGIN_GET_DEVICEVALUENAMES:
{
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[0], PSTR(PLUGIN_VALUENAME1_129));
strcpy_P(ExtraTaskSettings.TaskDeviceValueNames[1], PSTR(PLUGIN_VALUENAME2_129));
break;
}
case PLUGIN_WEBFORM_LOAD:
{
}
case PLUGIN_WEBFORM_SAVE:
{
String plugin1 = WebServer.arg("plugin_129_sensortype");
Settings.TaskDevicePluginConfig[event->TaskIndex][0] = plugin1.toInt();
success = true;
break;
}
case PLUGIN_INIT:
{
Serial.begin(115200); // HW Serial for TFmini
delay (100); // Give a little time for things to start
// Set to Standard Output mode
Serial.write(0x42);
Serial.write(0x57);
Serial.write(0x02);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x00);
Serial.write(0x01);
Serial.write(0x06);
break;
}
case PLUGIN_TEN_PER_SECOND:
{
while(Serial.available()>=9) // When at least 9 bytes of data available (expected number of bytes for 1 signal), then read
{
if((0x59 == Serial.read()) && (0x59 == Serial.read())) // byte 1 and byte 2
{
unsigned int t1 = Serial.read(); // byte 3 = Dist_L
unsigned int t2 = Serial.read(); // byte 4 = Dist_H
t2 <<= 8;
t2 += t1;
UserVar[event->BaseVarIndex] = t2;
String log = F("Dist: ");
log += t1;
addLog(LOG_LEVEL_INFO,log);
t1 = Serial.read(); // byte 5 = Strength_L
t2 = Serial.read(); // byte 6 = Strength_H
t2 <<= 8;
t2 += t1;
UserVar[event->BaseVarIndex + 1] = t2;
for(int i=0; i<3; i++)Serial.read(); // byte 7, 8, 9 are ignored
}
}
}
return success;
}