Skip to content

Commit

Permalink
UDP support (experimental)
Browse files Browse the repository at this point in the history
  • Loading branch information
bportaluri committed Jan 3, 2016
1 parent 5df484c commit ebd5dfc
Show file tree
Hide file tree
Showing 6 changed files with 438 additions and 4 deletions.
144 changes: 144 additions & 0 deletions examples/WebGetTime/WebGetTime.ino
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
/*
WiFiEsp example: WebClient
This sketch connects to google website using an ESP8266 module to
perform a simple web search.
For more details see: http://yaab-arduino.blogspot.com/p/wifiesp-example-client.html
*/

#include "WiFiEsp.h"
#include "WiFiEspUdp.h"

// Emulate Serial1 on pins 6/7 if not present
#ifndef HAVE_HWSERIAL1
#include "SoftwareSerial.h"
SoftwareSerial Serial1(6, 7); // RX, TX
#endif

char ssid[] = "WTwim"; // your network SSID (name)
char pass[] = "tiv05tiv"; // your network password
int status = WL_IDLE_STATUS; // the Wifi radio's status

unsigned int localPort = 2390; // local port to listen for UDP packets

IPAddress timeServer(129, 6, 15, 28); // time.nist.gov NTP server

const int NTP_PACKET_SIZE = 48; // NTP time stamp is in the first 48 bytes of the message

byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets

// A UDP instance to let us send and receive packets over UDP
WiFiEspUDP Udp;

void setup()
{
// initialize serial for debugging
Serial.begin(115200);
// initialize serial for ESP module
Serial1.begin(9600);
// initialize ESP module
WiFi.init(&Serial1);

// check for the presence of the shield
if (WiFi.status() == WL_NO_SHIELD) {
Serial.println("WiFi shield not present");
// don't continue
while (true);
}

// attempt to connect to WiFi network
while ( status != WL_CONNECTED) {
Serial.print("Attempting to connect to WPA SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network
status = WiFi.begin(ssid, pass);
}

// you're connected now, so print out the data
Serial.println("You're connected to the network");

Udp.begin(localPort);
}

void loop() {
sendNTPpacket(timeServer); // send an NTP packet to a time server
// wait to see if a reply is available
delay(1000);
Serial.println(Udp.parsePacket());
if (Udp.parsePacket()) {
Serial.println("packet received");
// We've received a packet, read the data from it
Udp.read(packetBuffer, NTP_PACKET_SIZE); // read the packet into the buffer

//the timestamp starts at byte 40 of the received packet and is four bytes,
// or two words, long. First, esxtract the two words:

unsigned long highWord = word(packetBuffer[40], packetBuffer[41]);
unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]);
// combine the four bytes (two words) into a long integer
// this is NTP time (seconds since Jan 1 1900):
unsigned long secsSince1900 = highWord << 16 | lowWord;
Serial.print("Seconds since Jan 1 1900 = ");
Serial.println(secsSince1900);

// now convert NTP time into everyday time:
Serial.print("Unix time = ");
// Unix time starts on Jan 1 1970. In seconds, that's 2208988800:
const unsigned long seventyYears = 2208988800UL;
// subtract seventy years:
unsigned long epoch = secsSince1900 - seventyYears;
// print Unix time:
Serial.println(epoch);


// print the hour, minute and second:
Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT)
Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day)
Serial.print(':');
if (((epoch % 3600) / 60) < 10) {
// In the first 10 minutes of each hour, we'll want a leading '0'
Serial.print('0');
}
Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute)
Serial.print(':');
if ((epoch % 60) < 10) {
// In the first 10 seconds of each minute, we'll want a leading '0'
Serial.print('0');
}
Serial.println(epoch % 60); // print the second
}
// wait ten seconds before asking for the time again
delay(10000);
}

// send an NTP request to the time server at the given address
unsigned long sendNTPpacket(IPAddress& address) {
//Serial.println("1");
// set all bytes in the buffer to 0
memset(packetBuffer, 0, NTP_PACKET_SIZE);
// Initialize values needed to form NTP request
// (see URL above for details on the packets)
//Serial.println("2");
packetBuffer[0] = 0b11100011; // LI, Version, Mode
packetBuffer[1] = 0; // Stratum, or type of clock
packetBuffer[2] = 6; // Polling Interval
packetBuffer[3] = 0xEC; // Peer Clock Precision
// 8 bytes of zero for Root Delay & Root Dispersion
packetBuffer[12] = 49;
packetBuffer[13] = 0x4E;
packetBuffer[14] = 49;
packetBuffer[15] = 52;

//Serial.println("3");

// all NTP fields have been given values, now
// you can send a packet requesting a timestamp:
Udp.beginPacket(address, 123); //NTP requests are to port 123
//Serial.println("4");
Udp.write(packetBuffer, NTP_PACKET_SIZE);
//Serial.println("5");
Udp.endPacket();
//Serial.println("6");
}

4 changes: 3 additions & 1 deletion src/WiFiEsp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ along with The Arduino WiFiEsp library. If not, see


