Closed gopi1230 closed 4 years ago
Did you solved this?
Thanks for making library available for us !
I started with the library examples. I am able to connect to Adaruits.io but not able to publish data continuously... In fact i don't know where to and how add publish routine.. if could any sample simple code which sending data to broker then it would be very helpful for me.
For all those getting stuck at MQTT.MessageFlag = true
when using software serial, ESP8266 or other platforms, change GSM_MQTT::processing
in GSM_MQTT.cpp
to
void GSM_MQTT::processing(void)
{
if (TCP_Flag == false)
{
MQTT_Flag = false;
_tcpInit();
}
_ping();
//**************new code*****************
unsigned long t = millis();
//add long waitPeriod = 0; somewhere at the beginning of the file
if (t - waitPeriod > 1000L)
{
waitPeriod = t;
serialEvent(); //call serialEvent every second
}
//*****************************************
}
I am using Sim800 with atmega328 and using software serial for communication. I tried subscribe example , first i struck at +++AT output . After i read issues , i change Serial and mySerial vice versa. i got below response
`AT
+CGATT: 1
OK
OK AT ATE1
OK AT+CREG?
OK
+CREG: 0,1
OK AT+CIPMUX=0 AT+CIPMODE=1
OK AT+CGATT?
OK AT+CGATT=1
+CGATT: 1
OK AT+CIPSTATUS
OK
OK
STATE: IP INITIAL AT+CSTT="AIRTELGPRS.COM"
OK AT+CIPSTATUS
OK
STATE: IP START AT+CIICR
OK AT+CIPSTATUS
OK
STATE: IP GPRSACT AT+CIFSR
100.64.189.225 AT+CIPSTATUS
OK
STATE: IP STATUS AT+CIPSTART="TCP","52.205.105.7","5555"
OK
CONNECT MQTT.TCP_Flag = True`
Then after i changed serialEvent to serialEvent1 and serialEvent2 also , but same response coming now . Here , I am sending my code and circuit diagram.
`#include "GSM_MQTT.h"
include
String MQTT_HOST = "52.205.105.7"; / MQTT host address / String MQTT_PORT = "5555"; / MQTT port / SoftwareSerial mySerial(3, 2); // RX, TX / Software Serial through which mqtt events log is printed at 9600 baud rate / void GSM_MQTT::AutoConnect(void) {
connect("qwertyuiop", 0, 0, "", "", 1, 0, 0, 0, "", "");
} void GSM_MQTT::OnConnect(void) { / This function is called when mqqt connection is established. put your subscription publish codes here. / subscribe(0, _generateMessageID(), "/DEVICE_STATUS_TO_DEVICE", 1);
} void GSM_MQTT::OnMessage(char Topic, int TopicLength, char Message, int MessageLength) {
//mySerial.println(TopicLength); // mySerial.println(Topic); // mySerial.println(MessageLength); Serial.println(Message);
} GSM_MQTT MQTT(20); / 20 is the keepalive duration in seconds /
void setup() { // initialize mqtt: // GSM modem should be connected to Harware Serial // index =0; MQTT.begin();
/ You can write your code here /
} void loop() {
/ You can write your code here / if (MQTT.available()) { / if you want to do something when mqtt connection is live. You can write your code here / }
MQTT.processing(); }
**Here GSM_MQTT.cpp**
/ MQTT.h - Library for GSM MQTT Client. Created by Nithin K. Kurian, Dhanish Vijayan, Elementz Engineers Guild Pvt. Ltd, July 2, 2016. Released into the public domain. /include "GSM_MQTT.h"
include "Arduino.h"
include
include <avr/pgmspace.h>
extern uint8_t GSM_Response;
extern SoftwareSerial mySerial; extern String MQTT_HOST; extern String MQTT_PORT;
extern GSM_MQTT MQTT; uint8_t GSM_Response = 0; unsigned long previousMillis = 0; //char inputString[UART_BUFFER_LENGTH]; // a string to hold incoming data boolean stringComplete = false; // whether the string is complete void serialEvent2(); GSM_MQTT::GSM_MQTT(unsigned long KeepAlive) { _KeepAliveTimeOut = KeepAlive; }
void GSM_MQTT::begin(void) { Serial.begin(9600); mySerial.begin(9600); mySerial.write("AT\r\n"); delay(1000); _tcpInit(); } char GSM_MQTT::_sendAT(char *command, unsigned long waitms) {
unsigned long PrevMillis = millis(); strcpy(reply, "none"); GSM_Response = 0; mySerial.write(command); unsigned long currentMillis = millis(); // Serial.println(PrevMillis); // Serial.println(currentMillis); while ( (GSM_Response == 0) && ((currentMillis - PrevMillis) < waitms) ) { // delay(1); serialEvent2(); currentMillis = millis(); } return GSM_Response; } char GSM_MQTT::sendATreply(char command, char replystr, unsigned long waitms) { strcpy(reply, replystr); unsigned long PrevMillis = millis(); GSM_ReplyFlag = 0; mySerial.write(command); unsigned long currentMillis = millis();
// Serial.println(PrevMillis); // Serial.println(currentMillis); while ( (GSM_ReplyFlag == 0) && ((currentMillis - PrevMillis) < waitms) ) { // delay(1); serialEvent2(); currentMillis = millis(); } return GSM_ReplyFlag; } void GSM_MQTT::_tcpInit(void) { switch (modemStatus) { case 0: { delay(1000); // mySerial.print("+++"); delay(500); if (_sendAT("AT\r\n", 5000) == 1) { modemStatus = 1; } else { modemStatus = 0; break; } } case 1: { if (_sendAT("ATE1\r\n", 2000) == 1) { modemStatus = 2; } else { modemStatus = 1; break; } } case 2: { if (sendATreply("AT+CREG?\r\n", "0,1", 5000) == 1) { _sendAT("AT+CIPMUX=0\r\n", 2000); _sendAT("AT+CIPMODE=1\r\n", 2000); if (sendATreply("AT+CGATT?\r\n", ": 1", 4000) != 1) { _sendAT("AT+CGATT=1\r\n", 2000); } modemStatus = 3; _tcpStatus = 2; } else { modemStatus = 2; break; } } case 3: { if (GSM_ReplyFlag != 7) { _tcpStatus = sendATreply("AT+CIPSTATUS\r\n", "STATE", 4000); if (_tcpStatusPrev == _tcpStatus) { tcpATerrorcount++; if (tcpATerrorcount >= 10) { tcpATerrorcount = 0; _tcpStatus = 7; }
}
}
void GSM_MQTT::_ping(void) {
if (pingFlag == true) { unsigned long currentMillis = millis(); if ((currentMillis - _PingPrevMillis ) >= _KeepAliveTimeOut 1000) { // save the last time you blinked the LED _PingPrevMillis = currentMillis; mySerial.print(char(PINGREQ 16)); _sendLength(0); } } } void GSM_MQTT::_sendUTFString(char string) { int localLength = strlen(string); mySerial.print(char(localLength / 256)); mySerial.print(char(localLength % 256)); mySerial.print(string); } void GSM_MQTT::_sendLength(int len) { bool length_flag = false; while (length_flag == false) { if ((len / 128) > 0) { mySerial.print(char(len % 128 + 128)); len /= 128; } else { length_flag = true; mySerial.print(char(len)); } } } void GSM_MQTT::connect(char ClientIdentifier, char UserNameFlag, char PasswordFlag, char UserName, char Password, char CleanSession, char WillFlag, char WillQoS, char WillRetain, char WillTopic, char WillMessage) { ConnectionAcknowledgement = NO_ACKNOWLEDGEMENT ; mySerial.print(char(CONNECT * 16 )); char ProtocolName[7] = "MQIsdp"; int localLength = (2 + strlen(ProtocolName)) + 1 + 3 + (2 + strlen(ClientIdentifier)); if (WillFlag != 0) { localLength = localLength + 2 + strlen(WillTopic) + 2 + strlen(WillMessage); } if (UserNameFlag != 0) { localLength = localLength + 2 + strlen(UserName);
} _sendLength(localLength); _sendUTFString(ProtocolName); mySerial.print(char(_ProtocolVersion)); mySerial.print(char(UserNameFlag User_Name_Flag_Mask + PasswordFlag Password_Flag_Mask + WillRetain Will_Retain_Mask + WillQoS Will_QoS_Scale + WillFlag Will_Flag_Mask + CleanSession Clean_Session_Mask)); mySerial.print(char(_KeepAliveTimeOut / 256)); mySerial.print(char(_KeepAliveTimeOut % 256)); _sendUTFString(ClientIdentifier); if (WillFlag != 0) { _sendUTFString(WillTopic); _sendUTFString(WillMessage); } if (UserNameFlag != 0) { _sendUTFString(UserName); if (PasswordFlag != 0) { _sendUTFString(Password); } } } void GSM_MQTT::publish(char DUP, char Qos, char RETAIN, unsigned int MessageID, char Topic, char Message) { mySerial.print(char(PUBLISH 16 + DUP DUP_Mask + Qos QoS_Scale + RETAIN)); int localLength = (2 + strlen(Topic)); if (Qos > 0) { localLength += 2; } localLength += strlen(Message); _sendLength(localLength); _sendUTFString(Topic); if (Qos > 0) { mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256)); } mySerial.print(Message); } void GSM_MQTT::publishACK(unsigned int MessageID) { mySerial.print(char(PUBACK 16)); _sendLength(2); mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256)); } void GSM_MQTT::publishREC(unsigned int MessageID) { mySerial.print(char(PUBREC 16)); _sendLength(2); mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256)); } void GSM_MQTT::publishREL(char DUP, unsigned int MessageID) { mySerial.print(char(PUBREL 16 + DUP DUP_Mask + 1 QoS_Scale)); _sendLength(2); mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256)); }
void GSM_MQTT::publishCOMP(unsigned int MessageID) { mySerial.print(char(PUBCOMP 16)); _sendLength(2); mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256)); } void GSM_MQTT::subscribe(char DUP, unsigned int MessageID, char SubTopic, char SubQoS) { mySerial.print(char(SUBSCRIBE 16 + DUP DUP_Mask + 1 * QoS_Scale)); int localLength = 2 + (2 + strlen(SubTopic)) + 1; _sendLength(localLength); mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256)); _sendUTFString(SubTopic); mySerial.print(SubQoS);
} void GSM_MQTT::unsubscribe(char DUP, unsigned int MessageID, char SubTopic) { mySerial.print(char(UNSUBSCRIBE 16 + DUP DUP_Mask + 1 QoS_Scale)); int localLength = (2 + strlen(SubTopic)) + 2; _sendLength(localLength);
mySerial.print(char(MessageID / 256)); mySerial.print(char(MessageID % 256));
_sendUTFString(SubTopic); } void GSM_MQTT::disconnect(void) { mySerial.print(char(DISCONNECT * 16)); _sendLength(0); pingFlag = false; } //Messages const char CONNECTMessage[] PROGMEM = {"Client request to connect to Server\r\n"}; const char CONNACKMessage[] PROGMEM = {"Connect Acknowledgment\r\n"}; const char PUBLISHMessage[] PROGMEM = {"Publish message\r\n"}; const char PUBACKMessage[] PROGMEM = {"Publish Acknowledgment\r\n"}; const char PUBRECMessage[] PROGMEM = {"Publish Received (assured delivery part 1)\r\n"}; const char PUBRELMessage[] PROGMEM = {"Publish Release (assured delivery part 2)\r\n"}; const char PUBCOMPMessage[] PROGMEM = {"Publish Complete (assured delivery part 3)\r\n"}; const char SUBSCRIBEMessage[] PROGMEM = {"Client Subscribe request\r\n"}; const char SUBACKMessage[] PROGMEM = {"Subscribe Acknowledgment\r\n"}; const char UNSUBSCRIBEMessage[] PROGMEM = {"Client Unsubscribe request\r\n"}; const char UNSUBACKMessage[] PROGMEM = {"Unsubscribe Acknowledgment\r\n"}; const char PINGREQMessage[] PROGMEM = {"PING Request\r\n"}; const char PINGRESPMessage[] PROGMEM = {"PING Response\r\n"}; const char DISCONNECTMessage[] PROGMEM = {"Client is Disconnecting\r\n"};
void GSM_MQTT::printMessageType(uint8_t Message) { switch (Message) { case CONNECT: { int k, len = strlen_P(CONNECTMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(CONNECTMessage + k); Serial.print(myChar); } break; } case CONNACK: { int k, len = strlen_P(CONNACKMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(CONNACKMessage + k); Serial.print(myChar); } break; } case PUBLISH: { int k, len = strlen_P(PUBLISHMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PUBLISHMessage + k); Serial.print(myChar); } break; } case PUBACK: { int k, len = strlen_P(PUBACKMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PUBACKMessage + k); Serial.print(myChar); } break; } case PUBREC: { int k, len = strlen_P(PUBRECMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PUBRECMessage + k); Serial.print(myChar); } break; } case PUBREL: { int k, len = strlen_P(PUBRELMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PUBRELMessage + k); Serial.print(myChar); } break; } case PUBCOMP: { int k, len = strlen_P(PUBCOMPMessage ); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PUBCOMPMessage + k); Serial.print(myChar); } break; } case SUBSCRIBE: { int k, len = strlen_P(SUBSCRIBEMessage ); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(SUBSCRIBEMessage + k); Serial.print(myChar); } break; } case SUBACK: { int k, len = strlen_P(SUBACKMessage ); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(SUBACKMessage + k); Serial.print(myChar); } break; } case UNSUBSCRIBE: { int k, len = strlen_P(UNSUBSCRIBEMessage ); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(UNSUBSCRIBEMessage + k); Serial.print(myChar); } break; } case UNSUBACK: { int k, len = strlen_P(UNSUBACKMessage ); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(UNSUBACKMessage + k); Serial.print(myChar); } break; } case PINGREQ: { int k, len = strlen_P(PINGREQMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PINGREQMessage + k); Serial.print(myChar); } break; } case PINGRESP: { int k, len = strlen_P(PINGRESPMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(PINGRESPMessage + k); Serial.print(myChar); } break; } case DISCONNECT: { int k, len = strlen_P(DISCONNECTMessage); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(DISCONNECTMessage + k); Serial.print(myChar); } break; } } }
//Connect Ack const char ConnectAck0[] PROGMEM = {"Connection Accepted\r\n"}; const char ConnectAck1[] PROGMEM = {"Connection Refused: unacceptable protocol version\r\n"}; const char ConnectAck2[] PROGMEM = {"Connection Refused: identifier rejected\r\n"}; const char ConnectAck3[] PROGMEM = {"Connection Refused: server unavailable\r\n"}; const char ConnectAck4[] PROGMEM = {"Connection Refused: bad user name or password\r\n"}; const char ConnectAck5[] PROGMEM = {"Connection Refused: not authorized\r\n"}; void GSM_MQTT::printConnectAck(uint8_t Ack) { switch (Ack) { case 0: { int k, len = strlen_P(ConnectAck0); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(ConnectAck0 + k); Serial.print(myChar); } break; } case 1: { int k, len = strlen_P(ConnectAck1); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(ConnectAck1 + k); Serial.print(myChar); } break; } case 2: { int k, len = strlen_P(ConnectAck2); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(ConnectAck2 + k); Serial.print(myChar); } break; } case 3: { int k, len = strlen_P(ConnectAck3); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(ConnectAck3 + k); Serial.print(myChar); } break; } case 4: { int k, len = strlen_P(ConnectAck4); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(ConnectAck4 + k); Serial.print(myChar); } break; } case 5: { int k, len = strlen_P(ConnectAck5); char myChar; for (k = 0; k < len; k++) { myChar = pgm_read_byte_near(ConnectAck5 + k); Serial.print(myChar); } break; } } } unsigned int GSM_MQTT::_generateMessageID(void) { if (_LastMessaseID < 65535) { return ++_LastMessaseID; } else { _LastMessaseID = 0; return _LastMessaseID; } } void GSM_MQTT::processing(void) { if (TCP_Flag == false) { MQTT_Flag = false; _tcpInit(); } _ping(); } bool GSM_MQTT::available(void) { return MQTT_Flag; } void serialEvent2() {
while (mySerial.available()) { char inChar = (char)mySerial.read(); if (MQTT.TCP_Flag == false) { if (MQTT.index < 200) { MQTT.inputString[MQTT.index++] = inChar; } if (inChar == '\n') { MQTT.inputString[MQTT.index] = 0; stringComplete = true; Serial.print(MQTT.inputString); if (strstr(MQTT.inputString, MQTT.reply) != NULL) { MQTT.GSM_ReplyFlag = 1; if (strstr(MQTT.inputString, " INITIAL") != 0) { MQTT.GSM_ReplyFlag = 2; // } else if (strstr(MQTT.inputString, " START") != 0) { MQTT.GSM_ReplyFlag = 3; // } else if (strstr(MQTT.inputString, "IP CONFIG") != 0) { _delay_us(10); MQTT.GSM_ReplyFlag = 4; } else if (strstr(MQTT.inputString, " GPRSACT") != 0) { MQTT.GSM_ReplyFlag = 4; // } else if ((strstr(MQTT.inputString, " STATUS") != 0) || (strstr(MQTT.inputString, "TCP CLOSED") != 0)) { MQTT.GSM_ReplyFlag = 5; // } else if (strstr(MQTT.inputString, " TCP CONNECTING") != 0) { MQTT.GSM_ReplyFlag = 6; // } else if ((strstr(MQTT.inputString, " CONNECT OK") != 0) || (strstr(MQTT.inputString, "CONNECT FAIL") != NULL) || (strstr(MQTT.inputString, "PDP DEACT") != 0)) { MQTT.GSM_ReplyFlag = 7; } } else if (strstr(MQTT.inputString, "OK") != NULL) { GSM_Response = 1; } else if (strstr(MQTT.inputString, "ERROR") != NULL) { GSM_Response = 2; } else if (strstr(MQTT.inputString, ".") != NULL) { GSM_Response = 3; } else if (strstr(MQTT.inputString, "CONNECT FAIL") != NULL) { GSM_Response = 5; } else if (strstr(MQTT.inputString, "CONNECT") != NULL) { GSM_Response = 4; MQTT.TCP_Flag = true; Serial.println("MQTT.TCP_Flag = True"); MQTT.AutoConnect(); MQTT.pingFlag = true; MQTT.tcpATerrorcount = 0; } else if (strstr(MQTT.inputString, "CLOSED") != NULL) { GSM_Response = 4; MQTT.TCP_Flag = false; MQTT.MQTT_Flag = false; } MQTT.index = 0; MQTT.inputString[0] = 0; } } else { uint8_t ReceivedMessageType = (inChar / 16) & 0x0F; uint8_t DUP = (inChar & DUP_Mask) / DUP_Mask; uint8_t QoS = (inChar & QoS_Mask) / QoS_Scale; uint8_t RETAIN = (inChar & RETAIN_Mask); if ((ReceivedMessageType >= CONNECT) && (ReceivedMessageType <= DISCONNECT)) { bool NextLengthByte = true; MQTT.length = 0; MQTT.lengthLocal = 0; uint32_t multiplier=1; delay(2); char Cchar = inChar; while ( (NextLengthByte == true) && (MQTT.TCP_Flag == true)) { if (mySerial.available()) { inChar = (char)mySerial.read(); Serial.println(inChar, DEC); if ((((Cchar & 0xFF) == 'C') && ((inChar & 0xFF) == 'L') && (MQTT.length == 0)) || (((Cchar & 0xFF) == '+') && ((inChar & 0xFF) == 'P') && (MQTT.length == 0))) { MQTT.index = 0; MQTT.inputString[MQTT.index++] = Cchar; MQTT.inputString[MQTT.index++] = inChar; MQTT.TCP_Flag = false; MQTT.MQTT_Flag = false; MQTT.pingFlag = false; Serial.println("Disconnecting"); } else { if ((inChar & 128) == 128) { MQTT.length += (inChar & 127) multiplier; multiplier = 128; Serial.println("More"); } else { NextLengthByte = false; MQTT.length += (inChar & 127) multiplier; multiplier = 128; } } } } MQTT.lengthLocal = MQTT.length; Serial.println(MQTT.length); if (MQTT.TCP_Flag == true) { MQTT.printMessageType(ReceivedMessageType); MQTT.index = 0L; uint32_t a = 0; while ((MQTT.length-- > 0) && (mySerial.available())) { MQTT.inputString[uint32_t(MQTT.index++)] = (char)mySerial.read();
} }`
Could you please help me out , i am trying it from one week.