diff --git a/platformio.ini b/platformio.ini index d52f3ec..27c022f 100644 --- a/platformio.ini +++ b/platformio.ini @@ -14,14 +14,12 @@ board = esp-wrover-kit framework = arduino build_flags = - -DMQTT_CLIENT_ID="\"${sysenv.MQTT_CLIENT_ID}\"" - -DMQTT_HOST="\"${sysenv.MQTT_HOST}\"" - -DMQTT_PASS="\"${sysenv.MQTT_PASS}\"" - -DMQTT_USER="\"${sysenv.MQTT_USER}\"" + -DHOSTNAME="\"${sysenv.HOSTNAME}\"" + -DHTTP_POST_URL="\"${sysenv.HTTP_POST_URL}\"" -DWIFI_NAME="\"${sysenv.WIFI_NAME}\"" -DWIFI_PASS="\"${sysenv.WIFI_PASS}\"" lib_deps = ArduinoJson - PubSub=https://github.com/knolleary/pubsubclient.git + HTTPClient-SSL Timer=https://github.com/brunocalou/Timer.git diff --git a/src/http.cpp b/src/http.cpp new file mode 100644 index 0000000..173a7a1 --- /dev/null +++ b/src/http.cpp @@ -0,0 +1,32 @@ +#include +#include + +#include "http.hpp" + +HTTPClient http; + +bool sendMessage(String from, String fromName, String date, String message) { + Serial.println("DBG: Sending message to HTTP endpoint..."); + + StaticJsonDocument<400> doc; + doc["from"] = from; + doc["fromName"] = fromName; + doc["date"] = date; + doc["message"] = message; + + char buffer[400]; + serializeJson(doc, buffer); + + http.begin(HTTP_POST_URL); + http.addHeader("Content-Type", "application/json"); + + int respCode = http.POST(buffer); + + if (respCode > 299) { + Serial.println("ERR: HTTP Post failed: " + http.getString()); + } + + http.end(); + + return respCode >= 200 && respCode < 300; +} diff --git a/src/http.hpp b/src/http.hpp new file mode 100644 index 0000000..a56a509 --- /dev/null +++ b/src/http.hpp @@ -0,0 +1,6 @@ +#ifndef H_HTTP +#define H_HTTP + +bool sendMessage(String from, String fromName, String date, String message); + +#endif diff --git a/src/main.cpp b/src/main.cpp index 7778ffd..03c2cbe 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,8 +1,9 @@ #include +#include +#include "http.hpp" #include "main.hpp" #include "modem.hpp" -#include "mqtt.hpp" #include "timer.h" #include "timerManager.h" @@ -20,10 +21,6 @@ void checkNet() { String creg = sendCommand("AT+CREG?", true); regInfo = creg.substring(7); Serial.println("INF: Registration: " + regInfo); - - if (!sendTelemetry(quality, regInfo)) { - Serial.println("ERR: Unable to submit telemetry"); - } } bool expectString(String expect) { return readLine() == expect; } @@ -41,7 +38,7 @@ void initModem() { Serial.println("Initializing modem in 4s..."); SerialAT.begin(115200, SERIAL_8N1, MODEM_RX, MODEM_TX); - delay(4000); + readLine(4000); if (!sendBoolCommand("AT")) { Serial.println("Modem initializing not successful"); @@ -80,7 +77,7 @@ void initModem() { void initWiFi() { Serial.println("Connecting to WiFi..."); - WiFi.setHostname(MQTT_CLIENT_ID); + WiFi.setHostname(HOSTNAME); WiFi.begin(WIFI_NAME, WIFI_PASS); while (WiFi.status() != WL_CONNECTED) { @@ -91,12 +88,28 @@ void initWiFi() { Serial.println(); } +String iso88591ToUTF8(String in) { + String out = ""; + + for (int i = 0; i < in.length(); i++) { + char ch = in.charAt(i); + if (ch < 0x80) { + out += ch; + } else { + out += (char)(0xc0 | (ch & 0xc0) >> 6); + out += (char)(0x80 | (ch & 0x3f)); + } + } + + return out; +} + void loop() { while (commandRunning) { delay(10); } - if (WiFi.status() != WL_CONNECTED || mqtt.state() != 0) { + if (WiFi.status() != WL_CONNECTED) { digitalWrite(MODEM_POWER_ON, LOW); esp_restart(); return; @@ -125,13 +138,12 @@ void loop() { pos = npos + 1; String date = line.substring(pos); - if (!sendMessage(trimQuotes(senderNo), trimQuotes(senderName), trimQuotes(date), message)) { + if (!sendMessage(trimQuotes(senderNo), trimQuotes(senderName), trimQuotes(date), iso88591ToUTF8(message))) { Serial.println("ERR: Unable to submit message"); } } TimerManager::instance().update(); - mqtt.loop(); } String readLine() { @@ -169,7 +181,7 @@ String readLine(int timeout) { } #ifdef MODEM_DEBUG - Serial.println("DBG: Received " + response); + Serial.println("DBG: <= " + response); #endif return response; @@ -184,9 +196,12 @@ String sendCommand(String command, bool readOK) { delay(10); } + Serial.println("DBG: => " + command); + commandRunning = true; SerialAT.println(command); if (!expectString(command)) { + commandRunning = false; return "ERR Command does not match"; } String resp = readLine(); @@ -199,11 +214,8 @@ String sendCommand(String command, bool readOK) { void setup() { Serial.begin(9600); + delay(2000); // Give terminal chance to connect initWiFi(); - while(!initMQTT()) { - Serial.println("ERR: Unable to connect to MQTT broker"); - delay(500); - } initModem(); } diff --git a/src/main.hpp b/src/main.hpp index 1d35b68..9d54ae2 100644 --- a/src/main.hpp +++ b/src/main.hpp @@ -12,6 +12,7 @@ void checkNet(); bool expectString(String expect); void initModem(); void initWiFi(); +String iso88591ToUTF8(String in); String readLine(); String readLine(int timeout); bool sendBoolCommand(String command); diff --git a/src/mqtt.cpp b/src/mqtt.cpp deleted file mode 100644 index 9f5014e..0000000 --- a/src/mqtt.cpp +++ /dev/null @@ -1,55 +0,0 @@ -#include - -#include "mqtt.hpp" - -WiFiClient wifi; -PubSubClient mqtt; - -bool initMQTT() { - Serial.println("Connecting to MQTT..."); - - mqtt.setClient(wifi); - mqtt.setServer(MQTT_HOST, 1883); - mqtt.setBufferSize(1024); - - return mqtt.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASS); -} - -bool sendMessage(String from, String fromName, String date, String message) { - Serial.println("DBG: Sending message to MQTT..."); - - if (mqtt.state() != 0) { - Serial.println("ERR: Invalid MQTT state " + String(mqtt.state(), DEC)); - return false; - } - - StaticJsonDocument<400> doc; - doc["from"] = from; - doc["fromName"] = fromName; - doc["date"] = date; - doc["message"] = message; - - char buffer[400]; - serializeJson(doc, buffer); - - return mqtt.publish(MQTT_TOPIC_MESSAGE, buffer); -} - -bool sendTelemetry(int quality, String regInfo) { - Serial.println("DBG: Sending telemetry to MQTT..."); - - if (mqtt.state() != 0) { - Serial.println("ERR: Invalid MQTT state " + String(mqtt.state(), DEC)); - return false; - } - - StaticJsonDocument<100> doc; - doc["quality"] = quality; - doc["reg"] = regInfo; - - char buffer[100]; - serializeJson(doc, buffer); - - return mqtt.publish(MQTT_TOPIC_TELE, buffer); -} - diff --git a/src/mqtt.hpp b/src/mqtt.hpp deleted file mode 100644 index f93d7cd..0000000 --- a/src/mqtt.hpp +++ /dev/null @@ -1,17 +0,0 @@ -#ifndef H_MQTT -#define H_MQTT - -#define MQTT_TOPIC_TELE "espsms/tele" -#define MQTT_TOPIC_MESSAGE "espsms/message" - -#include -#include - -extern WiFiClient wifi; -extern PubSubClient mqtt; - -bool initMQTT(); -bool sendMessage(String from, String fromName, String date, String message); -bool sendTelemetry(int quality, String regInfo); - -#endif