├── .gitignore ├── README.md ├── docker-compose.yml ├── LICENSE └── arduino └── esp8266-sms-gateway.ino /.gitignore: -------------------------------------------------------------------------------- 1 | .vscode/* 2 | !.vscode/settings.json 3 | !.vscode/tasks.json 4 | !.vscode/launch.json 5 | !.vscode/extensions.json 6 | *.code-workspace 7 | 8 | # Local History for Visual Studio Code 9 | .history/ 10 | .vscode -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ooceans 2 | Simple sms gateway using esp8266 + mqtt + sim800L 3 | 4 | This repo include arduino project file + docker-compose file. 5 | The REST API app can be founded here https://github.com/2pai/api-sms-gateway-mqtt 6 | 7 | Full video explanation in Bahasa 8 | https://www.youtube.com/watch?v=dRw7G7Dee8E 9 | -------------------------------------------------------------------------------- /docker-compose.yml: -------------------------------------------------------------------------------- 1 | version: '3.7' 2 | services: 3 | mosquitto: 4 | image: eclipse-mosquitto:1.6.7 5 | hostname: mqtt_2pai 6 | container_name: mqtt_2pai 7 | ports: 8 | - 1883:1883 9 | - 9001:9001 10 | networks: 11 | - kiana_net 12 | networks: 13 | kiana_net: 14 | driver: bridge 15 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2020 Iqbal syamil ayasy 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /arduino/esp8266-sms-gateway.ino: -------------------------------------------------------------------------------- 1 | #define SerialAT mySerial 2 | 3 | #define SerialMon Serial 4 | #define TINY_GSM_MODEM_SIM800 5 | 6 | #include 7 | #include // Include the Wi-Fi library 8 | #include 9 | #include 10 | #include 11 | 12 | //Create software serial object to communicate with SIM800L 13 | SoftwareSerial mySerial(D1, D2); //SIM800L Tx & Rx is connected to ESP8266 D1 & D2 14 | WiFiClient espClient; 15 | PubSubClient mqtt(espClient); 16 | TinyGsm modem(mySerial); 17 | const char* ssid = ""; // The SSID (name) of the Wi-Fi network you want to connect to 18 | const char* password = ""; // The password of the Wi-Fi network 19 | const char* broker = ""; // ip / domain your mqtt broker 20 | 21 | const char* topicSMS = ""; // name MQTTtopic for SMS 22 | const char* topicReport = ""; // name MQTTtopic for reporting 23 | const char* topicBalance = ""; // name MQTTtopic for check balance 24 | const char* deviceName = ""; // name of your device 25 | StaticJsonDocument<250> wrapper; 26 | 27 | boolean res; 28 | boolean mqttConnect() { 29 | char buffer[256]; 30 | SerialMon.print("Connecting to "); 31 | SerialMon.print(broker); 32 | wrapper["deviceName"] = deviceName; 33 | 34 | // Connect to MQTT Broker 35 | boolean status = mqtt.connect(deviceName); 36 | 37 | if (status == false) { 38 | SerialMon.println("fail"); 39 | return false; 40 | } 41 | SerialMon.println("success"); 42 | mqtt.subscribe(topicBalance); 43 | mqtt.subscribe(topicSMS); 44 | wrapper["kind"] = "connected"; 45 | wrapper["status"] = true; 46 | size_t n = serializeJson(wrapper,buffer); 47 | mqtt.publish(topicReport,buffer,n); 48 | return mqtt.connected(); 49 | } 50 | 51 | void callback(char* topic, byte* payload, unsigned int length) { 52 | StaticJsonDocument<200> doc; 53 | char buffer[256]; 54 | deserializeJson(doc,payload,length); 55 | wrapper["id"] = doc["id"]; 56 | wrapper["deviceName"] = deviceName; 57 | wrapper["kind"] = topic; 58 | 59 | if(strcmp(topic, topicBalance) == 0){ 60 | String ussd = modem.sendUSSD("*123#"); 61 | wrapper["payload"] = (char*) ussd.c_str(); 62 | size_t n = serializeJson(wrapper,buffer); 63 | mqtt.publish(topicReport,buffer,n); 64 | 65 | }else if(strcmp(topic, topicSMS) == 0){ 66 | const char* phoneNumber = doc["phoneNumber"]; 67 | const char* messagePayload = doc["message"]; 68 | res = modem.sendSMS(phoneNumber,messagePayload); 69 | wrapper["status"] = res; 70 | size_t n = serializeJson(wrapper,buffer); 71 | mqtt.publish(topicReport,buffer,n); 72 | } 73 | 74 | } 75 | 76 | 77 | void setup() 78 | { 79 | SerialMon.begin(9600); 80 | mySerial.begin(9600); 81 | WiFi.begin(ssid, password); 82 | Serial.print("Connecting to "); 83 | Serial.println(ssid); 84 | while (WiFi.status() != WL_CONNECTED) { // Wait for the Wi-Fi to connect 85 | delay(1000); 86 | Serial.print('*'); 87 | } 88 | 89 | modem.init(); 90 | 91 | mqtt.setServer(broker, 1883); // connect to mqtt broker with port (default : 1883) 92 | mqtt.setCallback(callback); 93 | 94 | } 95 | 96 | void loop() 97 | { 98 | if (!mqtt.connected()) { 99 | SerialMon.println("Trying Connecting to mqtt broker"); 100 | if(mqttConnect()){ 101 | SerialMon.println("MQTT Connected"); 102 | } 103 | } 104 | 105 | 106 | mqtt.loop(); 107 | 108 | } 109 | 110 | --------------------------------------------------------------------------------