/*
ESP-COAP Client
*/
#include <WiFi.h>
#include "coap_client.h"
//instance for coapclient
coapClient coap;
//WiFi connection info
const char* ssid = "Wokwi-GUEST";
const char* password = "";
//http://coap.me/coap://coap.me/seg1
//ip address and default port of coap server in which your interested in
IPAddress ip(129,132,15,80);//take ETH Zurich or coap.me server to run and check client
int port =5683;
// coap client response callback
void callback_response(coapPacket &packet, IPAddress ip, int port);
// coap client response callback
void callback_response(coapPacket &packet, IPAddress ip, int port) {
char p[packet.payloadlen + 1];
memcpy(p, packet.payload, packet.payloadlen);
p[packet.payloadlen] = NULL;
//response from coap server
if(packet.type==3 && packet.code==0){
Serial.println("ping ok");
}
Serial.println(p);
}
void setup() {
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println(" ");
// Connection info 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 of client
Serial.println(WiFi.localIP());
// client response callback.
// this endpoint is single callback.
coap.response(callback_response);
// start coap client
coap.start();
//get request to server (arguments ip adrress of server,default port,resource(uri))
int msgid = coap.get(ip,port,"light");
}
void loop() {
bool state;
state= coap.loop();
delay(1000);
}