Go Down

Topic: Ethernet shield and hard serial conflict in Arduino Mega 2560. (Read 377 times) previous topic - next topic

DarkerInMind

So I'm using Arduino Mega 2560 with Ethernet Shield connected to a router and module Sim800 to receive call (and end it). But somehow this code doesn't work when I send Call command from PC:


Code: [Select]
#include <SPI.h>         // needed for Arduino versions later than 0018
#include <Ethernet.h>
#include <EthernetUdp.h>
#include <String.h>
char data[3];
String command;
String senddata;
byte mac[] = {0x90, 0xA2, 0xEA, 0x01, 0x97, 0x10};
IPAddress ip(10, 0, 0, 4);
IPAddress myDns(10, 0, 0, 1);
IPAddress gateway(10, 0, 0, 1);
IPAddress subnet(255, 0, 0, 0);
unsigned int localPort = 8888;   // local port to listen on
unsigned int sendPort = 12800;
// buffers for receiving and sending data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE];  //buffer to hold incoming packet,
EthernetUDP Udp;

void setup() {
  Serial.begin(9600);
  while(!Serial);
  Serial1.begin(9600);
  while(!Serial1);
  Ethernet.begin(mac, ip, myDns, gateway, subnet);
  Udp.begin(localPort);
  Serial.println("Setup Complete!");
  delay(1000);
}

void loop() {
  // if there's data available, read a packet
  int packetSize = Udp.parsePacket();
  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);
    command = packetBuffer;
    if(command == "Cal"){
        for (int i = 0; i < 40000; i++){
              char t[4];
              String c;
              if(Serial1.available()){
              c = Serial1.readString();
              Serial.println(c);
              t[0] = c[2];
              if (t[0] == 'R'){
              Serial1.println("ATH");
              Udp.beginPacket(Udp.remoteIP(), sendPort);
              Udp.write("SUCCESS");
              Udp.endPacket();
              i = 39999;
              }
           }
           else if (i == 39998) {
              Udp.beginPacket(Udp.remoteIP(), sendPort);
              Udp.write("FAIL");
              Udp.endPacket();
           }
           delay(1);
        }
    }
    if(command == "Chk"){
    Udp.beginPacket(Udp.remoteIP(), sendPort);
    Udp.write("OK");
    Udp.endPacket();
    }
  }
}


I can see the Cal command sent to Arduino. But when I made call it doesn't hang up or print out anything to Serial Monitor.

Go Up