hey, Im currently programming a wemos d1 pro (esp8266), using the Arduino Iot cloud (message is a Iot var). As soon as I start the programm it starts to call the onMessageChange() function by its own.
Does anybody know a fix for this?
I know, my code is ugly af right now, but I am in a hardcore debug proccess xD
Thanks!
`
/*
Sketch generated by the Arduino IoT Cloud Thing "Untitled"
https://create.arduino.cc/cloud/things/6eb14f19-2a6f-4f0e-b2db-0fe1b122b32e
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"
int timer;
unsigned long currentMillis;
bool isNumber;
bool wateringSuccess;
char testForDigit;
String messageLow;
bool breakWhile;
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);
// 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();
pinMode(16, OUTPUT);
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH);
message = "started right now";
}
void loop() {
ArduinoCloud.update();
// Your code here
}
void onMessageChange() {
Serial.println("run");
// check if String is a number
isNumber = true;
breakWhile = false;
// convert string to char, so you can use .isDigit()
char charsOfMessage[message.length() - 1];
message.toCharArray(charsOfMessage, message.length());
for (int i = 0; i < sizeof(charsOfMessage); i++) {
if (!isDigit(charsOfMessage[i])) {
isNumber = false;
break;
}
}
// code if mesage is a number
if (isNumber) {
// convert message to number
timer = message.toInt() * 1000;
// start loop, activate led and pin
currentMillis = millis();
digitalWrite(LED_BUILTIN, LOW);
digitalWrite(16, HIGH);
wateringSuccess = true;
while (millis() < timer + currentMillis) {
// keep the wifi connection from crashing
yield();
ArduinoCloud.update();
// check if changed message is string
// convert string to char, so you can use .isDigit()
char charsOfMessage[message.length() - 1];
message.toCharArray(charsOfMessage, message.length());
for (int i = 0; i < sizeof(charsOfMessage); i++) {
if (isDigit(charsOfMessage[i]) == false) {
// if you send the stop message
messageLow = message;
Serial.println(messageLow);
messageLow.toLowerCase();
Serial.println(messageLow);
Serial.println(messageLow.compareTo("stop") == 0);
if (!messageLow.compareTo("stop") == 0) {
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(16, LOW);
wateringSuccess = false;
// break out of the while
//breakWhile = true;
Serial.println("break deine mudda 2");
// break out of for
break;
}
}
}
// check if break while true, so break out of while
if (breakWhile) {
break;
}
}
}
digitalWrite(LED_BUILTIN, HIGH);
digitalWrite(16, LOW);
Serial.println(wateringSuccess);
Serial.println(breakWhile);
//send back succeFs message
if (wateringSuccess && !breakWhile) {
message = "watered successfully";
}
else if (!wateringSuccess && breakWhile) {
message = "stopped successfully";
}
else {
message = "error occured";
}
/* if message is no number
if(! isNumber){
if(message.compareTo("HI") == 0){
message = "HI";
}
}*/
}
`