-
-
Save ChathuDharmasiri/03098161354e05dc75937677501b196f to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #include <Arduino.h> | |
| #include <Wire.h> | |
| #include <WiFi.h> // Include the WiFi library for MAC address | |
| #include <ArduinoJson.h> | |
| #include "Secret.h" // Include the file to get the username and pasasword of MQTT server | |
| #include"datacake.h" | |
| String gsm_send_serial(String command, int delay); | |
| #define SerialMon Serial | |
| #define SerialAT Serial1 | |
| #define GSM_PIN "" | |
| //#define D0 36 | |
| //#define D1 35 | |
| //#define D2 32 | |
| //#define D3 33 | |
| //#define R0 4 | |
| // Your GPRS credentials, if any | |
| const char apn[] = "dialogbb"; | |
| const char gprsUser[] = ""; | |
| const char gprsPass[] = ""; | |
| // MQTT details | |
| String broker = "mqtt.datacake.co"; | |
| String MQTTport = "8883"; | |
| #define uS_TO_S_FACTOR 1000000ULL // Conversion factor for micro seconds to seconds | |
| #define TIME_TO_SLEEP 60 // Time ESP32 will go to sleep (in seconds) | |
| #define UART_BAUD 115200 | |
| #define MODEM_TX 32 | |
| #define MODEM_RX 33 | |
| #define GSM_RESET 21 | |
| #define MAC_ADDRESS_SIZE 18 // Assuming MAC address is in format "XX:XX:XX:XX:XX:XX" | |
| byte mac[6]; | |
| String str_macAddress; | |
| unsigned long prevMillis = 0; | |
| const unsigned long interval = 60000; // Interval for sending messages | |
| // Device-specific details | |
| const char* deviceSerial = "34865D461830"; // Replace with your device serial | |
| void Init(void); // Function prototype for network and GPRS initialization | |
| void connectToGPRS(void); // Function prototype for GPRS connection | |
| void connectToMQTT(void); // Function prototype for MQTT connection | |
| bool isNetworkConnected(); // Function prototype to check if network is connected | |
| bool isGPRSConnected(); // Function prototype to check if GPRS is connected | |
| void mqttCallback(char* topic, byte* payload, unsigned int len) { | |
| SerialMon.print("Message arrived ["); | |
| SerialMon.print(topic); | |
| SerialMon.print("]: "); | |
| SerialMon.write(payload, len); | |
| SerialMon.println(); | |
| // Extract serial number from the topic | |
| String topicStr = String(topic); | |
| int firstSlash = topicStr.indexOf('/'); | |
| int lastSlash = topicStr.lastIndexOf('/'); | |
| String MAC_ID = topicStr.substring(firstSlash + 1, lastSlash); | |
| SerialMon.print("MAC ID: "); | |
| SerialMon.println(MAC_ID); | |
| if (MAC_ID == deviceSerial) { | |
| // Decode the received message | |
| StaticJsonDocument<200> doc; | |
| DeserializationError error = deserializeJson(doc, payload, len); | |
| if (error) { | |
| SerialMon.print("deserializeJson() failed: "); | |
| SerialMon.println(error.c_str()); | |
| return; | |
| } | |
| // Extract the payload | |
| bool state = doc["state"]; | |
| // if(state == 0){ | |
| // digitalWrite(R0,LOW); | |
| // }else if(state == 1){ | |
| // digitalWrite(R0,HIGH); | |
| // } | |
| } else { | |
| SerialMon.println("Received message for a different serial number"); | |
| } | |
| } | |
| void setup() { | |
| // Set console baud rate | |
| Serial.begin(115200); | |
| delay(10); | |
| SerialAT.begin(UART_BAUD, SERIAL_8N1, MODEM_RX, MODEM_TX); | |
| delay(2000); | |
| pinMode(GSM_RESET, OUTPUT); | |
| digitalWrite(GSM_RESET, HIGH); // RS-485 | |
| delay(2000); | |
| // pinMode(D0, INPUT); | |
| // pinMode(D1, INPUT); | |
| // pinMode(D2, INPUT); | |
| // pinMode(D3, INPUT); | |
| // pinMode(R0, OUTPUT); | |
| Init(); | |
| connectToGPRS(); | |
| connectToMQTT(); | |
| } | |
| void loop() { | |
| if (millis() - prevMillis >= interval) { | |
| prevMillis = millis(); | |
| // bool IN1 = digitalRead(D0); | |
| // bool IN2 = digitalRead(D1); | |
| // bool IN3 = digitalRead(D2); | |
| // bool IN4 = digitalRead(D3); | |
| // Create JSON object | |
| // StaticJsonDocument<200> doc; | |
| // doc["D0"] = IN1 ? 1 : 0; | |
| // doc["D1"] = IN2 ? 1 : 0; | |
| // doc["D2"] = IN3 ? 1 : 0; | |
| // doc["D3"] = IN4 ? 1 : 0; | |
| int DI0 = 1; | |
| // int DI1 = IN2 ? 1 : 0; | |
| // int DI2 = IN3 ? 1 : 0; | |
| // int DI3 = IN4 ? 1 : 0; | |
| // Construct the command string with the certificate length | |
| // String command5 = "AT+CCERTDOWN=\"server_cert.pem\"," + String(cert_length); | |
| // gsm_send_serial(command5, 2000); | |
| gsm_send_serial("AT+CCERTDOWN=\"server_cert.pem\",1806", 2000); | |
| delay(2000); | |
| // String command4 = mqtt_ca_cert + "\x1A"; | |
| gsm_send_serial(mqtt_ca_cert, 2000); | |
| delay(2000); | |
| gsm_send_serial("AT+CCERTLIST", 2000); | |
| delay(1000); | |
| gsm_send_serial("AT+CSSLCFG=\"sslversion\",0,4", 2000); | |
| gsm_send_serial(" AT+CSSLCFG=\"authmode\",0,1", 2000); | |
| gsm_send_serial("AT+CSSLCFG=\"ignorelocaltime\",0,1", 2000); | |
| gsm_send_serial("AT+CSSLCFG=\"cacert\",0,\"server_cert.pem\"", 2000); | |
| gsm_send_serial("AT+CSSLCFG=\"ciphersuites\",0,0xFFFF", 2000); | |
| gsm_send_serial("AT+CSSLCFG=\"enableSNI\",0,1", 2000); | |
| gsm_send_serial("AT+CMQTTREL=0", 2000); | |
| gsm_send_serial("AT+CSSLCFG?", 2000); | |
| gsm_send_serial("AT+CCHADDR", 2000); | |
| gsm_send_serial("AT+CMQTTSTOP", 2000); | |
| gsm_send_serial("AT+CMQTTSTART", 2000); | |
| delay(2000); | |
| //gsm_send_serial(" AT+CSSLCFG=\"cacert\",1,\"datacake_ca.pem \"", 1000); | |
| gsm_send_serial("AT+CMQTTACCQ=0,\"PE\",1", 2000); | |
| delay(2000); | |
| gsm_send_serial("AT+CMQTTSSLCFG=0,0", 2000); | |
| gsm_send_serial("AT+CMQTTCFG=\"checkUTF8\",0,0", 2000); | |
| gsm_send_serial("AT+CSSLCFG=0", 2000); | |
| gsm_send_serial("AT+CMQTTWILLTOPIC=0,1", 5000); | |
| gsm_send_serial("p\x1A", 5000); | |
| gsm_send_serial("AT+CMQTTWILLMSG=0,1,1", 5000); | |
| gsm_send_serial("q\x1A", 5000); | |
| delay(2000); | |
| String command = "AT+CMQTTCONNECT=0,\"tcp://mqtt.datacake.co:8883\",60,1,\"" + username + "\",\"" + password + "\""; | |
| gsm_send_serial(command,1500); | |
| delay(2000); | |
| // String downlinkTopic = "NORVI/+/OUTPUT"; | |
| // gsm_send_serial("AT+CMQTTSUB=0,14,1", 1000); | |
| // gsm_send_serial(downlinkTopic + "\x1A", 1000); | |
| // delay(2000); | |
| } | |
| bool isNetworkConnected() { | |
| String response = gsm_send_serial("AT+CREG?", 3000); | |
| return (response.indexOf("+CREG: 0,1") != -1 || response.indexOf("+CREG: 0,5") != -1); | |
| } | |
| bool isGPRSConnected() { | |
| String response = gsm_send_serial("AT+CGATT?", 3000); | |
| return (response.indexOf("+CGATT: 1") != -1); | |
| } | |
| String gsm_send_serial(String command, int delay) { | |
| String buff_resp = ""; | |
| Serial.println("Send ->: " + command); | |
| SerialAT.println(command); | |
| long wtimer = millis(); | |
| while (wtimer + delay > millis()) { | |
| while (SerialAT.available()) { | |
| buff_resp += SerialAT.readString(); | |
| } | |
| } | |
| Serial.println(buff_resp); | |
| return buff_resp; | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment