Skip to content

Commit

Permalink
MCP23017 changes
Browse files Browse the repository at this point in the history
Changed MCP23017 plugin to scan inputs realtime instead of using the
system timer mechanism
Changed MCP23017 default pullup resistors on input pins
Moved MCP23017 output and generic GPIO and PWM to device plugins
Added simple pulse option for GPIO outputs (only for small pulses less
than a few seconds).
  • Loading branch information
martinusGH committed Oct 2, 2015
1 parent 317b8a2 commit 11b306f
Show file tree
Hide file tree
Showing 19 changed files with 402 additions and 312 deletions.
41 changes: 14 additions & 27 deletions Controller.ino
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,9 @@ boolean sendData(struct EventStruct *event)
PiDome_sendDataMQTT(event);
break;
}

PluginCall(PLUGIN_EVENT_OUT, event, dummyString);

if (Settings.MessageDelay != 0)
{
char log[30];
Expand All @@ -85,6 +88,7 @@ boolean sendData(struct EventStruct *event)
while (millis() < timer)
backgroundtasks();
}

}
//#endif

Expand Down Expand Up @@ -423,43 +427,26 @@ void MQTTMessage(String *topic, String *message)
{
String cmd = topicSplit[1];
int pin = topicSplit[2].toInt();
if (cmd == "gpio")
{
int value = message->toFloat();
char cmd[80];
sprintf_P(cmd, PSTR("gpio,%u,%u"), pin, value);
Serial.println(cmd);
#ifdef ESP_CONNEXIO
ExecuteLine(cmd, VALUE_SOURCE_SERIAL);
#endif
#ifdef ESP_EASY
ExecuteCommand(cmd);
#endif
}
int value = message->toFloat();
struct EventStruct TempEvent;
TempEvent.Par1 = pin;
TempEvent.Par2 = value;
PluginCall(PLUGIN_WRITE, &TempEvent, cmd);
break;
}


case PROTOCOL_PIDOME_MQTT:
{
String name = topicSplit[4];
String cmd = topicSplit[5];
int pin = topicSplit[6].toInt();
int value = (*message == "true");
struct EventStruct TempEvent;
TempEvent.Par1 = pin;
TempEvent.Par2 = value;
if (name == Settings.Name)
{
if (cmd == "gpio")
{
int value = (*message == "true");
char cmd[80];
sprintf_P(cmd, PSTR("gpio,%u,%u"), pin, value);
Serial.println(cmd);
#ifdef ESP_CONNEXIO
ExecuteLine(cmd, VALUE_SOURCE_SERIAL);
#endif
#ifdef ESP_EASY
ExecuteCommand(cmd);
#endif
}
PluginCall(PLUGIN_WRITE, &TempEvent, cmd);
}
break;
}
Expand Down
87 changes: 1 addition & 86 deletions Devices.ino
Original file line number Diff line number Diff line change
@@ -1,86 +1 @@
// These are output devices that do not need a device plugin (yet).
// May change in the future if we decide that all IO should be done through plugins.

/*********************************************************************************************\
* Extender (Arduino Mini Pro connected through I2C)
* The Mini Pro needs a small sketch to support this
\*********************************************************************************************/
int extender(byte Cmd, byte Port, int Value)
{
uint8_t address = 0x7f;
Wire.beginTransmission(address);
Wire.write(Cmd);
Wire.write(Port);
Wire.write(Value & 0xff);
Wire.write((Value >> 8));
Wire.endTransmission();
if (Cmd == 2 || Cmd == 4)
{
delay(1); // remote unit needs some time to do the adc stuff
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
int portvalue = Wire.read();
//portvalue += Wire.read()*256;
return portvalue;
}
}
return -1;
}

/*********************************************************************************************\
* MCP23017 OUTPUT
\*********************************************************************************************/
boolean mcp23017(byte Par1, byte Par2)
{
boolean success = false;
byte portvalue = 0;
byte unit = (Par1 - 1) / 16;
byte port = Par1 - (unit * 16);
uint8_t address = 0x20 + unit;
byte IOBankConfigReg = 0;
byte IOBankValueReg = 0x12;
if (port > 8)
{
port = port - 8;
IOBankConfigReg++;
IOBankValueReg++;
}
// turn this port into output, first read current config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
portvalue = Wire.read();
portvalue &= ~(1 << (port - 1)); // change pin from (default) input to output

// write new IO config
Wire.beginTransmission(address);
Wire.write(IOBankConfigReg); // IO config register
Wire.write(portvalue);
Wire.endTransmission();
}
// get the current pin status
Wire.beginTransmission(address);
Wire.write(IOBankValueReg); // IO data register
Wire.endTransmission();
Wire.requestFrom(address, (uint8_t)0x1);
if (Wire.available())
{
portvalue = Wire.read();
if (Par2 == 1)
portvalue |= (1 << (port - 1));
else
portvalue &= ~(1 << (port - 1));

// write back new data
Wire.beginTransmission(address);
Wire.write(IOBankValueReg);
Wire.write(portvalue);
Wire.endTransmission();
success = true;
}
}

// obsolete, all code moved to plugins
12 changes: 9 additions & 3 deletions ESPEasy.ino
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
#define ESP_PROJECT_PID 2015050101L
#define ESP_EASY
#define VERSION 9
#define BUILD 26
#define BUILD 27
#define REBOOT_ON_MAX_CONNECTION_FAILURES 30
#define FEATURE_SPIFFS false

Expand Down Expand Up @@ -118,7 +118,7 @@

