hie guys help please, Im trying to edit my code to add write-API key and read-API key.

1 visualizzazione (ultimi 30 giorni)
#include <ESP8266WiFi.h>
#include <LiquidCrystal_I2C.h>
#include <DHT.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include <ThingSpeak.h>
// Wi-Fi credentials
const char* ssid = "YourWiFiSSID";
const char* password = "YourWiFiPassword";
const char* serverAddress = "api.thingspeak.com";
const char* apiKey = "YourThingSpeakAPIKey";
const int updateInterval = 60000; // Update data every 1 minute
// DHT11 Sensor
#define DHTPIN 2
#define DHTTYPE DHT11
#define lightPin A1
DHT dht(DHTPIN, DHTTYPE);
// LCD Display
LiquidCrystal_I2C lcd(0x27, 16, 2); // Change the address if necessary
// Relay Pins
const int fanPin = 3; // Electric Fan
const int dehumidifierPin = 4; // Dehumidifier
const int pumpPin = 5; // Water Pump
const int lightPin = 6; // Light Bulb
// Thresholds
int tempThreshold = 25; // Temperature threshold in degrees Celsius
int humidityThreshold = 60; // Humidity threshold in percentage
int moistureThreshold = 500; // Soil moisture threshold
int lightThreshold = 500; // Light intensity threshold
// Trend Variables
float temperature;
float humidity;
int moisture;
int lightIntensity;
// Wi-Fi Server
WiFiServer server(80);
void setup() {
// Initialize LCD Display
lcd.begin(16, 2);
lcd.print("Initializing...");
// Initialize DHT11 Sensor
dht.begin();
// Initialize Relay Pins
pinMode(fanPin, OUTPUT);
pinMode(dehumidifierPin, OUTPUT);
pinMode(pumpPin, OUTPUT);
pinMode(lightPin, OUTPUT);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
lcd.setCursor(0, 1);
lcd.print("Connecting...");
}
// Start the Wi-Fi server
server.begin();
// Display IP address on LCD
lcd.clear();
lcd.print("Connected!");
lcd.setCursor(0, 1);
lcd.print("IP: ");
lcd.print(WiFi.localIP());
delay(2000);
}
void loop() {
// Check for new client connections
WiFiClient client = server.available();
if (client) {
// Process the client request
processClientRequest(client);
}
// Read sensor values
temperature = dht.readTemperature();
humidity = dht.readHumidity();
moisture = analogRead(A0);
lightIntensity = analogRead(A1);
// Display readings on LCD
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp: ");
lcd.print(temperature);
lcd.print("C");
lcd.print("Humidity: ");
lcd.print(humidity);
lcd.print("%");
lcd.setCursor(0, 1);
lcd.print("Moisture: ");
lcd.print(moisture);
lcd.print("mm");
lcd.print("L.intenstity: ");
lcd.print(lightIntensity);
// Check and control parameters
if (temperature > tempThreshold) {
digitalWrite(fanPin, HIGH); // Turn on the fan
} else {
digitalWrite(fanPin, LOW); // Turn off the fan
}
if (humidity > humidityThreshold) {
digitalWrite(dehumidifierPin, HIGH); // Turn on the dehumidifier
} else {
digitalWrite(dehumidifierPin, LOW); // Turn off the dehumidifier
}
if (moisture < moistureThreshold) {
digitalWrite(pumpPin, HIGH); // Turn on the water pump
} else {
digitalWrite(pumpPin, LOW); // Turn off the water pump
}
if (lightIntensity < lightThreshold) {
digitalWrite(lightPin, HIGH); // Turn on the light bulb
} else {
digitalWrite(lightPin, LOW); // Turn off the light bulb
}
delay(2000); // Update readings every 2 seconds
}
void processClientRequest(WiFiClient& client) {
String request = client.readStringUntil('\r');
client.flush();
// Check if the request is to adjust thresholds
if (request.indexOf("/thresholds") != -1) {
// Extract the values from the request
int equalsIndex = request.indexOf('=');
int ampersandIndex = request.indexOf('&');
//int newTempThreshold = request.substring(equalsIndex + 1, apologize, but it seems that the code got cut off) //Here's the rest of the code:
int newTempThreshold = request.substring(equalsIndex + 1, ampersandIndex).toInt();
equalsIndex = request.indexOf('=', ampersandIndex);
ampersandIndex = request.indexOf('&', ampersandIndex + 1);
int newHumidityThreshold = request.substring(equalsIndex + 1, ampersandIndex).toInt();
equalsIndex = request.indexOf('=', ampersandIndex);
ampersandIndex = request.indexOf('&', ampersandIndex + 1);
int newMoistureThreshold = request.substring(equalsIndex + 1, ampersandIndex).toInt();
equalsIndex = request.indexOf('=', ampersandIndex);
int newLightThreshold = request.substring(equalsIndex + 1).toInt();
// Update the thresholds
tempThreshold = newTempThreshold;
humidityThreshold = newHumidityThreshold;
moistureThreshold = newMoistureThreshold;
lightThreshold = newLightThreshold;
// Send a response to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body>");
client.println("<h1>Thresholds updated successfully</h1>");
client.println("</body></html>");
}
else if (request.indexOf("/trends") != -1) {
// Send the recorded trends to the client
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body>");
client.println("<h1>Recorded Trends</h1>");
// Display the recorded trends
client.print("Temperature: ");
client.print(temperature);
client.println("C");
client.print("Humidity: ");
client.print(humidity);
client.println("%");
client.print("Moisture: ");
client.print(moisture);
client.print("Light Intensity: ");
client.print(lightIntensity);
client.println("</body></html>");
}
else {
// Send a 404 Not Found response
client.println("HTTP/1.1 404 Not Found");
client.println("Content-Type: text/html");
client.println();
client.println("<html><body>");
client.println("<h1>404 Not Found</h1>");
client.println("</body></html>");
}
// Close the connection
client.stop();
}

Risposte (1)

Satwik
Satwik il 16 Set 2024
Hello Archiford,
To interact with ‘ThingSpeak’ for reading and writing data, setting up a ‘ThingSpeak’ channel is necessary. This setup provides the essential read and write API keys for use in code. If a channel has not been created yet, follow the steps as detailed in this example:
  1. Sign in to ‘ThingSpeak’ using MathWorks account credentials or create a new account if necessary.
  2. Navigate to Channels > My Channels.
  3. On the Channels page, click ‘New Channel’.
  4. Enter the required channel setting values.
  5. Save the channel by clicking ‘Save Channel’ at the bottom of the settings page.
After setting up the channel, the API keys can be viewed in the 'API Keys' tab. In the code, define the API keys as follows:
% ThingSpeak API keys
const char* writeAPIKey = "WriteAPIKey"; % Replace with the Write API Key
const char* readAPIKey = "ReadAPIKey"; % Replace with the Read API Key
These keys allow reading from and writing to the 'ThingSpeak' channel using the commands 'thingSpeakRead' and 'thingSpeakWrite'. Here is the syntax:
% To read data
data = thingSpeakRead(___, ReadKey='channel Read API key');
% To write data
thingSpeakWrite(channelID, data, 'WriteKey', 'channel write API key');
For further details on these methods, refer to the following documentation:
Hope this helps!

Categorie

Scopri di più su Write Data to Channel in Help Center e File Exchange

Tag

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by