Hi, I read all the different articles and form for the udp but still can get one set up, can someone help break it down for my and how to read it. using this and trying to just get some udp set up from Electron
How do you post the electron ip address, how often does it change if mounted to a Car ? Any Help would be great.
/*
******************************************************************************
Copyright (c) 2017 Particle Industries, Inc. All rights reserved.
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, either
version 3 of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this program; if not, see <http://www.gnu.org/licenses/>.
******************************************************************************
*/
#include "application.h"
#define PARTICLE_CLOUD 1
Serial1LogHandler logHandler(115200, LOG_LEVEL_ALL);
// UDP Port used for two way communication
unsigned int localPort1 = 5000;
unsigned int localPort2 = 6000;
IPAddress serverIPaddress(1**,1**,0,157 ); // IMPORTANT SET THIS!!! (is this my Comp or Electron ) ?
// An UDP instance to let us send and receive packets over UDP
UDP Udp1;
UDP Udp2;
SYSTEM_MODE(SEMI_AUTOMATIC);
void callHomeUDP1()
{
static String msg;
static int count = 1;
LOG(INFO,"UDP 1 HELLO SENDING\r\n");
Udp1.beginPacket(serverIPaddress, localPort1);
msg = "hello from electron " + String(count++);
Udp1.write(msg);
Udp1.endPacket();
LOG(INFO,"UDP 1 HELLO SENT\r\n");
}
void checkUDP1() {
// Check if data has been received
if (Udp1.parsePacket() > 0) {
// Read first char of data received
char c = Udp1.read();
String temp = String(c);
// Ignore other chars
while(Udp1.available()) {
c = Udp1.read();
temp.concat(String(c));
}
// Store sender ip and port
IPAddress ipAddress = Udp1.remoteIP();
int port = Udp1.remotePort();
// Log complete message
LOG(INFO,"UDP 1 MESSAGE FROM %s:%d \"%s\"", String(ipAddress).c_str(), port, temp.c_str());
// Echo back data to sender
// Udp1.beginPacket(ipAddress, port);
// Udp1.write(temp.c_str());
// Udp1.endPacket();
}
}
void callHomeUDP2()
{
static String msg;
static int count = 1;
LOG(INFO,"UDP 2 HELLO SENDING\r\n");
Udp2.beginPacket(serverIPaddress, localPort2);
msg = "hello from electron " + String(count++);
Udp2.write(msg);
Udp2.endPacket();
LOG(INFO,"UDP 2 HELLO SENT\r\n");
}
void checkUDP2() {
// Check if data has been received
if (Udp2.parsePacket() > 0) {
// Read first char of data received
char c = Udp2.read();
String temp = String(c);
// Ignore other chars
while(Udp2.available()) {
c = Udp2.read();
temp.concat(String(c));
}
// Store sender ip and port
IPAddress ipAddress = Udp2.remoteIP();
int port = Udp2.remotePort();
// Log complete message
LOG(INFO,"UDP 2 MESSAGE FROM %s:%d \"%s\"", String(ipAddress).c_str(), port, temp.c_str());
// Echo back data to sender
// Udp2.beginPacket(ipAddress, port);
// Udp2.write(temp.c_str());
// Udp2.endPacket();
}
}
void setup()
{
Cellular.on();
Cellular.connect();
waitUntil(Cellular.ready);
pinMode(B0, INPUT_PULLUP);
pinMode(B1, INPUT_PULLUP);
Udp1.begin(localPort1);
callHomeUDP1();
#if PARTICLE_CLOUD
Particle.connect();
waitUntil(Particle.connected);
#endif
Udp2.begin(localPort2);
callHomeUDP2();
}
void loop()
{
if (digitalRead(B1)==LOW) {
callHomeUDP2();
delay(1000);
}
if (digitalRead(B0)==LOW) {
callHomeUDP1();
delay(1000);
}
checkUDP1();
checkUDP2();
}