#define PLUGIN_INIT_ALL 1
#define PLUGIN_INIT 2
#define PLUGIN_COMMAND 3
#define PLUGIN_READ 3
#define PLUGIN_ONCE_A_SECOND 4
#define PLUGIN_TEN_PER_SECOND 5
#define PLUGIN_DEVICE_ADD 6
Expand All @@ -128,6 +128,8 @@
#define PLUGIN_WEBFORM_VALUES 10
#define PLUGIN_GET_DEVICENAME 11
#define PLUGIN_GET_DEVICEVALUENAMES 12
#define PLUGIN_WRITE 13
#define PLUGIN_EVENT_OUT 14

#include <ESP8266WiFi.h>
#include <WiFiUdp.h>
Expand Down Expand Up @@ -216,6 +218,10 @@ struct EventStruct
byte BaseVarIndex;
byte idx;
byte sensorType;
int Par1;
int Par2;
int Par3;
byte OriginTaskIndex;
};

struct LogStruct
Expand Down Expand Up @@ -461,7 +467,7 @@ void SensorSend()
for (byte varNr = 0; varNr < VARS_PER_TASK; varNr++)
preValue[varNr] = UserVar[varIndex + varNr];

success = PluginCall(PLUGIN_COMMAND, &TempEvent, dummyString);
success = PluginCall(PLUGIN_READ, &TempEvent, dummyString);

if (success)
{
Expand Down
80 changes: 3 additions & 77 deletions Serial.ino
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ void ExecuteCommand(char *Line)
// commands for debugging
// ****************************************

if (strcasecmp_P(Command, PSTR("resetpid")) == 0)
if (strcasecmp_P(Command, PSTR("Pullup")) == 0)
{
Settings.PID = 123456789;
SaveSettings();
Plugin_009_Config(18, 1);
}

#if FEATURE_SPIFFS
Expand All @@ -41,82 +40,9 @@ void ExecuteCommand(char *Line)
}

// ****************************************
// commands to execute io tasks
// special commands for old nodo plugin
// ****************************************

if (strcasecmp_P(Command, PSTR("GPIO")) == 0)
{
if (Par1 >= 0 && Par1 <= 16)
{
pinMode(Par1, OUTPUT);
digitalWrite(Par1, Par2);
if (printToWeb)
{
printWebString += F("GPIO ");
printWebString += Par1;
printWebString += F(" Set to ");
printWebString += Par2;
printWebString += F("<BR>");
}
}
}

if (strcasecmp_P(Command, PSTR("PWM")) == 0)
{
if (Par1 >= 0 && Par1 <= 1023)
{
pinMode(Par1, OUTPUT);
analogWrite(Par1, Par2);
if (printToWeb)
{
printWebString += F("GPIO ");
printWebString += Par1;
printWebString += F(" Set PWM to ");
printWebString += Par2;
printWebString += F("<BR>");
}
}
}

if (strcasecmp_P(Command, PSTR("MCPGPIO")) == 0)
{
mcp23017(Par1, Par2);
if (printToWeb)
{
printWebString += F("MCPGPIO ");
printWebString += Par1;
printWebString += F(" Set to ");
printWebString += Par2;
printWebString += F("<BR>");
}
}

if (strcasecmp_P(Command, PSTR("ExtRead")) == 0)
{
uint8_t address = 0x7f;
Wire.requestFrom(address, (uint8_t)Par1);
if (Wire.available())
{
for (byte x = 0; x < Par1; x++)
Serial.println(Wire.read());
}
}

if (strcasecmp_P(Command, PSTR("ExtGPIO")) == 0)
extender(1, Par1, Par2);
if (strcasecmp_P(Command, PSTR("ExtPWM")) == 0)
extender(3, Par1, Par2);
if (strcasecmp_P(Command, PSTR("ExtGPIORead")) == 0)
{
byte value = extender(2, Par1, 0);
Serial.println(value);
}
if (strcasecmp_P(Command, PSTR("ExtADCRead")) == 0)
{
int value = extender(4, Par1, 0);
Serial.println(value);
}

if (strcasecmp_P(Command, PSTR("DomoticzSend")) == 0)
{
if (GetArgv(Line, TmpStr1, 4))
Expand Down
27 changes: 12 additions & 15 deletions WebServer.ino
Original file line number Diff line number Diff line change
Expand Up @@ -1204,28 +1204,25 @@ void handle_control() {
urlDecode(command);
boolean validCmd = false;

struct EventStruct TempEvent;
char TmpStr1[80];
TmpStr1[0] = 0;
TempEvent.Par1 = 0;
TempEvent.Par2 = 0;
TempEvent.Par3 = 0;

char Cmd[40];
Cmd[0] = 0;
GetArgv(command, Cmd, 1);

if ((strcasecmp_P(Cmd, PSTR("gpio")) == 0) || (strcasecmp_P(Cmd, PSTR("pwm")) == 0) || (strcasecmp_P(Cmd, PSTR("mcpgpio")) == 0))
validCmd = true;

String reply = "";
if (GetArgv(command, TmpStr1, 2)) TempEvent.Par1 = str2int(TmpStr1);
if (GetArgv(command, TmpStr1, 3)) TempEvent.Par2 = str2int(TmpStr1);
if (GetArgv(command, TmpStr1, 4)) TempEvent.Par3 = str2int(TmpStr1);

printToWeb = true;
printWebString = "";
String reply = "";

if (validCmd)
{
#ifdef ESP_CONNEXIO
ExecuteLine(command, VALUE_SOURCE_SERIAL);
#endif
#ifdef ESP_EASY
ExecuteCommand(command);
#endif
}
else
if (!PluginCall(PLUGIN_WRITE, &TempEvent, webrequest))
reply += F("Unknown or restricted command!");

reply += printWebString;
Expand Down
Loading

0 comments on commit 11b306f

Please sign in to comment.