mirror of
https://github.com/Luzifer/espsms.git
synced 2024-12-20 18:01:22 +00:00
Transmit through HTTP, convert iso8859-1 to utf-8
This commit is contained in:
parent
aad6b7fdd1
commit
85deb7e285
7 changed files with 69 additions and 92 deletions
|
@ -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
|
||||
|
|
32
src/http.cpp
Normal file
32
src/http.cpp
Normal file
|
@ -0,0 +1,32 @@
|
|||
#include <ArduinoJson.h>
|
||||
#include <HTTPClient.h>
|
||||
|
||||
#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;
|
||||
}
|
6
src/http.hpp
Normal file
6
src/http.hpp
Normal file
|
@ -0,0 +1,6 @@
|
|||
#ifndef H_HTTP
|
||||
#define H_HTTP
|
||||
|
||||
bool sendMessage(String from, String fromName, String date, String message);
|
||||
|
||||
#endif
|
42
src/main.cpp
42
src/main.cpp
|
@ -1,8 +1,9 @@
|
|||
#include <Arduino.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
#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();
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
55
src/mqtt.cpp
55
src/mqtt.cpp
|
@ -1,55 +0,0 @@
|
|||
#include <ArduinoJson.h>
|
||||
|
||||
#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);
|
||||
}
|
||||
|
17
src/mqtt.hpp
17
src/mqtt.hpp
|
@ -1,17 +0,0 @@
|
|||
#ifndef H_MQTT
|
||||
#define H_MQTT
|
||||
|
||||
#define MQTT_TOPIC_TELE "espsms/tele"
|
||||
#define MQTT_TOPIC_MESSAGE "espsms/message"
|
||||
|
||||
#include <PubSubClient.h>
|
||||
#include <WiFi.h>
|
||||
|
||||
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
|
Loading…
Reference in a new issue