Created
August 27, 2016 16:20
-
-
Save Bigomby/a22edb84f3eb500050486d7f12dbf17b to your computer and use it in GitHub Desktop.
This file contains 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
// 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; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment