0

I am trying to make a CoAP NON server using CoAPSharp binary in Visual Studio 2015. My target device is Raspberry Pi with Windows IoT core.

"Remotesender" part has the error. I don't know how to solve it

/// <summary>
/// Called when a request is received
/// </summary>
/// <param name="coapReq">The CoAPRequest object</param>
void OnCoAPRequestReceived(CoAPRequest coapReq)
{
    //This sample only works on NON requests of type GET
    //This sample simualtes a temperature sensor at the path "sensors/temp"

    string reqURIPath = (coapReq.GetPath() != null) ? coapReq.GetPath    ().ToLower() : "";
/**
        * Draft 18 of the specification, section 5.2.3 states, that if     against a NON message,
        * a response is required, then it must be sent as a NON message
        */
    if (coapReq.MessageType.Value != CoAPMessageType.NON)
    {
        //only NON  combination supported..we do not understand this send a RST back
        CoAPResponse msgTypeNotSupported = new CoAPResponse    (CoAPMessageType.RST, /*Message type*/
                                                            CoAPMessageCode.NOT_IMPLEMENTED, /*Not implemented*/
                                                            coapReq.ID.Value /*copy message Id*/);
        msgTypeNotSupported.Token = coapReq.Token; //Always match the     request/response token
        msgTypeNotSupported.RemoteSender = coapReq.RemoteSender;
        //send response to client
        this._coapServer.Send(msgTypeNotSupported);
    }
    else if (coapReq.Code.Value != CoAPMessageCode.GET)
    {
        //only GET method supported..we do not understand this send a RST back
        CoAPResponse unsupportedCType = new CoAPResponse    (CoAPMessageType.RST, /*Message type*/
                                        CoAPMessageCode.METHOD_NOT_ALLOWED, /*Method not allowed*/
                                        coapReq.ID.Value /*copy message Id*/);
        unsupportedCType.Token = coapReq.Token; //Always match the request/response token
        unsupportedCType.RemoteSender = coapReq.RemoteSender;
        //send response to client
        this._coapServer.Send(unsupportedCType);
}
    else if (reqURIPath != "sensors/temp")
{
        //classic 404 not found..we do not understand this send a RST back 
        CoAPResponse unsupportedPath = new CoAPResponse    (CoAPMessageType.RST, /*Message type*/
                                            CoAPMessageCode.NOT_FOUND, /*Not found*/
                                            coapReq.ID.Value /*copy message Id*/);
        unsupportedPath.Token = coapReq.Token; //Always match the     request/response token
        unsupportedPath.RemoteSender = coapReq.RemoteSender;
        //send response to client
        this._coapServer.Send(unsupportedPath);
    }
    else
    {
        //All is well...send the measured temperature back
    //Again, this is a NON message...we will send this message as a JSON
    //string
    Hashtable valuesForJSON = new Hashtable();
    valuesForJSON.Add("temp", this.GetRoomTemperature());
    string tempAsJSON = JSONResult.ToJSON(valuesForJSON);
    //Now prepare the object
    CoAPResponse measuredTemp = new CoAPResponse(CoAPMessageType.NON, /*Message type*/
                                        CoAPMessageCode.CONTENT, /*Carries content*/
                                        coapReq.ID.Value/*copy message Id*/);
    measuredTemp.Token = coapReq.Token; //Always match the request/response token
    //Add the payload
    measuredTemp.Payload = new CoAPPayload(tempAsJSON);
    //Indicate the content-type of the payload
    measuredTemp.AddOption(CoAPHeaderOption.CONTENT_FORMAT,
                        AbstractByteUtils.GetBytes(CoAPContentFormatOption.APPLICATION_JSON));
    //Add remote sender address details
    measuredTemp.RemoteSender = coapReq.RemoteSender;
    //send response to client
    this._coapServer.Send(measuredTemp);
}
}

Error:

error: CS1061 C# does not contain a definition for and no extension method accepting a first argument of type could be found (are you missing a using directive or an assembly reference?)

Simply following the tutorial on CoAPSharp official website.

halfer
  • 19,824
  • 17
  • 99
  • 186
IoT
  • 1
  • 4

1 Answers1

0

If you want to run the CoAPSharp library on Raspberry Pi with Windows 10 IoT core, you need to download the experimental release for Windows 10 IoT core. The URL is http://www.coapsharp.com/releases/ . See the last download link.

Also, you may want yo get the source and recompile to get the latest binary to reference in your main project.

