*/
#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 "your_device_id"
#define clientId "your_client_id"
#define clientKey "your_client_key"
#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
//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.
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");
while(!Device.Subscribe(callback, mqttServer, 1883)) // make certain that we can receive message from the iot platform (activate mqtt)
Serial.println("retrying");
}
unsigned long time; //only send every x amount of time.
unsigned int prevVal =0;
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;
}
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) //check for which asset we received an actuator so that we can perform the correct actuation.
{
msgString.toLowerCase(); //to make certain that our comparison later on works ok (it could be that a 'True' or 'False' was sent)
if (msgString == "false") {
digitalWrite(ledPin, LOW); //change the led
idOut = &ledPin;
}
else if (msgString == "true") {
digitalWrite(ledPin, HIGH);
idOut = &ledPin;
#include <Ethernet2.h>
#include <EthernetClient.h>
#include <PubSubClient.h>
#include <ATT_IOT.h> //AllThingsTalk IoT library
#include <SPI.h> //required to have support for signed/unsigned long type.
#include "keys.h" // Keep all your personal account information in a seperate file
#include <SoftwareSerial.h>
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
// Define PIN numbers for assets
// For digital and analog sensors, we recommend to use the physical pin id as the asset id
// For other sensors (I2C and UART), you can select any unique number as the asset id
#define readerId 0 // Analog sensor is connected to pin A0 on grove shield
SoftwareSerial SoftSerial(2, 3);
char buffer[64]; // Buffer array for data receive over serial port of the RFID device
int count=0; // Counter for buffer array
//required for the device
void callback(char* topic, byte* payload, unsigned int length);
EthernetClient ethClient;
PubSubClient pubSub(mqttServer, 1883, callback, ethClient);
void setup()
{
Serial.begin(57600); // Init serial link for debugging
while(!Serial && millis() < 1000); // Make sure you see all output on the monitor. After 1 sec, it will skip this step, so that the board can also work without being connected to a pc Serial.println("Starting sketch");
SoftSerial.begin(9600);
byte mac[] = {0x90, 0xA2, 0xDA, 0x0D, 0xE1, 0x3E}; // Adapt to your Arduino MAC Address
if (Ethernet.begin(mac) == 0) // Initialize the Ethernet connection:
{
Serial.println(F("DHCP failed,end"));
while(true); //we failed to connect, halt execution here.
}
while(!Device.Connect(ðClient, httpServer)) // connect the device with the IOT platform.
Serial.println("retrying");
Device.AddAsset(readerId, "RFID reader", "reader", false, "string"); // Create the sensor asset for your device
while(!Device.Subscribe(pubSub)) // make certain that we can receive message from the iot platform (activate mqtt)
Serial.println("retrying");
Serial.println("RFID reader is ready for use!");
}
void clearBufferArray() // Function to clear buffer array
{
for (int i=0; i<sizeof(buffer);i++)
buffer[i]=NULL; // Clear all indices of array with command NULL
count = 0; // Set counter of while loop to zero
}
void loop()
{
if (SoftSerial.available()) // Data is coming from software serial port comes from SoftSerial shield
{
bool foundStart = false;
while(SoftSerial.available()) // reading RFID data into char array
{
char val = SoftSerial.read();
if(val == 2) // 2 indicates start of text
foundStart = true;
else if(foundStart == true){
if(val == 3 || count == 64) break; //3 indicates end of text.
buffer[count++]= val; // writing RFID data into array
}
delay(10); // Delay because the Genuino 101 seems to be to fast for the serial port routine
}
buffer[count++] = '\0'; // Needs a terminating String char.
Serial.print("RFID tag: ");Serial.println(buffer);
Device.Send(buffer, readerId);
clearBufferArray(); // Call clearBufferArray function to clear the stored data from the array
}
Device.Process();
}
// Callback function: handles messages that were sent from the iot platform to this device.
void callback(char* topic, byte* payload, unsigned int length)
{
char message_buff[length + 1]; //need to copy over the payload so that we can add a /0 terminator, this can then be wrapped inside a string for easy manipulation
strncpy(message_buff, (char*)payload, length); //copy over the data
message_buff[length] = '\0'; //make certain that it ends with a null
Serial.print("Payload: "); //show some debugging
Serial.println(message_buff);
Serial.print("topic: ");
Serial.println(topic);
}
}
}
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);
int switchstate = 0;
void setup() {
// declare the LED pins as outputs
pinMode(5,OUTPUT);
// declare the switch pin as an input
pinMode(2,INPUT);
}
void loop() {
// read the value of the switch
// digitalRead() checks to see if there is voltage
// on the pin or not
switchstate = digitalRead(2);
// if the button is not pressed
// blink the red LEDs
if (switchstate == LOW) {
digitalWrite(5, LOW); // turn the red LED on pin 5 off
}
// this else is part of the above if() statement.
// if the switch is not LOW (the button is pressed)
// the code below will run
else {
digitalWrite(5, HIGH); // turn the red LED on pin 5 on
// wait for a quarter second before changing the light
delay(250);
digitalWrite(5, LOW); // turn the red LED on pin 5 off
// wait for a quarter second before changing the light
delay(250);
}
}
Comments