0

我不能在铜中使用“GET”。我知道超声波传感器不能像灯一样设置(二进制数据)。但我只想了解我所做的 if 参数

   if (jarak < 50) {
    Serial.println ("jarak Aman ");
    
    }
   
   else {
    Serial.println ("harus diisi");

这是完整的代码

#include <ESP8266WiFi.h>
#include <coap_server.h>
#include <Ultrasonic.h>


#define triggerPin  D8
#define echoPin     D7

char * duration, jarak;

coapServer coap;

//WiFi connection info
const char* ssid = "ZTE-d17d4e";
const char* password = "amatapit";

bool LEDSTATE;

//Setup


void setup() {
  yield();


  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //serial begin
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println(" ");

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    //delay(500);
    yield();
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  // Print the IP address
  Serial.println(WiFi.localIP());

  
  // LED State
  pinMode(16, OUTPUT);
  digitalWrite(16, HIGH);
  LEDSTATE = true;

  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  //LEDSTATE = true;




  // add server url endpoints.
  // can add multiple endpoint urls.

  coap.server(callback_light, "light");
  coap.server(callback_sensor, "sensor");

  // start coap server/client
  coap.start();
  // coap.start(5683);
}

void loop() {
  coap.loop();
  delay(1000);

long duration, jarak;
   
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2); 
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH);
   //inches = microsecondsToInches(duration);
//    cm = microsecondsToCentimeters(duration);
   jarak = duration/29/2;
   Serial.print("Jarak :");
   Serial.println(jarak);
   Serial.print(" cm");
   Serial.println("");
   if (jarak < 50) {
    Serial.println ("jarak Aman ");
    
    }
   
   else {
    Serial.println ("harus diisi");
   }
   
  delay(500);


}



// CoAP server endpoint URL
void callback_light(coapPacket *packet, IPAddress ip, int port,int obs) {
  Serial.println("light");

  // send response
  char p[packet->payloadlen + 1];
  memcpy(p, packet->payload, packet->payloadlen);
  p[packet->payloadlen] = NULL;
  Serial.println(p);

  String message(p);

  if (message.equals("0"))
  {
    digitalWrite(16,LOW);
    Serial.println("Lampu Mati");
  }
  else if (message.equals("1"))
  {
    digitalWrite(16,HIGH);
    Serial.println("Lampu Menyala");
  } 
  char *light = (digitalRead(16) > 0)? ((char *) "Lampu Menyala:") :((char *) "Lampu Mati");
  
   //coap.sendResponse(packet, ip, port, light);
   if(obs==1)
    coap.sendResponse(light);
   else
    coap.sendResponse(ip,port,light);
 
}


void callback_sensor(coapPacket *packet, IPAddress ip, int port,int obs) {
  Serial.println("sensor");

// send response
  char p[packet->payloadlen + 1];
  memcpy(p, packet->payload, packet->payloadlen);
  p[packet->payloadlen] = NULL;
  Serial.println(p);

  String message(p);
 if (message.equals("1"))
  {
   // digitalWrite(16,LOW);
    Serial.println(jarak);
  }
  else if (message.equals("2"))
  {
   // digitalWrite(16,HIGH);
    Serial.println(jarak);
  } 


  
  char *sensor = ((jarak) > 0)? ((char *) "Nyala nih") :((char *) "Mati");
 
  
  
   //coap.sendResponse(packet, ip, port, sensor);
   if(obs==1)
    coap.sendResponse(sensor);
   else
    coap.sendResponse(ip,port,sensor);
}#include <ESP8266WiFi.h>
#include <coap_server.h>
#include <Ultrasonic.h>


#define triggerPin  D8
#define echoPin     D7

char * duration, jarak;

coapServer coap;

//WiFi connection info
const char* ssid = "ZTE-d17d4e";
const char* password = "amatapit";

bool LEDSTATE;

//Setup


void setup() {
  yield();


  pinMode(triggerPin, OUTPUT);
  pinMode(echoPin, INPUT);
  //serial begin
  Serial.begin(115200);

  WiFi.begin(ssid, password);
  Serial.println(" ");

  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    //delay(500);
    yield();
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
  // Print the IP address
  Serial.println(WiFi.localIP());

  
  // LED State
  pinMode(16, OUTPUT);
  digitalWrite(16, HIGH);
  LEDSTATE = true;

  pinMode(5, OUTPUT);
  digitalWrite(5, HIGH);
  //LEDSTATE = true;




  // add server url endpoints.
  // can add multiple endpoint urls.

  coap.server(callback_light, "light");
  coap.server(callback_sensor, "sensor");

  // start coap server/client
  coap.start();
  // coap.start(5683);
}

void loop() {
  coap.loop();
  delay(1000);

long duration, jarak;
   
  digitalWrite(triggerPin, LOW);
  delayMicroseconds(2); 
  digitalWrite(triggerPin, HIGH);
  delayMicroseconds(10); 
  digitalWrite(triggerPin, LOW);
  duration = pulseIn(echoPin, HIGH);
   //inches = microsecondsToInches(duration);
//    cm = microsecondsToCentimeters(duration);
   jarak = duration/29/2;
   Serial.print("Jarak :");
   Serial.println(jarak);
   Serial.print(" cm");
   Serial.println("");
   if (jarak < 50) {
    Serial.println ("jarak Aman ");
    
    }
   
   else {
    Serial.println ("harus diisi");
   }
   
  delay(500);


}



// CoAP server endpoint URL
void callback_light(coapPacket *packet, IPAddress ip, int port,int obs) {
  Serial.println("light");

  // send response
  char p[packet->payloadlen + 1];
  memcpy(p, packet->payload, packet->payloadlen);
  p[packet->payloadlen] = NULL;
  Serial.println(p);

  String message(p);

  if (message.equals("0"))
  {
    digitalWrite(16,LOW);
    Serial.println("Lampu Mati");
  }
  else if (message.equals("1"))
  {
    digitalWrite(16,HIGH);
    Serial.println("Lampu Menyala");
  } 
  char *light = (digitalRead(16) > 0)? ((char *) "Lampu Menyala:") :((char *) "Lampu Mati");
  
   //coap.sendResponse(packet, ip, port, light);
   if(obs==1)
    coap.sendResponse(light);
   else
    coap.sendResponse(ip,port,light);
 
}


void callback_sensor(coapPacket *packet, IPAddress ip, int port,int obs) {
  Serial.println("sensor");

// send response
  char p[packet->payloadlen + 1];
  memcpy(p, packet->payload, packet->payloadlen);
  p[packet->payloadlen] = NULL;
  Serial.println(p);

  String message(p);
 if (message.equals("1"))
  {
   // digitalWrite(16,LOW);
    Serial.println(jarak);
  }
  else if (message.equals("2"))
  {
   // digitalWrite(16,HIGH);
    Serial.println(jarak);
  } 


  
  char *sensor = ((jarak) > 0)? ((char *) "Nyala nih") :((char *) "Mati");
 
  
  
   //coap.sendResponse(packet, ip, port, sensor);
   if(obs==1)
    coap.sendResponse(sensor);
   else
    coap.sendResponse(ip,port,sensor);
}
4

1 回答 1

0

要分析问题,您还可以尝试其他客户端,例如libcoapCalifornium。如果这仍然导致问题,IP 捕获有助于了解情况。

或者,您的问题更多是关于您的服务器端代码而不是关于 coap?然后,来自您服务器的一些日志可能有助于了解它的工作原理并在那里找到问题。

于 2021-06-08T06:49:48.630 回答