/* This code is free software; you can redistribute it and/or modify it under the terms of the GNU Lesser General Public License as published by the Free Software Foundation; either version 2.1 of the License, or (at your option) any later version. This code 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 Lesser General Public License for more details. You should have received a copy of the GNU Lesser General Public License along with this library; if not, write to the Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ #define VERSION "0.0.5" #include "GwHardware.h" #define LOG_SERIAL true #include #include // This will automatically choose right CAN library and create suitable NMEA2000 object #include #include #include #include #include #include #include "N2kDataToNMEA0183.h" #include "GwLog.h" #include "GWConfig.h" #include "GWWifi.h" #include "GwSocketServer.h" #include "GwBoatData.h" GwLog logger(LOG_SERIAL); GwConfigHandler config(&logger); GwWifi gwWifi(&config,&logger); GwSocketServer socketServer(&config,&logger); GwBoatData boatData(&logger); //counter int numCan=0; int NodeAddress; // To store last Node Address Preferences preferences; // Nonvolatile storage on ESP32 - To store LastDeviceAddress bool SendNMEA0183Conversion = true; // Do we send NMEA2000 -> NMEA0183 conversion bool SendSeaSmart = false; // Do we send NMEA2000 messages in SeaSmart format N2kDataToNMEA0183 nmea0183Converter(&logger, &boatData,&NMEA2000, 0); // Set the information for other bus devices, which messages we support const unsigned long TransmitMessages[] PROGMEM = {127489L, // Engine dynamic 0 }; const unsigned long ReceiveMessages[] PROGMEM = {/*126992L,*/ // System time 127250L, // Heading 127258L, // Magnetic variation 128259UL,// Boat speed 128267UL,// Depth 129025UL,// Position 129026L, // COG and SOG 129029L, // GNSS 130306L, // Wind 128275UL,// Log 127245UL,// Rudder 0 }; // Forward declarations void HandleNMEA2000Msg(const tN2kMsg &N2kMsg); void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg); WebServer webserver(80); // Serial port 2 config (GPIO 16) const int baudrate = 38400; const int rs_config = SERIAL_8N1; // Buffer config #define MAX_NMEA0183_MESSAGE_SIZE 150 // For AIS char buff[MAX_NMEA0183_MESSAGE_SIZE]; tNMEA0183 NMEA0183; #define JSON_OK "{\"status\":\"OK\"}" //embedded files extern const uint8_t indexFile[] asm("_binary_web_index_html_gz_start"); extern const uint8_t indexFileEnd[] asm("_binary_web_index_html_gz_end"); extern const uint8_t indexFileLen[] asm("_binary_web_index_html_gz_size"); void web_index() // Wenn "http:///" aufgerufen wurde { webserver.sendHeader(F("Content-Encoding"), F("gzip")); webserver.send_P(200, "text/html", (const char *)indexFile,(int)indexFileLen); //dann Index Webseite senden } void js_reset() // Wenn "http:///gauge.min.js" aufgerufen wurde { Serial.println("Reset Button"); ESP.restart(); } void js_status(){ StaticJsonDocument<256> status; status["numcan"]=numCan; status["version"]=VERSION; status["wifiConnected"]=gwWifi.clientConnected(); status["clientIP"]=WiFi.localIP().toString(); status["numClients"]=socketServer.numClients(); String buf; serializeJson(status,buf); webserver.send(200,F("application/json"),buf); } void js_config(){ webserver.send(200,F("application/json"),config.toJson()); } void js_boatData(){ webserver.send(200,F("application/json"),boatData.toJson()); } void web_setConfig(){ bool ok=true; String error; for (int i=0;i NMEA 0183 conversion NMEA2000.SetMsgHandler(HandleNMEA2000Msg); // Also send all NMEA2000 messages in SeaSmart format nmea0183Converter.SetSendNMEA0183MessageCallback(SendNMEA0183Message); NMEA2000.Open(); } //***************************************************************************** #define MAX_NMEA2000_MESSAGE_SEASMART_SIZE 500 //***************************************************************************** //NMEA 2000 message handler void HandleNMEA2000Msg(const tN2kMsg &N2kMsg) { numCan++; if ( !sendSeasmart->asBoolean() ) return; char buf[MAX_NMEA2000_MESSAGE_SEASMART_SIZE]; if ( N2kToSeasmart(N2kMsg, millis(), buf, MAX_NMEA2000_MESSAGE_SEASMART_SIZE) == 0 ) return; socketServer.sendToClients(buf); } //***************************************************************************** void SendNMEA0183Message(const tNMEA0183Msg &NMEA0183Msg) { if ( ! sendTCP->asBoolean() && ! sendUsb->asBoolean() ) return; char buf[MAX_NMEA0183_MESSAGE_SIZE]; if ( !NMEA0183Msg.GetMessage(buf, MAX_NMEA0183_MESSAGE_SIZE) ) return; if (sendTCP->asBoolean()){ socketServer.sendToClients(buf); } if (sendUsb->asBoolean()){ Serial.println(buf); } } void loop() { webserver.handleClient(); gwWifi.loop(); socketServer.loop(); NMEA2000.ParseMessages(); int SourceAddress = NMEA2000.GetN2kSource(); if (SourceAddress != NodeAddress) { // Save potentially changed Source Address to NVS memory NodeAddress = SourceAddress; // Set new Node Address (to save only once) preferences.begin("nvs", false); preferences.putInt("LastNodeAddress", SourceAddress); preferences.end(); Serial.printf("Address Change: New Address=%d\n", SourceAddress); } nmea0183Converter.loop(); // Dummy to empty input buffer to avoid board to stuck with e.g. NMEA Reader if ( Serial.available() ) { Serial.read(); } }