Iot Cloud Messenger

Hey everybody,

I want to use the Iot Cloud messenger to change certain values in my code by writing a keyword like "interval". then i want to get a response message saying something like "Please enter new value" followed by another input by the user with the value.

My problem is, that the documentation for the messenger is close to not existant and im new to the whole arduino thing so most of the forum chats to this topic i dont understand fully or don't contribute to solving my problem.

If anyone has some insights on how to send messages back and forth while evaluating the user input I would greatly appreciate some help :slight_smile:

I'll add some of my test code (which is probably going to be wrong on many levels ^^).

#include "thingProperties.h"
#include "DHT.h"

#define DHTTYPE DHT22
int dhtpin = 2;
bool outgoingMessage = false;

unsigned long previousMillis = 0;
const long interval = 2000; // Interval for sensor reading

void setup() {
  // Initialize serial and wait for port to open:
  Serial.begin(9600);
 
  // This delay gives the chance to wait for a Serial Monitor without blocking if none is found
  delay(1500); 

  pinMode(2, INPUT);

  // Defined in thingProperties.h
  initProperties();

  // Connect to Arduino IoT Cloud
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);
  
  /*
     The following function allows you to obtain more information
     related to the state of network and IoT Cloud connection and errors
     the higher number the more granular information you’ll get.
     The default is 0 (only errors).
     Maximum is 4
 */
  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}

void loop() 
{
  ArduinoCloud.update();
  DHT dht(dhtpin, DHTTYPE);
  dht.begin();
  
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) 
  {
    previousMillis = currentMillis;    
    float newTemp = dht.readTemperature();
    float newHum = dht.readHumidity();
    temp = newTemp;
    hum = newHum;
    heatIndex = dht.computeHeatIndex(temp, hum);
  }
  if (message1 == "test")
  {
    printMessageToDashboard("Test successfull!");
  }
}

void printMessageToDashboard(String text)
{
  outgoingMessage = true;
  message = text;
}

void messageQueue(){
  message5 = message4;
  message4 = message3;
  message3 = message2;
  message2 = message1;
  message1 = message;
  Serial.println("M" + message);
  Serial.println("M1" + message1);
  Serial.println("M2" + message2);
  Serial.println("M3" + message3);
  Serial.println("M4" + message4);
  Serial.println("M5" + message5);
}

void onSwitchChange()  {
  // Add your code here to act upon Switch change
  if(_switch_){
    dhtpin = 2;
  }
  else{
    dhtpin = 3;
  }
  pinval = dhtpin;
}

void onMessageChange() 
{
  if (outgoingMessage)
  {
    outgoingMessage = false;
    return;
  }
  
  messageQueue();
}

Hi @paulwindt2601. I recommend you start with a very simple Arduino Cloud IoT Thing that has a single Variable, and a sketch that only contains a small amount of code to communicate with a Messenger dashboard Widget via that Variable. Don't add anything to do with the DHT22 sensor to the sketch.

Upload the Thing sketch to your board and then verify that the behavior is as expected.

This simple Thing project will allow you to understand how the Messenger Widget functions and ensure that you can write 100% working code for interacting with it. You can then apply that understanding to your more complex real project.

I'll provide a simple Thing sketch, which uses a Variable named message to communicate with the Messenger Widget on a dashboard:

/* 
  Sketch generated by the Arduino IoT Cloud Thing "Untitled 7"
  https://create.arduino.cc/cloud/things/63c77aa1-720e-4e0f-bc32-6a68b60e6136 

  Arduino IoT Cloud Variables description

  The following variables are automatically generated and updated when changes are made to the Thing

  String message;

  Variables which are marked as READ/WRITE in the Cloud Thing will also have functions
  which are called when their values are changed from the Dashboard.
  These functions are generated with the Thing and added at the end of this sketch.
*/

#include "thingProperties.h"

void setup() {
  Serial.begin(9600);
  delay(1500);

  initProperties();
  ArduinoCloud.begin(ArduinoIoTPreferredConnection);

  setDebugMessageLevel(2);
  ArduinoCloud.printDebugInfo();
}


void loop() {
  ArduinoCloud.update();
}


/*
  Since message is READ_WRITE variable, onMessageChange() is
  executed every time a new value is received from IoT Cloud.
*/
void onMessageChange() {
  // The value of the variable changed because the user sent a message from the Messenger widget.
  Serial.print("Received message: ");
  Serial.println(message);

  // Parse the received message and set the Variable to the response that should be displayed in the Messenger.
  if (message == "hello") {
    message = "howdy";
  } else if (message == "goodbye") {
    message = "farewell";
  } else {
    message = "I don't understand";
  }
}