下面的代码是整个项目的代码。它真的不工作,所以我需要帮助。
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
#define TRIGGER_PIN1 7
#define ECHO_PIN1 6
#define TRIGGER_PIN2 9
#define ECHO_PIN2 8
#define GREEN_LED_PIN1 2
#define GREEN_LED_PIN2 3
#define RED_LED_PIN1 4
#define RED_LED_PIN2 5
const char* ssid = "A";
const char* password = "B";
const char* mqttServer = "mqtt.thingspeak.com";
const int mqttPort = 1883;
const char* mqttUsername = "ECYYHBMYHQIRLxUzJgghEyU";
const char* mqttPassword = "XF+iiofTxPi4o/o8ycNXEyWF";
const char* channelID = "2164468";
const char* writeAPIKey = "MMXEWBFUDA1XJLRO";
LiquidCrystal_I2C lcd(0x27, 16, 2);
int Seat1 = 0, Seat2 = 0;
int Seat = 2;
WiFiClient espClient;
PubSubClient client(espClient);
void setup() {
Serial.begin(9600);
pinMode(TRIGGER_PIN1, OUTPUT);
pinMode(ECHO_PIN1, INPUT);
pinMode(TRIGGER_PIN2, OUTPUT);
pinMode(ECHO_PIN2, INPUT);
pinMode(GREEN_LED_PIN1, OUTPUT);
pinMode(GREEN_LED_PIN2, OUTPUT);
pinMode(RED_LED_PIN1, OUTPUT);
pinMode(RED_LED_PIN2, OUTPUT);
lcd.init();
lcd.backlight();
lcd.clear();
lcd.print("Hello, Welcome to");
lcd.setCursor(0, 1);
lcd.print("Seat Reservation");
lcd.setCursor(0, 2);
lcd.print("System");
delay(2000);
lcd.clear();
int total = Seat1 + Seat2;
Seat = Seat - total;
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
client.setServer(mqttServer, mqttPort);
while (!client.connected()) {
if (client.connect("NodeMCU", mqttUsername, mqttPassword)) {
Serial.println("Connected to MQTT broker");
} else {
Serial.print("Failed, rc=");
Serial.print(client.state());
Serial.println(" Retrying in 5 seconds...");
delay(5000);
}
}
}
void loop() {
long duration1, distance1;
digitalWrite(TRIGGER_PIN1, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN1, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN1, LOW);
duration1 = pulseIn(ECHO_PIN1, HIGH);
distance1 = duration1 * 0.034 / 2;
long duration2, distance2;
digitalWrite(TRIGGER_PIN2, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN2, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN2, LOW);
duration2 = pulseIn(ECHO_PIN2, HIGH);
distance2 = duration2 * 0.034 / 2;
lcd.setCursor(0, 0);
lcd.print("Total Seat: ");
lcd.print(Seat);
lcd.print(" ");
lcd.setCursor(0, 1);
if (Seat1 == 1) {
lcd.print("Seat 1: Fill ");
} else {
lcd.print("Seat 1: Empty");
}
lcd.setCursor(0, 2);
if (Seat2 == 1) {
lcd.print("Seat 2: Fill ");
} else {
lcd.print("Seat 2: Empty");
}
if (distance1 > 2) {
digitalWrite(GREEN_LED_PIN1, HIGH);
digitalWrite(RED_LED_PIN1, LOW);
Seat1 = 0;
} else {
digitalWrite(GREEN_LED_PIN1, LOW);
digitalWrite(RED_LED_PIN1, HIGH);
Seat1 = 1;
}
if (distance2 > 2) {
digitalWrite(GREEN_LED_PIN2, HIGH);
digitalWrite(RED_LED_PIN2, LOW);
Seat2 = 0;
} else {
digitalWrite(GREEN_LED_PIN2, LOW);
digitalWrite(RED_LED_PIN2, HIGH);
Seat2 = 1;
}
const char* payload = "Sample payload"; // Replace "Sample payload" with your actual payload data
if (client.connected()) {
String topic = "channels/" + String(channelID) + "/publish/" + String(writeAPIKey);
const char* topicPtr = topic.c_str();
const char* payloadPtr = payload;
client.publish(topicPtr, payloadPtr);
Serial.println("Data sent to ThingSpeak");
} else {
Serial.println("Failed to send data to ThingSpeak");
}
delay(200);
}
我写的代码。今天我试着运行它。但它不起作用,因为在我的ThingSpeak通道没有任何数据。请用其他代码帮我弄清楚。这是我的个人项目。该项目的主题是座位预订系统与物联网。
1条答案
按热度按时间ffvjumwh1#
使用“ThingSpeak.h”然后尝试。参考以下链接https://github.com/mathworks/thingspeak-arduino