2018年2月1日 星期四

COAP Server

COAP on ESP8266

源自於 http://www.ikanbilis.com/362.php
COAP (constrained Application Protocol) is a protocol for limited resourced IOT devices like Arduino and ESP8266 to exhange data in Client-Server method.
Steps:
1) Download library from https://github.com/automote/ESP-CoAP
2) Put the downloded folder in Arduino Library.
3) I am using Nodemcu 12E. Normal ESP8266 will work.
4) Get a Firefox version 53 and install plug-in named Copper.
5) https://addons.mozilla.org/en-US/firefox/addon/copper-270430
6) Connect a LED on D0 on Nodemcu.
7) Upload the coapserver.ino to Nodemcu
8) Type 1 in outgoing panel and click PUT
9) A return string PUT Ok appear in incoming panel
10)The LED will on





/*
ESP-COAP Server
D0 = GPIO16;  D1 = GPIO5;   D2 = GPIO4;  D3 = GPIO0;
D4 = GPIO2;   D5 = GPIO14;  D6 = GPIO12; D7 = GPIO13;
D8 = GPIO15;  D9 = GPIO3;   D10 = GPIO1; LED_BUILTIN = GPIO16 (auxiliary constant for the board LED, not a board pin);
*/

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

// CoAP server endpoint url callback
void callback_light(coapPacket &packet, IPAddress ip, int port, int obs);
void callback_lightled(coapPacket &packet, IPAddress ip, int port,int obs);

coapServer coap;

//WiFi connection info
const char* ssid = "74170287";
const char* password = "24063173";

// LED STATE
bool LEDSTATE;

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

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

  String message(p);
  Serial.println(p);

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

}


void callback_lightled(coapPacket *packet, IPAddress ip, int port,int obs) {
  Serial.print("Lightled");

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

  String message(p);

  if (message.startsWith("0"))
    LEDSTATE = false;
  else if (message.startsWith("1"))
    LEDSTATE = true;

  if (LEDSTATE) {
    digitalWrite(5, HIGH) ;
    Serial.println("LED=high");
    if(obs==1)
     coap.sendResponse("1");
     else
    coap.sendResponse(ip, port, "1");
    
    //coap.sendResponse("1");
  } else {
    digitalWrite(5, LOW) ;
    Serial.println("LED=low");
    if (obs==1)
    coap.sendResponse("0");
    else
    coap.sendResponse(ip, port, "0");
    //coap.sendResponse("0");
  }
}


void setup() {
  yield();
  //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_lightled, "lightled");
 // coap.server(callback_text,"text");

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

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

}

沒有留言:

張貼留言

Node-Red & ModeBus FC=1

Node-Red & ModeBus FC=1 write address=0..7   8bits read address=16..23 8bits  [{"id":"cf7c1253d4d997dd","type&q...