Hello,
I am new to programming microcontrollers and looking for a little guidance. The basic project includes an Android App connected to a wireless router via wireless and a microcontroller connected to the router via Cat5. The microcontroller I am using is the Arduino Ethernet. I am looking to receive the value "1" from the Android App to make pin 9 high; receive value "2" to make pin 7 high; or receive value "3" to make pin 6 high. The pins will be digital out and be used further in our rf circuits; for now they will light up LEDs. Through a little research, I have gathered the code below. I am thinking that UDP is the way to go. Will this also mean that the Android App will have to send UDP as well? Any help or direction is greatly appreciated. Thank you
Michael
#include <SPI.h>
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <SoftwareSerial.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0x90, 0xA2, 0xDA, 0x0D, 0x59, 0x0E };
IPAddress ip(192, 168, 1, 106);
unsigned int localPort = 445; // local port to listen on
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet,
char ReplyBuffer[] = "acknowledged"; // a string to send back
int led_yellow= 9;
int led_red = 7;
int led_green= 6;
// An EthernetUDP instance to let us send and receive packets over UDP
EthernetUDP Udp;
void setup() {
// start the Ethernet and UDP:
Ethernet.begin(mac,ip);
Udp.begin(localPort);
pinMode(led_yellow, OUTPUT);
pinMode(led_red, OUTPUT);
pinMode(led_green, OUTPUT);
Serial.begin(9600);
}
// the loop routine runs over and over again forever:
void loop() {
// if there's data available, read a packet
int packetSize = Udp.parsePacket();
Serial.println(packetSize);
if(packetSize)
{
Serial.print("Received packet of size ");
Serial.println(packetSize);
Serial.print("From ");
IPAddress remote = Udp.remoteIP();
for (int i =0; i < 4; i++)
{
Serial.print(remote[i], DEC);
if (i < 3)
{
Serial.print(".");
}
}
Serial.print(", port ");
Serial.println(Udp.remotePort());
// read the packet into packetBufffer
Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE);
Serial.println("Contents:");
Serial.println(packetBuffer);
Serial.println(packetBuffer[0]);
if(packetBuffer[0]=='1'){
digitalWrite(led_yellow,HIGH);
delay(1000);
digitalWrite(led_yellow,LOW);
//}
else(packetBuffer[0]=='2'){
digitalWrite(led_red,HIGH);
delay(1000);
digitalWrite(led_red,LOW);
//}
else if(packetBuffer[0]=='3'){
digitalWrite(led_green,HIGH);
delay(1000);
digitalWrite(led_green,LOW);
//}
}
}
}