Help needed adjusting regular IDE code to Iot Cloud Code

Hi. I'm going on a trip soon and I was planning on using my laptop to virtually connect to my desktop to do work some work while I'm away since my desktop has much better specs. Just in case the desktop turns off, I made an actuator that would press the power button on my pc. I made this little actuator using a servo that moves a certain amount of degrees if a button is pressed using code generated by ChatGPT since I'm not that good at code yet. I tested the code out with a physical button and an ESP8266 V1.0 and it worked great. However, I planned on using this device through the cloud, and that's why I chose Arduino Iot Cloud to control this device. My only problem is that I thought that the Iot cloud simply took in regular IDE code, however this is not the case. Is there anyone out that can help me adjust the IDE code so that it works with IoT cloud??? Thank's in advance!!

This is my schematic that I was using (keep in mind this is with the physical button)

Screenshot 2023-05-28 173226(1)

This is my original, IDE Code made by ChatGPT.

#include <Servo.h>

// Pin assignments
const int servoPin = 13;   // Servo signal pin
const int buttonPin = 4;  // Button pin
// Servo parameters
const int initialPosition = 100;  // Initial position of the servo
const int targetPosition = 85;   // Target position (35 degrees from initial position)

// Time parameters
const unsigned long delayTime = 4000;  // Delay time in milliseconds

Servo myServo;
bool buttonState = false;
bool servoActivated = false;
unsigned long startTime = 0;

void setup() {
  // Initialize the servo and button
  myServo.attach(servoPin, 500, 2500);  // Servo control pin and pulse width limits
  pinMode(buttonPin, INPUT_PULLUP);
  
  // Set initial servo position
  myServo.write(initialPosition);
}

void loop() {
  // Read button state
  buttonState = digitalRead(buttonPin);
  
  // Check if the button is pressed and the servo is not activated
  if (buttonState == LOW && !servoActivated) {
    servoActivated = true;  // Set servo activation flag
    myServo.write(targetPosition);  // Move servo to the target position
    startTime = millis();  // Record the start time
  }
  
  // Check if the servo is activated and the delay time has passed
  if (servoActivated && (millis() - startTime >= delayTime)) {
    myServo.write(initialPosition);  // Move servo back to the initial position
    servoActivated = false;  // Reset servo activation flag
  }
}

I don't see anything WiFi in your code, so how should it work?

Hey! Yeah the code I provided is the original IDE code that I used to test out the basic idea. I was looking for anyone that would be willing to help convert that basic code to code that is compatible with the arduino IoT cloud. I have been trying to figure out the IoT cloud, but I have had no luck so far.

Still looking at the code but what do you want to control via Iot cloud?

I'm guessing that you want the to make a cloud button.
Check this out https://docs.arduino.cc/arduino-cloud/getting-started/iot-cloud-getting-started
I think a saw a pushbutton example before. If I find it I will give it to you.

I'm not 100% sure this will work as I'm a beginner as well, but you could try this:

If you go to the IoT cloud create a "Thing". You can link this to the device you are using and it will create a sketch for you that is like an empty sketch, but has the necessary framework you need for the cloud.

Your thing will have a setup tab and a sketch tab (and a metadata tab but you wont use it for now).
In the setup tab add a variable boolean. Name it RemoteButton or something.

This will automatically create something in your sketch. If you go to the sketch tab you will see this at the bottom. Leave it alone for now. I would start copying the code from IDE bit by bit to the cloud Sketch. If you are done, see if it still works.

Now at the bottom there is the void that was generated to suit your added variable. If you named it RemoteButton it will be called

void onRemoteButtonChange()  {

}

In this void you can put the same code as you have in your if statements.

You will probably have some trouble getting it to work, maybe you will learn a lot about coding :wink:

To get it to work from your phone for instance you can now go to the Dashboard menu, next to Things, in the top. Here you can add a button to your dashboard. Link the button to your RemoteButton variable and everytime you push the button on your phone you will trigger the onRemoteButtonChange() code in your sketch.

Good luck!

P.S. download the Arduino IoT remote app and log in. You will see the dashboard there.

You can disable desktop to turn off in windows or whatever OS you`re using ?

1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.