Skip to content

Commit

Permalink
R104
Browse files Browse the repository at this point in the history
  • Loading branch information
martinusGH committed Apr 25, 2016
1 parent e2af12a commit ded0de2
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 12 deletions.
49 changes: 49 additions & 0 deletions Command.ino
Expand Up @@ -143,6 +143,52 @@ void ExecuteCommand(byte source, const char *Line)
}
}
}

if (strcasecmp_P(Command, PSTR("SendToUDP")) == 0)
{
success = true;
String strLine = Line;
String ip = parseString(strLine,2);
String port = parseString(strLine,3);
int msgpos = getParamStartPos(strLine,4);
String message = strLine.substring(msgpos);
byte ipaddress[4];
str2ip((char*)ip.c_str(), ipaddress);
IPAddress UDP_IP(ipaddress[0], ipaddress[1], ipaddress[2], ipaddress[3]);
portUDP.beginPacket(UDP_IP, port.toInt());
portUDP.write(message.c_str(), message.length());
portUDP.endPacket();
}

if (strcasecmp_P(Command, PSTR("SendToHTTP")) == 0)
{
success = true;
String strLine = Line;
String host = parseString(strLine,2);
String port = parseString(strLine,3);
int pathpos = getParamStartPos(strLine,4);
String path = strLine.substring(pathpos);
WiFiClient client;
if (client.connect(host.c_str(), port.toInt()))
{
client.print(String("GET ") + path + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");

unsigned long timer = millis() + 200;
while (!client.available() && millis() < timer)
delay(1);

while (client.available()) {
String line = client.readStringUntil('\n');
if (line.substring(0, 15) == "HTTP/1.1 200 OK")
addLog(LOG_LEVEL_DEBUG, line);
delay(1);
}
client.flush();
client.stop();
}
}

// ****************************************
// special commands for old nodo plugin
Expand Down Expand Up @@ -311,10 +357,13 @@ void ExecuteCommand(byte source, const char *Line)
Serial.print(F(" Free mem : ")); Serial.println(FreeMem());
}

yield();

if (success)
status += F("\nOk");
else
status += F("\nUnknown command!");
SendStatus(source,status);
yield();
}

4 changes: 3 additions & 1 deletion ESPEasy.ino
Expand Up @@ -111,7 +111,7 @@
#define ESP_PROJECT_PID 2015050101L
#define ESP_EASY
#define VERSION 9
#define BUILD 103
#define BUILD 104
#define REBOOT_ON_MAX_CONNECTION_FAILURES 30
#define FEATURE_SPIFFS false

Expand Down Expand Up @@ -143,6 +143,8 @@
#define SYSTEM_TIMER_MAX 8
#define SYSTEM_CMD_TIMER_MAX 2
#define PINSTATE_TABLE_MAX 32
#define RULES_MAX_SIZE 2048
#define RULES_MAX_NESTING_LEVEL 3

#define PIN_MODE_UNDEFINED 0
#define PIN_MODE_INPUT 1
Expand Down
64 changes: 53 additions & 11 deletions Misc.ino
Expand Up @@ -35,6 +35,27 @@ String parseString(String& string, byte indexFind)
}


/*********************************************************************************************\
Parse a string and get the xth command or parameter
\*********************************************************************************************/
int getParamStartPos(String& string, byte indexFind)
{
String tmpString = string;
byte count = 0;
tmpString.replace(" ", ",");
for (int x=0; x < tmpString.length(); x++)
{
if(tmpString.charAt(x) == ',')
{
count++;
if (count == (indexFind -1))
return x+1;
}
}
return -1;
}


/*********************************************************************************************\
set pin mode & state (info table)
\*********************************************************************************************/
Expand Down Expand Up @@ -1707,20 +1728,35 @@ unsigned long getNtpTime()
\*********************************************************************************************/
void rulesProcessing(String& event)
{
unsigned long timer = micros();
static uint8_t* data;
static byte nestingLevel;

String log = "";

nestingLevel++;
if (nestingLevel > RULES_MAX_NESTING_LEVEL)
{
log = F("EVENT: Error: Nesting level exceeded!");
addLog(LOG_LEVEL_ERROR, log);
nestingLevel--;
return;
}

log = F("EVENT: ");
log += event;
addLog(LOG_LEVEL_INFO, log);

// load rules from flash memory, stored in offset block 10
uint8_t* data = new uint8_t[FLASH_EEPROM_SIZE];
uint32_t _sector = ((uint32_t)&_SPIFFS_start - 0x40200000) / SPI_FLASH_SEC_SIZE;
_sector += 10;
noInterrupts();
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(data), FLASH_EEPROM_SIZE);
interrupts();
if (data == NULL)
{
data = new uint8_t[RULES_MAX_SIZE];
uint32_t _sector = ((uint32_t)&_SPIFFS_start - 0x40200000) / SPI_FLASH_SEC_SIZE;
_sector += 10;
noInterrupts();
spi_flash_read(_sector * SPI_FLASH_SEC_SIZE, reinterpret_cast<uint32_t*>(data), RULES_MAX_SIZE);
interrupts();
data[RULES_MAX_SIZE-1]=0; // make sure it's terminated!
}

int pos = 0;
String line = "";
Expand All @@ -1739,7 +1775,6 @@ void rulesProcessing(String& event)
if (data[pos] == 10) // if line complete, parse this rule
{
line.replace("\r", "");
line.trim();
if (line.substring(0, 2) != "//" && line.length() > 0)
{
isCommand = true;
Expand All @@ -1749,6 +1784,7 @@ void rulesProcessing(String& event)
line = line.substring(0, comment);

line = parseTemplate(line, line.length());
line.trim();

String lineOrg = line; // store original line for future use
line.toLowerCase(); // convert all to lower case to make checks easier
Expand Down Expand Up @@ -1827,8 +1863,10 @@ void rulesProcessing(String& event)

struct EventStruct TempEvent;
parseCommandString(&TempEvent, action);
yield();
if (!PluginCall(PLUGIN_WRITE, &TempEvent, action))
ExecuteCommand(VALUE_SOURCE_SYSTEM, action.c_str());
yield();
}
}
}
Expand All @@ -1837,9 +1875,13 @@ void rulesProcessing(String& event)
}
pos++;
}
delete [] data;
timer = micros() - timer;
//Serial.println(timer);

nestingLevel--;
if(nestingLevel == 0)
{
delete [] data;
data = NULL;
}
}


Expand Down
7 changes: 7 additions & 0 deletions __ReleaseNotes.ino
@@ -1,3 +1,10 @@
// R104 25-04-2016
// Added a few lowlevel networking commands, to be used in the rules section:
// SendToUDP <ip>,<port>,<message> sample: SendToUDP 192.168.0.123,65500,Hello
// SendToHTTP <ip>,<port>,<path> sample: SendToHTTP 192.168.0.8,8080,/json.htm
// Reduced flash load size to 2048 in rules engine and use static pointer to save memory on recursive calls
// Max nesting level on rules is 3 levels deep

// R103 19-04-2016
// Upon request, added option for factory default static IP settings.

Expand Down

0 comments on commit ded0de2

Please sign in to comment.