Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 3 of 3

Thread: Teensy 3.2 + Piezo Sensors + WIZ850io Ethernet = A Bad Time?

  1. #1
    Junior Member
    Join Date
    Aug 2018
    Posts
    2

    Teensy 3.2 + Piezo Sensors + WIZ850io Ethernet = A Bad Time?

    Hello,

    I'm trying to create a simple circuit with piezo sensors, using thresholding to detect when a "knock" is generated (very close to the "Knock" example code from Arduino). This is working fine on its own, but as soon as I try to incorporate Ethernet into the code (the goal is to be able to transmit when a knock is registered), the code refuses to loop.

    Specifically, after the "Ethernet begins" print I get no more information via the serial monitor, nor do I get any UDP packets sent as expected. I've narrowed the problem down to the Udp.endPacket() line. The program gets stuck right there and refuses to continue:

    Code:
    // these constants won't change:
    const int knockSensor = A8; // the piezo is connected to analog pin 8
    const int potentiometer = A9;
    int threshold = 100;  // threshold value to decide when the detected sound is a knock or not
    
    
    // these variables will change:
    int sensorReading = 0;      // variable to store the value read from the sensor pin
    int highestReading = 0;
    int lowestReading = 1000;
    int difference = 0;
    int potReading = 0;
    
    elapsedMillis timeSinceLastInterval;
    
    // ethernet
    #include <Ethernet.h>
    #include <EthernetUdp.h>
    
    byte myMac[] = {0x98, 0x76, 0xB6, 0x10, 0x61, 0x0A};
    IPAddress myIP(192, 168, 1, 81);
    
    IPAddress myDNS(192, 168, 1, 1); 
    IPAddress myGW(192, 168, 1, 1); 
    
    IPAddress myMask(255, 255, 255, 0); 
    unsigned int localPort = 8888;      // local port to listen on
    
    IPAddress destIP(192, 168, 1, 103);
    
    EthernetUDP Udp;
    
    void setup() {
      Serial.begin(9600);       // use the serial port
    
      //ethernet
      Ethernet.begin(myMac, myIP, myDNS, myGW, myMask);
      Udp.begin(localPort);
    
      Serial.print("Ethernet begins");
    }
    
    void loop() {
      // read the sensor and store it in the variable sensorReading:
      sensorReading = analogRead(knockSensor);
      if(sensorReading > highestReading){
        highestReading = sensorReading;
      }
      if(sensorReading < lowestReading){
        lowestReading = sensorReading;
      }
    
      potReading = analogRead(potentiometer);
      threshold = potReading/8;
    
      if(timeSinceLastInterval >= 50){
        difference = highestReading-lowestReading;
        Serial.println(difference);
        if(difference > threshold){
          Serial.println("KNOCK!");
        }
        highestReading = 0;
        lowestReading = 1000;
        timeSinceLastInterval = 0;
      }
    
      Udp.beginPacket(destIP, 7778); // used by comp
      Udp.print("testing");
      Udp.endPacket();
    
    }
    I have my computer and the Teensy hooked up to a small switch and have given them the static IPs and such. I've double-checked my wiring with the ethernet adapter and can't find any glaring issues. I've done this before with buttons and the like and have had success so I'm not sure what's so different about this time unless I made a silly error that is eluding me.

    Any ideas?

    Thanks in advance for the help.

  2. #2
    Senior Member+ manitou's Avatar
    Join Date
    Jan 2013
    Posts
    1,642
    maybe add a delay(100); and some informational Serial.print before the Udp.beginPacket() in loop(). Can you ping 192.168.1.81 when your sketch is running? Check the return value of Ethernet.begin().

    Do you have other working Ethernet/UDP sketches? Check your wiring. Try one of the Ethernet lib UDP examples like UDPSendReceiveString.

    EDIT: For what it's worth, your sketch works for me. Using WIZ850io (pins 9-13, RST, SPI pins) on my local ethernet (changed mac and IP's for my local net). I have a UDP receiver listening on 7778 that prints "testing" using a delay(2000);, and I have nothing connected to the analog pins, so the analogRead() returns random numbers.

    a python UDP receive/print
    Code:
    import sys
    from socket import *
    
    UPORT = 7778
    BUFSIZE = 1024
    
    def server():
        port = UPORT
        s = socket(AF_INET, SOCK_DGRAM)
        s.bind(('', port))
        print 'udp echo server ready on port ', UPORT
        while 1:
            data, addr = s.recvfrom(BUFSIZE)
            print 'server received %r from %r' % (data, addr)
    
    server()
    Last edited by manitou; 12-04-2018 at 10:37 PM.

  3. #3
    Junior Member
    Join Date
    Aug 2018
    Posts
    2
    Hey, thanks for the input - figured out today I had the ethernet port hooked up in reverse (despite checking it several times). D'oh!

    All is good!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •