i can't use 'GET' in copper.I know that ultrasonic sensors can't be set like lights (binary data). but I just want to get a description of the if parameter I've made
if (jarak < 50) {
Serial.println ("jarak Aman ");
}
else {
Serial.println ("harus diisi");
this is the full code
#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);
}
To analyse the issue, you may also try other clients, e.g. libcoap or Californium. If that still results in trouble, a IP capture helps to understand the case.
Or, is your question more about your server-side code than about coap? Then some logs from your server may help to understand, how it works and to find the issue there.