int16_t WiFiEspClass::_state[MAX_SOCK_NUM] = { NA_STATE, NA_STATE, NA_STATE, NA_STATE };
uint16_t WiFiEspClass::_server_port[MAX_SOCK_NUM] = { 0, 0, 0, 0 };

uint8_t WiFiEspClass::espMode=0;

uint8_t WiFiEspClass::espMode = 0;


WiFiEspClass::WiFiEspClass()
Expand Down
1 change: 1 addition & 0 deletions src/WiFiEsp.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class WiFiEspClass
public:

static int16_t _state[MAX_SOCK_NUM];
static uint16_t _server_port[MAX_SOCK_NUM];

WiFiEspClass();

Expand Down
198 changes: 198 additions & 0 deletions src/WiFiEspUdp.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,198 @@

/*
#include <string.h>
#include "utility/server_drv.h"
#include "utility/wifi_drv.h"
#include "WiFi.h"
#include "WiFiUdp.h"
#include "WiFiClient.h"
#include "WiFiServer.h"
*/

#include "WiFiEsp.h"
#include "WiFiEspUdp.h"

#include "utility/EspDrv.h"
#include "utility/debug.h"

/* Constructor */
WiFiEspUDP::WiFiEspUDP() : _sock(NO_SOCKET_AVAIL) {}

/* Start WiFiUDP socket, listening at local port PORT */

uint8_t WiFiEspUDP::begin(uint16_t port)
{
uint8_t sock = getFirstSocket();
if (sock != NO_SOCKET_AVAIL)
{
//EspDrv::startClient(host, port, _sock)
//ServerDrv::startServer(port, sock, UDP_MODE);
WiFiEspClass::_server_port[sock] = port;
_sock = sock;
_port = port;
return 1;
}
return 0;

}


/* return number of bytes available in the current packet,
will return zero if parsePacket hasn't been called yet */
int WiFiEspUDP::available() {
if (_sock != NO_SOCKET_AVAIL)
{
int bytes = EspDrv::availData(_sock);
if (bytes>0)
{
return bytes;
}
}

return 0;
}

/* Release any resources being used by this WiFiUDP instance */
void WiFiEspUDP::stop()
{
if (_sock == NO_SOCKET_AVAIL)
return;

//ServerDrv::stopClient(_sock);

_sock = NO_SOCKET_AVAIL;
}

int WiFiEspUDP::beginPacket(const char *host, uint16_t port)
{
if (_sock == NO_SOCKET_AVAIL)
_sock = getFirstSocket();
if (_sock != NO_SOCKET_AVAIL)
{
EspDrv::startClient(host, port, _sock, UDP_MODE);
WiFiEspClass::_state[_sock] = _sock;
return 1;
}
return 0;
}


int WiFiEspUDP::beginPacket(IPAddress ip, uint16_t port)
{
char s[18];
sprintf(s, "%d.%d.%d.%d", ip[0], ip[1], ip[2], ip[3]);

return beginPacket(s, port);
}


int WiFiEspUDP::endPacket()
{
return 1; //ServerDrv::sendUdpData(_sock);
}

size_t WiFiEspUDP::write(uint8_t byte)
{
return write(&byte, 1);
}

size_t WiFiEspUDP::write(const uint8_t *buffer, size_t size)
{
bool r = EspDrv::sendData(_sock, buffer, size);
if (!r)
{
return 0;
}

return size;
}

int WiFiEspUDP::parsePacket()
{
return available();
}

int WiFiEspUDP::read()
{
uint8_t b;
if (!available())
return -1;

bool connClose = false;
EspDrv::getData(_sock, &b, false, &connClose);

return b;
}

int WiFiEspUDP::read(unsigned char* buffer, size_t len)
{
int bytes = available();

if(bytes==-1)
return -1;

if (bytes!=len)
LOGWARN1(bytes, len);


uint16_t size = 0;
if (!EspDrv::getDataBuf(_sock, buffer, &size))
return -1;
return size;
}

int WiFiEspUDP::peek()
{
uint8_t b;
if (!available())
return -1;

//ServerDrv::getData(_sock, &b, 1);
return b;
}

void WiFiEspUDP::flush()
{
// TODO: a real check to ensure transmission has been completed
}

IPAddress WiFiEspUDP::remoteIP()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};

//WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
IPAddress ip(_remoteIp);
return ip;
}

uint16_t WiFiEspUDP::remotePort()
{
uint8_t _remoteIp[4] = {0};
uint8_t _remotePort[2] = {0};

// WiFiDrv::getRemoteData(_sock, _remoteIp, _remotePort);
uint16_t port = (_remotePort[0]<<8)+_remotePort[1];
return port;
}


////////////////////////////////////////////////////////////////////////////////
// Private Methods
////////////////////////////////////////////////////////////////////////////////

// TODO remove duplication with WiFiEspClient::getFirstSocket()

uint8_t WiFiEspUDP::getFirstSocket()
{
for (int i = 0; i < MAX_SOCK_NUM; i++)
{
if (WiFiEspClass::_state[i] == NA_STATE)
{
return i;
}
}
return SOCK_NOT_AVAIL;
}

Loading

0 comments on commit ebd5dfc

Please sign in to comment.