#include <WiFi.h>
#include "ThingSpeak.h"
#include <AccelStepper.h>
uint8_t LDR_PIN = T0; // Define pin for LDR (analog pin)
#define TEMP_SENSOR_PIN 34 // Define GPIO number for analog temperature sensor on A1
#define RELAY_PIN 2 // Define GPIO number for the relay
#define STEPPER_PIN1 5 // Define GPIO number for stepper motor wire 1
#define STEPPER_PIN2 18 // Define GPIO number for stepper motor wire 2
#define STEPPER_PIN3 19 // Define GPIO number for stepper motor wire 3
#define STEPPER_PIN4 21 // Define GPIO number for stepper motor wire 4
// Define WiFi credentials
const char* ssid = "Wokwi-GUEST"; // your network SSID (name)
const char* pass = ""; // your network password
WiFiClient client;
// ThingSpeak channel details
unsigned long myChannelNumber = 2294941; // channel number taken from cloud
const char *myWriteAPIKey = "8XEE72XQ6LG7XFML"; // Write API key of cloud
const char *myCounterReadAPIKey = "LU5Y8F8F6YJ6SFB1"; // Read API key from cloud
AccelStepper stepper(AccelStepper::FULL4WIRE, STEPPER_PIN1, STEPPER_PIN2, STEPPER_PIN3, STEPPER_PIN4);
void setup() {
// Initialize serial communication
Serial.begin(115200);
while (!Serial) {
; // wait for the serial port to connect. native USB port only
}
// Initialize ThingSpeak
ThingSpeak.begin(client);
// Set pin modes for various pins
pinMode(RELAY_PIN, OUTPUT);
// Set up the stepper motor
stepper.setMaxSpeed(1000.0);
stepper.setAcceleration(500.0);
}
void loop() {
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED) {
WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using an open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}
// Read temperature from analog sensor
float t = analogRead(TEMP_SENSOR_PIN) * 0.25; // Convert analog value to temperature in Celsius for LM35
Serial.print("Temperature: ");
Serial.println(t);
// Read light status from LDR
int L = analogRead(LDR_PIN);
Serial.print("LDR Value: ");
Serial.println(L);
// Set ThingSpeak fields with sensor values
ThingSpeak.setField(1, t);
ThingSpeak.setField(2, 0); // Since we removed the humidity sensor, set humidity to 0
ThingSpeak.setField(3, L);
// Write to the ThingSpeak channel
int x = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey);
Serial.print("ThingSpeak write result: ");
Serial.println(x);
if (x == 200) {
Serial.println("Channel update successful.");
} else {
Serial.println("Channel update failed. Check ThingSpeak channel details.");
}
// Control relay and stepper motor based on conditions
if (L == 0) {
digitalWrite(RELAY_PIN, HIGH); // Turn on the relay
stepper.moveTo(500); // Move the stepper motor to a specific position
} else {
digitalWrite(RELAY_PIN, LOW); // Turn off the relay
stepper.moveTo(0); // Move the stepper motor to another position
}
stepper.run(); // Run the stepper motor
delay(15000); // Wait for 15 seconds before updating ThingSpeak again
}