Additional information added on 3-Nov-16: OK, I understood what the problem is. Here is a full and large answer :-)

  1. The CoAPSharp experimental library has a small bug (although unrelated to your problem). I am one of the CoAPSharp developers and work for the company which built the library. The updated library with the fix should be available in a day or two. The issue was with synchronous receive.

  2. The sample you are trying to run, is for NETMF and not for Windows 10 IoT Core and that is why you are getting the errors. There are no samples for the Raspberry Pi experimental release. To help you with the problem, I'm giving below step-by-step solution:

  3. First, download the latest CoAPSharp experimental library to get the fix for synchronous receive bug.

  4. Next, create a solution in which, create one UWA project and add the CoAPSharp library to the solution. Reference the library in the UWA project.

5.1 Create a small server code for Raspberry Pi in this project, as shown below:

    /// <summary>
    /// We will start a server locally and then connect to it
    /// </summary>
    private void TestLocalCoAPServer()
    {
        /*_coapServer variable is a class level variable*/
        _coapServer = new CoAPServerChannel();
        _coapServer.CoAPRequestReceived += OnCoAPRequestReceived;
        _coapServer.Initialize(null, 5683);
    }
    /// <summary>
    /// Gets called everytime a CoAP request is received
    /// </summary>
    /// <param name="coapReq">The CoAP Request object instance</param>
    private async void OnCoAPRequestReceived(CoAPRequest coapReq)
    {
        //send ACK back
        Debug.WriteLine("Received Request::" + coapReq.ToString());
        CoAPResponse coapResp = new CoAPResponse(CoAPMessageType.ACK, CoAPMessageCode.CONTENT, coapReq);
        coapResp.AddPayload("GOT IT!");
        await _coapServer.Send(coapResp);
    }

5.2 Next, ensure that port 5683 is not blocked on Raspberry Pi. If yes, then use the powershell to unblock it.

5.3 Next, create a new solution on your desktop(I used Windows 10 and Visual Studio 2015 Community edition) and add another copy of CoAPSharp Raspberry Pi experimental library.

5.3 Now create another UWA project on your desktop . Also, refer the CoAPSharp project in this new project.

5.4 Now write a client in this project that will send/receive CoAP messages from the server hosted on the Raspberry Pi:

    private async void TestCoAPAsyncClient()
    {
        /*_coapAsyncClient is declared at class level*/
        this._coapAsyncClient = new CoAPClientChannel();
        /*minwinpc was the name of the device running Windows IoT core on Raspberry Pi*/
        this._coapAsyncClient.Initialize("minwinpc", 5683);
        this._coapAsyncClient.CoAPError += delegate (Exception e, AbstractCoAPMessage associatedMsg) {
            Debug.WriteLine("Exception e=" + e.Message);
            Debug.WriteLine("Associated Message=" + ((associatedMsg != null) ? associatedMsg.ToString() : "NULL"));
        };
        this._coapAsyncClient.CoAPRequestReceived += delegate (CoAPRequest coapReq) {
            Debug.WriteLine("REQUEST RECEIVED !!!!!!!!!!!!!!" + coapReq.ToString());
        };
        this._coapAsyncClient.CoAPResponseReceived += delegate (CoAPResponse coapResp) {
            Debug.WriteLine("RESPONSE RECEIVED <<<<<<<<<<<<<" + coapResp.ToString());
        };

        CoAPRequest req = new CoAPRequest(CoAPMessageType.CON, CoAPMessageCode.GET, 100);
        //req.SetURL("coap://capi.coapworks.com:5683/v1/time/pcl?mid=CPWK-TESTM");
        req.SetURL("coap://localhost:5683/someurl");
        await this._coapAsyncClient.Send(req);
    }
  1. You can call the method TestCoAPAsyncClient() in a button click or when the page is loaded.

  2. The setup I used was, one desktop with Windows 10 and Visual Studio Community Edition 2015. This was connected to Raspberry Pi running Windows 10 IoT core (OS: 10.0.10240.16384). Both my desktop and the Raspberry Pi were connected to ethernet.

CoAPSharp team added a sample at the link http://www.coapsharp.com/coap-server-windows-10-iot-core-raspberry-pi/ to elaborate this further.

