Author Topic: Getting started with sending UDP  (Read 3003 times)

spiderbike

  • Newbie
  • *
  • Posts: 5
Getting started with sending UDP
« on: March 02, 2016, 12:17:41 pm »
has anyone got some sample code of getting started with the OAK, to send UDP over the WiFi module?

peabo

  • Newbie
  • *
  • Posts: 8
Re: Getting started with sending UDP
« Reply #1 on: March 02, 2016, 03:16:20 pm »
Here you go.
I used this adapted blink program to see if a new sketch had successfully been uploaded OTA:-

Code: [Select]

/*
 variable LED BLINKER
 01/03/2016
 */



void setup() {
  // initialize digital pin 1 as an output.
  pinMode(1, OUTPUT);
}

// the loop function runs over and over again forever
void loop() {

int totaltime = 170;
int starttime = 90;


  for (int j=starttime; j<=totaltime; j=((j+2))) {     // Start our for loop
                                      digitalWrite(1,HIGH); //Turn  LED on
                                      delay(totaltime-j);             
                                      digitalWrite(1,LOW);  //Turn  LED off
                                      delay(totaltime-j);           
                                      }
     
  for (int j=totaltime; j>=starttime; j=((j-2))) {     // Start our for loop
                                      digitalWrite(1,HIGH); //Turn  LED on
                                      delay(totaltime-j);             
                                      digitalWrite(1,LOW);  //Turn LED off
                                      delay(totaltime-j);           
                                      }
     
     
     
}




Cheers


Dan

spiderbike

  • Newbie
  • *
  • Posts: 5
Re: Getting started with sending UDP
« Reply #2 on: March 05, 2016, 08:14:55 am »
Hey Dan,

thanks for the code sample, not really what I was after, but I managed to work it out in the end, I have shared the code below in case it helps others, it seems to be working fine, it connects to a little board I built that sends a pulse to one of the I/O pins every time the light flashes on our solar panel generation meter, and then sends it as a hit via UDP to our Graphite server

Code: [Select]
#include <WiFiClient.h>
#include <WiFiServer.h>
#include <WiFiUdp.h>
#include <ESP8266WiFi.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>

int status = WL_IDLE_STATUS;     // the Wifi radio's status
bool lastSensorStateOn = false;
WiFiUDP Udp;


void printWifiData() {
  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
    Serial.print("IP Address: ");
  Serial.println(ip);
  Serial.println(ip);
 
  // print your MAC address:
  byte mac[6]; 
  WiFi.macAddress(mac);
  Serial.print("MAC address: ");
  Serial.print(mac[5],HEX);
  Serial.print(":");
  Serial.print(mac[4],HEX);
  Serial.print(":");
  Serial.print(mac[3],HEX);
  Serial.print(":");
  Serial.print(mac[2],HEX);
  Serial.print(":");
  Serial.print(mac[1],HEX);
  Serial.print(":");
  Serial.println(mac[0],HEX);
 
}

void printCurrentNet() {
  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print the received signal strength:
  long rssi = WiFi.RSSI();
  Serial.print("signal strength (RSSI):");
  Serial.println(rssi);


}


void setup() {
  //Initialize serial and wait for port to open:
  Serial.begin(115200);
  while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }

  pinMode(1, OUTPUT);  //LED
  pinMode(8, INPUT);   //Input for light reader
 
 // attempt to connect to Wifi network:
  while ( WiFi.status() != WL_CONNECTED) {
    status = WiFi.begin();
    Serial.print("Attempting a connect to WIFI: ");
    Serial.println(status);

    // wait 10 seconds for connection:
    delay(10000);
  }
   
  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();

}

void loop() {  digitalWrite(1,LOW); //Turn  LED on
  if (digitalRead(8) == HIGH)
  {
      if (!lastSensorStateOn)
      {
          lastSensorStateOn = true;
          // turn on the LED
          digitalWrite(1,HIGH); //Turn  LED on
          LogStat();
      }
  }
  else
  {
      lastSensorStateOn = false;
  }
  delay(20);
}


void LogStat()
  {
     /* NetworkInterface niInterface = new NetworkInterface();
      niInterface.Port = 8125;
      niInterface.Ip = "10.0.50.93";
      niInterface.SendMessage("SolarKWGenerated.count:1|c");
      */

 
    IPAddress ip = IPAddress(10,0,50,93);
    Udp.beginPacket(ip, 8125);
    Udp.write("SolarKWGenerated.count:1|c");
    Udp.endPacket();
    Serial.println("Sent to Graphite 1");
  }