Jump to content









Photo

Arduino MQTT with GSM Shield

Arduino MQTT GSM

  • Please log in to reply
1 reply to this topic

#1 gava88

gava88

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 09 October 2015 - 03:08 PM

Hello,

I'm trying to send data to Carriots using MQTT with the Arduino GSM Shield (https://www.arduino....rduinoGSMShieldbut I have some problems.

 

This is my sketch:

// libraries
#include <stdlib.h>
#include <GSM.h>
#include <SPI.h>
#include <String.h>
#include <PubSubClient.h>
///////////////////////GSM///////////////////////
// PIN Number
#define PINNUMBER ""
// APN data
#define GPRS_APN       "internet.wind" // replace your GPRS APN
#define GPRS_LOGIN     ""    // replace with your GPRS login
#define GPRS_PASSWORD  "" // replace with your GPRS password
// initialize the library instance

const String APIKEY = "XXX";
const String DEVICE = "YYY";
//SendCarriots sender;

GSMClient client;
GPRS gprs;
GSM gsmAccess(true);
//GSM gsmAccess;
// modem verification object

///////////////////////MQTT///////////////////////
PubSubClient mqttClient(client);

///////////////////////SERIAL///////////////////////
#define arduinoLED 13   // Arduino LED on board

//TIME
unsigned long StartTime;
unsigned long CurrentTime;
unsigned long ElapsedTime = -1;

void flash(int ledPin)
{
  /* Flash LED three times. */
  for (int i = 0; i < 3; i++) {
    digitalWrite(ledPin, HIGH);
    delay(100);
    digitalWrite(ledPin, LOW);
    delay(100);
  }
}
void startGsm() {
  // connection state
  boolean notConnected = true;
  // attach the shield to the GPRS network with the APN, login and password
  while (notConnected)
  {
    if ((gsmAccess.begin() == GSM_READY) &
        (gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY))
      notConnected = false;
    else
    {
      Serial.println("Not connected");
      delay(1000);
    }
  }
  flash(arduinoLED);
  Serial.println("GPRS OK");
}
void startMqtt()
{
  //client.setServer(server, 1883);
  mqttClient.setServer("mqtt.carriots.com", 1883);
 mqttClient.connect("","XXX", "");
}

void setup()
{ pinMode(arduinoLED, OUTPUT);     // Configure the onboard LED for output
  digitalWrite(arduinoLED, LOW);   // default to LED off
  Serial.begin(115200);
  flash(arduinoLED);
   Serial.println("START MQTT CLIENT CARRIOTS");
  startGsm();
  startMqtt();
  Serial.println("MQTT OK");
  flash(arduinoLED);
}

void loop()
{
  sendMqtt();
  delay(5000);
}

void sendMqtt(){
   // Serial.println("PUB MSG");
   // Serial.println("PUB MSG");
  String payload = "{\"protocol\":\"v2\",\"checksum\":\"\",\"device\":\"";
  payload += DEVICE;
  payload += "\",\"at\":\"now\",\"data\":{\"mqtt\":\"1";
  // String payload = "{\"d\":{{\"name\":\"";

  payload += "\"}}";
  
Serial.println(payload);


if (isConnected())
    mqttClient.connect( "","XXX", "");
 mqttClient.publish("XXX/streams", payload.c_str());

}

boolean isConnected() {
  if (!mqttClient.connected()) {
    Serial.println("Connection failed");
    Serial.print("Reconnecting client to ");
    Serial.println("mqtt.carriots.com");
    while (!!! mqttClient.connect( "","XXX", "")) {
      Serial.print(".");
      delay(500);
    }
    Serial.println();
  }
  return true;
}

And this is the problem with the GSM modem:

 

 
AT+QIOPEN="TCP","mqtt.carriots.com",1883%13%
101 107>%13%%10%OK%13%%10%
 
101 118>%13%%10%OK%13%%10%%13%%10%CONNECT%13%%10%
%16%P%0%%4%MQTT%4%%194%%0%<%0%%0%%0%@XXX%0%%0%+++AT+QICLOSE%13%
13 13>
.AT+QIDNSIP=1%13%

 

I have successfully connected with the python client example but with Arduino I can't. I also tested the PubSub lib on different sites with the GSM shield and works without problems.

Help me please :) 

 

Thanks Fabio



#2 fortinlo

fortinlo

    Newbie

  • Members
  • Pip
  • 1 posts

Posted 04 July 2016 - 01:47 PM

Hey Fabio, did you manage to get over your issue ? I'm living the same situation. 

 

Greeting. 

Loïc 






1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users