JuraTerm

The JuraTerm module is similar to the Jura one, but it accepts remote commands, encodes them in the same way as the Jura module, and returns the decoded response. In the project it used to be the Jura class until it got superseded by the new Jura class and this one was delegated to just a terminal class. In a future revision this module's functionality will therefore be merged into the main Jura class.

#include "base_module.h" 
 
class JuraTermModule {
static String mqttTxBuffer;

static bool toCoffeemaker(String cmd);
static void onSerialReceived(Stream &stream, char arrivedChar, unsigned short availableCharsCount);

public:
static bool initialize();
static bool start();
static bool shutdown();
static void commandCallback(String message);
};#include "juraterm_module.h"

String JuraTermModule::mqttTxBuffer;

bool JuraTermModule::initialize() {
BaseModule::registerModule(MOD_IDX_JURATERM, JuraTermModule::start, JuraTermModule::shutdown);
}


bool JuraTermModule::start() {
if (!OtaCore::claimPin(ESP8266_gpio03)) { return false; } // RX 0
if (!OtaCore::claimPin(ESP8266_gpio01)) { return false; } // TX 0

OtaCore::registerTopic("coffee/command/" + OtaCore::getLocation(),
JuraTermModule::commandCallback);
Serial.end();
delay(10);
Serial.begin(9600);
Serial.setCallback(&JuraTermModule::onSerialReceived);

return true;
}


bool JuraTermModule::shutdown() {
if (!OtaCore::releasePin(ESP8266_gpio03)) { return false; } // RX 0
if (!OtaCore::releasePin(ESP8266_gpio01)) { return false; } // TX 0

Serial.end();
OtaCore::deregisterTopic("coffee/command/" + OtaCore::getLocation());
return true;
}


void JuraTermModule::commandCallback(String message) {
if (message == "AN:0A") { return; }

JuraTermModule::toCoffeemaker(message);
}

When we start this module, we register an MQTT topic to receive commands. This allows us to receive the coffee machine commands. We basically act as a straight pass-through for these commands, except for this one particular command. This command that we filter out would erase the machine's EEPROM, which is something which we are unlikely to want.

Again, we use the same method to encode the command:

 bool JuraTermModule::toCoffeemaker(String cmd) {
OtaCore::log(LOG_DEBUG, "Sending command: " + cmd);

cmd += " ";

for (int i = 0; i < cmd.length(); ++i) {
uint8_t ch = static_cast<uint8_t>(cmd[i]);
uint8_t d0 = 0xFF;
uint8_t d1 = 0xFF;
uint8_t d2 = 0xFF;
uint8_t d3 = 0xFF;

bitWrite(d0, 2, bitRead(ch, 0));
bitWrite(d0, 5, bitRead(ch, 1));
bitWrite(d1, 2, bitRead(ch, 2));
bitWrite(d1, 5, bitRead(ch, 3));
bitWrite(d2, 2, bitRead(ch, 4));
bitWrite(d2, 5, bitRead(ch, 5));
bitWrite(d3, 2, bitRead(ch, 6));
bitWrite(d3, 5, bitRead(ch, 7));

delay(1);
Serial.write(d0);
delay(1);
Serial.write(d1);
delay(1);
Serial.write(d2);
delay(1);
Serial.write(d3);
delay(7);
}

return true;
}


void JuraTermModule::onSerialReceived(Stream &stream, char arrivedChar, unsigned short availableCharsCount) {
OtaCore::log(LOG_DEBUG, "Receiving UART 0.");

while(stream.available()){
delay(1);
uint8_t d0 = stream.read();
delay(1);
uint8_t d1 = stream.read();
delay(1);
uint8_t d2 = stream.read();
delay(1);
uint8_t d3 = stream.read();
delay(7);

uint8_t d4;
bitWrite(d4, 0, bitRead(d0, 2));
bitWrite(d4, 1, bitRead(d0, 5));
bitWrite(d4, 2, bitRead(d1, 2));
bitWrite(d4, 3, bitRead(d1, 5));
bitWrite(d4, 4, bitRead(d2, 2));
bitWrite(d4, 5, bitRead(d2, 5));
bitWrite(d4, 6, bitRead(d3, 2));
bitWrite(d4, 7, bitRead(d3, 5));

OtaCore::log(LOG_TRACE, String(d4));

mqttTxBuffer += (char) d4;
if (' ' == (char) d4) {
OtaCore::publish("coffee/response", OtaCore::getLocation() + ";" + mqttTxBuffer);
mqttTxBuffer = "";
}
}
}

Instead of interpreting the data in any way, we merely return the response on its respective MQTT topic.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.199.243