1248/microcoap

Arduino due do not work?

Opened this issue · 0 comments

Arduino due can not use Ethernet,So I changed the microcoap.ino like thie
/*

  • WARNING - UDP_TX_PACKET_MAX_SIZE is hardcoded by Arduino to 24 bytes
  • This limits the size of possible outbound UDP packets
    */
    #include <Triton_WiFi.h>
    #include <SPI.h>
    #include <ccspi.h>
    #include "utility/debug.h"
    #include <stdint.h>
    #include <EthernetUdp.h>
    #include "coap.h"
    #include <string.h>
    #include <Wire.h>

#define WLAN_SSID "ASUS" // cannot be longer than 32 characters!
#define WLAN_PASS "96881000"
// Security can be WLAN_SEC_UNSEC, WLAN_SEC_WEP, WLAN_SEC_WPA or WLAN_SEC_WPA2
#define WLAN_SECURITY WLAN_SEC_WPA2
#define PORT 5683
EthernetUDP udp;
uint8_t packetbuf[256];
static uint8_t scratch_raw[32];
static coap_rw_buffer_t scratch_buf = {scratch_raw, sizeof(scratch_raw)};
uint32_t ip;

bool displayConnectionDetails(void)
{
uint32_t ipAddress, netmask, gateway, dhcpserv, dnsserv;
if(!wifi.getIPAddress(&ipAddress, &netmask, &gateway, &dhcpserv, &dnsserv))
{
Serial.println(F("Unable to retrieve the IP Address!\r\n"));
return false;
}
else
{
Serial.print(F("\nIP Addr: ")); wifi.printIPdotsRev(ipAddress);
Serial.print(F("\nNetmask: ")); wifi.printIPdotsRev(netmask);
Serial.print(F("\nGateway: ")); wifi.printIPdotsRev(gateway);
Serial.print(F("\nDHCPsrv: ")); wifi.printIPdotsRev(dhcpserv);
Serial.print(F("\nDNSserv: ")); wifi.printIPdotsRev(dnsserv);
Serial.println();
return true;
}
}
void InitHP() //init the color box
{
Wire.begin();
Wire.beginTransmission(byte(0x20)); //0x40 >> 1
Wire.write(byte(0x06));
Wire.write(byte(0x00));
Wire.write(byte(0x00));
Wire.endTransmission();
}
void WriteHP(uint16_t hp)
{
int data;
Wire.beginTransmission(byte(0x20)); //0x40 >> 1
data = 0xffff<<hp;
Wire.write(byte(0x02));
Wire.write(data & 0xff);
Wire.write((data & 0xff00) >> 8);
Wire.endTransmission();
}

void setup()
{
InitHP();
Serial.begin(9600);
while (!Serial)
{
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println(F("\nInitializing..."));
if (!wifi.begin())
{
Serial.println(F("Couldn't begin()! Check your wiring?"));
while(1);
}

// Optional SSID scan
// listSSIDResults();
Serial.print(F("\nAttempting to connect to ")); Serial.println(WLAN_SSID);
if (!wifi.connectToAP(WLAN_SSID, WLAN_PASS, WLAN_SECURITY)) {
Serial.println(F("Failed!"));
while(1);
}
Serial.println(F("Connected!"));

/* Wait for DHCP to complete */
Serial.println(F("Request DHCP"));
while (!wifi.checkDHCP())
{
  delay(100); // ToDo: Insert a DHCP timeout!
}  

/* Display the IP address DNS, Gateway, etc. */  
while (! displayConnectionDetails()) {
delay(1000);}
Serial.println();
udp.begin(PORT);

coap_setup();
endpoint_setup();

}

void udp_send(const uint8_t *buf, int buflen)
{
udp.beginPacket(udp.remoteIP(), udp.remotePort());
while(buflen--)
udp.write(*buf++);
udp.endPacket();
}

void loop()
{
int sz;
int rc;
coap_packet_t pkt;
int i;

if ((sz = udp.parsePacket()) > 0)
{
    udp.read(packetbuf, sizeof(packetbuf));

    for (i=0;i<sz;i++)
    {
        Serial.print(packetbuf[i], HEX);
        Serial.print(" ");
    }
    Serial.println("");

    if (0 != (rc = coap_parse(&pkt, packetbuf, sz)))
    {
        Serial.print("Bad packet rc=");
        Serial.println(rc, DEC);
    }
    else
    {
        size_t rsplen = sizeof(packetbuf);
        coap_packet_t rsppkt;
        coap_handle_req(&scratch_buf, &pkt, &rsppkt);

        memset(packetbuf, 0, UDP_TX_PACKET_MAX_SIZE);
        if (0 != (rc = coap_build(packetbuf, &rsplen, &rsppkt)))
        {
            Serial.print("coap_build failed rc=");
            Serial.println(rc, DEC);
        }
        else
        {
            udp_send(packetbuf, rsplen);
        }
    }
}

}

It is working great to connect WIFI like this:
Initializing...

Attempting to connect to ASUS
Started AP/SSID scan
Connecting to ASUS...Waiting to connect...Connected!
Request DHCP

IP Addr: 192.168.50.141
Netmask: 255.255.255.0
Gateway: 192.168.50.1
DHCPsrv: 192.168.50.1
DNSserv: 192.168.50.1

BUT it does not work when i use libcoap client or firefox cooper.
always timeout
please help!
thanks