#include <SoftwareSerial.h>
#include "ATT_IOT_FONA.h"
#include "ATT_MQTT.h"
#include "ATT_IOT_GPRS.h"
#include <SPI.h> //required to have support for signed/unsigned long type.
#define deviceId ""
#define clientId ""
#define clientKey ""
#define FONA_APN "web.pro.be"
#define FONA_USERNAME ""
#define FONA_PASSWORD ""
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 5
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
ATTDevice Device(deviceId, clientId, clientKey); //create the object that provides the connection to the cloud to manager the device.
#define httpServer "api.AllThingsTalk.io" // HTTP API Server host
#define mqttServer httpServer // MQTT Server Address
//CAREFULL: don't use the same pins as used by the modem or things will start to crash
int knobPin = 1; // Analog 1 is the input pin + identifies the asset on the cloud platform
int ledPin = 4; // Pin 4 is the LED output pin + identifies the asset on the cloud platform
int ledPinR = 8;
int drukPin = 3;
//required for the device
void callback(const char* topic, const char* payload, unsigned int length);
// Setup the FONA MQTT class by passing in the FONA class and MQTT server and login details.
void setup()
{
pinMode(ledPin, OUTPUT); // initialize the digital pin as an output.
pinMode(ledPinR, OUTPUT);
pinMode(drukPin, INPUT);
SendValue(digitalRead(drukPin));
fonaSS.begin(19200); // if you're using software serial
Serial.begin(57600); // init serial link for debugging
while(!Serial && millis() < 2000); // Make sure you see all output on the monitor. After 2 sec, it will skip this step, so that the board can also work without being connected to a pc
while (! Device.InitGPRS(fonaSS, FONA_RST, F(FONA_APN), F(FONA_USERNAME), F(FONA_PASSWORD))) {
Serial.println("Retrying FONA");
}
Serial.println(F("Connected to Cellular!"));
delay(2000); // wait a few seconds to stabilize connection
while(!Device.Connect(httpServer)) // connect the device with the IOT platform.
Serial.println("retrying");
Device.AddAsset(knobPin, "knob", "rotary switch",false, "{\"type\": \"integer\", \"minimum\": 0, \"maximum\": 1023}");
Device.AddAsset(ledPin, "led", "light emitting diode", true, "boolean");
Device.AddAsset(ledPinR, "ledR", "light emitting diode", true, "boolean");
Device.AddAsset(drukPin, "Switch", "switch", false, "boolean");
while(!Device.Subscribe(callback, mqttServer, 1883)) // make certain that we can receive message from the iot platform (activate mqtt)
Serial.println("retrying");
}
void SendValue(bool value)
{
Serial.print("Button changed to: ");
Serial.println(value);
if(value)
Device.Send("true", drukPin);
else
Device.Send("false", drukPin);
}
unsigned long time; //only send every x amount of time.
unsigned int prevVal =0;
bool sensorVal;
void loop()
{
unsigned long curTime = millis();
if (curTime > (time + 3000)) // publish reading every 3 seconds to sensor knobPin
{
unsigned int knobRead = analogRead(knobPin); // read from sensor (knob)
if(prevVal != knobRead){
Device.Send(String(knobRead), knobPin);
prevVal = knobRead;
}
time = curTime;
}
bool sensorRead = digitalRead(drukPin); // Read status Digital Sensor
if (sensorVal != sensorRead) // Verify if value has changed
{
sensorVal = sensorRead;
SendValue(sensorVal);
}
Device.Process();
}
// Callback function: handles messages that were sent from the iot platform to this device.
void callback(const char* topic, const char* payload, unsigned int length)
{
String msgString(payload); //convert to string object, so we can easily compare and modify the string.
int* idOut = NULL;
idOut = &ledPin;
int pinNr = Device.GetPinNr(topic, strlen(topic));
Serial.print("Payload: "); //show some debugging.
Serial.println(msgString);
Serial.print("topic: ");
Serial.println(topic);
Serial.print("pin: ");
Serial.println(pinNr);
if (pinNr == ledPin)
{
msgString.toLowerCase();
if (msgString == "false")
{
digitalWrite(ledPin, LOW);
idOut = &ledPin;
}
else if (msgString == "true")
{
digitalWrite(ledPin, HIGH);
idOut = &ledPin;
}
}
if (topic[pinNr - 9] == (ledPinR + 48))
{
msgString.toLowerCase();
if (msgString == "false")
{
digitalWrite(ledPinR, LOW);
idOut = &ledPinR;
}
else if (topic[pinNr - 9] == (ledPinR + 48))
{
analogWrite(ledPinR, msgString.toInt());
idOut = &ledPinR;
}
}
if(idOut != NULL) //also let the iot platform know that the operation was succesful: give it some feedback. This also allows the iot to update the GUI's correctly & run scenarios.
Device.Send(msgString, *idOut);
}
Comments
Please log in or sign up to comment.