Vishnu
  • 71
  • 1
  • 8
  • Thank you so much fro your response,Yes, I have completed all the above mentioned requirements. I need to know the exact method of how to establish a server and client.Can i run both server and client code on same PC? – IoT Nov 03 '16 at 04:49
  • I have also added CoAP Sharp binary after its compilation.While compiling the server code, it mentions the error of 'RemoteSender'.Obviously because it has no place to send data from server.I am really confused.. – IoT Nov 03 '16 at 04:55
  • Added more information in my original answer. Hope this will now help you. – Vishnu Nov 03 '16 at 13:52
  • Thank you SO much Vishnu.Let me give it a try.I hope it will work.Just making it more clear, after making these projects I need to just Build it and deploy it to Raspberry pi through remote device option and later I shall check the well/known core through Copper(CU)? – IoT Nov 03 '16 at 14:04
  • Two desktops, one with IoT core Edition connected to raspberry pi and other with windows 10 and visual studio?I also have both desktops connected to Ethernet but different ones.I hope it doesn't matter.I am really thankful for your concern.will be needing your help in future too:) – IoT Nov 03 '16 at 14:06
  • Yes, that is correct. After you build and deploy using "Remote Device" option, you must ensure that the main program is running. Post that, you can either use Copper plugin (Firefox) or you can write code to check. I also tested with NETMF client and it works. – Vishnu Nov 03 '16 at 14:07
  • You need one PC to build , deploy and interact with Raspberry Pi. Another PC to write the client code. You can do both on the same PC too. Just open two instance of Visual Studio. In one, run the Raspberry Pi code and in another, run your client code. – Vishnu Nov 03 '16 at 14:10
  • Hi again, almost everything is working smooth but still cant get rid of this following error. Error: "Exporting unsealed types is not supported. Please mark type 'RPServer1.program' as sealed". P.S While building this project, remote connection with the device wasn't established" – IoT Nov 04 '16 at 06:31
  • It would be great if you can post your code and elaborate a bit more on what you are trying to do. – Vishnu Nov 04 '16 at 11:03
  • You might also want to see [link](http://stackoverflow.com/questions/36334929/inheritance-impossible-in-windows-runtime-component/36335347) – Vishnu Nov 04 '16 at 12:00
  • Thank you.Problem solved..Now I am working on port 5683 of RP, need to get it unblocked. – IoT Nov 05 '16 at 04:41
  • If you can please guide me on how to unblock port 5683, otherwise I have to go through lots of google stuff.Need to end up this server/client thing as soon as possible:) – IoT Nov 05 '16 at 04:51
  • You need to do two things: – Vishnu Nov 05 '16 at 13:46
  • 1. Access Raspberry Pi using Windows Powershell. [See here](https://developer.microsoft.com/en-us/windows/iot/docs/powershell) – Vishnu Nov 05 '16 at 13:46
  • 2. Next, add rule to firewall to allow incoming connection to UDP port 5683. [See here] (https://wiki.mcneel.com/zoo/zoo5netsh) Just use UDP port – Vishnu Nov 05 '16 at 13:51
  • Hi @Vishnu ,I have completed all the steps above.It' been a while I am trying to figure out the output.I don't know what should be the possible output of above mentioned codes.When I open Copper add-on its blank.I send commands like GET,but no reply form the server.Plus when I telnet Rp's server IP address to listen at port 5683 it doesn't respond even the powershell configuration was complete – IoT Nov 14 '16 at 07:01
  • I have another question too.What if one RP is server and other is the client.With codes deployed. How can I see the output or whats going on in there? – IoT Nov 14 '16 at 07:05
  • The simplest way to check is to write a CoAP client application on your PC. Send the CoAPRequest and when you get the response, you can use the "ToString" method of the response object to print the response on the console. In order to know what is the output, you can create a forms application for RaspberryPi Windows IoT 10 core and write the output on the screen of your client. – Vishnu Nov 14 '16 at 11:32
  • Hi vishnu I have deployed client's code on my PC and server code is deployed on raspberry pi.How can I see any request coming? – IoT Nov 15 '16 at 05:01
  • I mean what will be the interface through which I can see what is going on in there – IoT Nov 15 '16 at 05:39
  • The server code stops with the error "The program '[2568] backgroundTaskHost.exe' has exited with code 1 (0*1).This is an issue, right? – IoT Nov 15 '16 at 05:44
  • I would suggest that instead of discussing this here, you should write to CoAPSharp team, it would be far easier. Since these questions are not directly related to the original question, please visit [CoAPSharp Support](http://www.coapsharp.com/contact-us/) and write to them. They will work with you on this. – Vishnu Nov 15 '16 at 06:59