Hi,
I created a project with an ArduinoYUN and Temp sensor. Every 5 seconds the sensor send info to AWS IOT platform.
Now I wold like to Publish a topic to Another YUN and this activate a relay. Does anybody how to do it?
This is the scketck for the sensor Arduino:
#include <aws_iot_mqtt.h>
#include <aws_iot_version.h>
#include "aws_iot_config.h"
aws_iot_mqtt_client myClient; // init iot_mqtt_client
char msg[32]; // read-write buffer
int cnt = 0; // loop counts
int rc = -100; // return value placeholder
bool success_connect = false; // whether it is connected
#include <ArduinoJson.h>
char data[80];
StaticJsonBuffer<200> jsonBuffer;
#include <Process.h>
Process date; // process used to get the date
#include "DHT.h" //cargamos la librería DHT
#define DHTPIN 2 //Seleccionamos el pin en el que se conectará el sensor
#define DHTTYPE DHT22 //Se selecciona el DHT22(hay otros DHT)
DHT dht(DHTPIN, DHTTYPE); //Se inicia una variable que será usada por Arduino para comunicarse con el sensor
const int moisturePin = A1; // Grove Moisture sensor connected to A1 on the Grove Shield
const int ledPin = 2;
int moisturValue = 0; // variable to store the Moisture Sensor
const int lightPin = A2; //variable to store the Light Sensor
// Basic callback function that prints out the message
void msg_callback(char* src, int len) {
Serial.println("CALLBACK:");
int i;
for(i = 0; i < len; i++) {
Serial.print(src[i]);
}
Serial.println("");
}
void setup()
{
// Start Serial for print-out and wait until it's ready
Serial.begin(115200);
while(!Serial);
dht.begin(); //Se inicia el sensor
Bridge.begin(); // initialize Bridge
//
char curr_version[80];
snprintf_P(curr_version, 80, PSTR("AWS IoT SDK Version(dev) %d.%d.%d-%s\n"), VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH, VERSION_TAG);
Serial.println(curr_version);
// Set up the client
if((rc = myClient.setup(AWS_IOT_CLIENT_ID)) == 0) {
// Load user configuration
if((rc = myClient.config(AWS_IOT_MQTT_HOST, AWS_IOT_MQTT_PORT, AWS_IOT_ROOT_CA_PATH, AWS_IOT_PRIVATE_KEY_PATH, AWS_IOT_CERTIFICATE_PATH)) == 0) {
// Use default connect: 60 sec for keepalive
if((rc = myClient.connect()) == 0) {
success_connect = true;
// Subscribe to "topic1"
if((rc = myClient.subscribe("topic1", 1, msg_callback)) != 0) {
Serial.println("Subscribe failed!");
Serial.println(rc);
}
}
else {
Serial.println(F("Connect failed!"));
Serial.println(rc);
}
}
else {
Serial.println(F("Config failed!"));
Serial.println(rc);
}
}
else {
Serial.println(F("Setup failed!"));
Serial.println(rc);
}
// Delay to make sure SUBACK is received, delay time could vary according to the server
delay(2000);
}
void loop() {
//Lectura Hora de Sistema
date.begin("/bin/date");
date.addParameter("+%d/%m/%Y %T");
date.run();
String timeString = date.readString();
//Lecturas de Sensores
float humity = dht.readHumidity(); //Se lee la humedad
int temperature = dht.readTemperature(); //Se lee la temperatura
moisturValue = analogRead(moisturePin);//Se lee la humedad del suelo
int lightValue = analogRead(lightPin); //Se lee la luz solar
//Se imprimen las variables
//sprintf(msg,"\"Temperature\": %d", temperature);
Serial.print("moisture = " ); Serial.println(moisturValue); //Check the Value for with Dry sand and then with Wet Sand
Serial.print("light value = "); Serial.println(lightValue);
Serial.print("Temperatura= ");Serial.println(temperature);
Serial.print("Humedad= ");Serial.println(humity);
//Creating the JSON payload
String temp = "\"Temperature\": " + String(temperature) ;
String humi = ", \"Humity\": " + String(humity) ;
String light = ", \"lightval\":" + String(lightValue) ;
String soilMoist = ", \"moistval\":" + String(moisturValue) ;
// Add both value together to send as one string.
String value = temp + humi + light +soilMoist;
Serial.println(value);
String payload = "{" + value + "}";
payload.toCharArray(data, (payload.length() + 1));
//Json File
//Serial.print("\{");Serial.print("\"Id\":\"001\"\,");Serial.print("\"Date\":\"");Serial.print(timeString);Serial.print("\"\,"); Serial.print("\"Temperature\":\"");Serial.print(t);Serial.print("\"");Serial.println("\}");
delay(10000); //Se espera 10 segundos para seguir leyendo //datos
//Publish data to AWS IoT using topic/plantdata,which in turn will fire the AWS IoT rule to write sensor data to Dynamo DB
if(success_connect) {
// Generate a new message in each loop and publish to "topic/plantdata"
if((rc = myClient.publish("topic1", data, strlen(data), 1, false)) != 0) {
Serial.println("Publish failed!");
Serial.println(rc);
}
// Get a chance to run a callback
if((rc = myClient.yield()) != 0) {
Serial.println("Yield failed!");
Serial.println(rc);
}
// Done with the current loop
sprintf(msg, "publish %d done", cnt++);
Serial.println(msg);
delay(10000);
}
// }//end if checking for time interval to post
delay(5000);
}
Thank you very much
Eduard