Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By Vulcnar
#52480 Using Arduino 1.6.7

I am trying to make a version of an amazon dash button that will tell me how many button presses there have been. The hardware consists of some transistors and a button. Basically one button press will pull reset low, then the ESP kicks on a transistor to keep subsequent button presses from pulling reset low again (a little more complicated, but that is the basic idea). The ESP will count the button presses, then connects to the wifi network to send a UDP packet with the number of button presses. It seems to work with button presses for the first 1.5 - 2 seconds but any more and it won't connect to the wifi. I used some Serial outputs to watch it try to connect, but it times out. I'm trying to debug it, but thought maybe someone here might know better.
I can consistently get 1-4 button presses (with button press one being the reset pulse), but getting to 6 or above seems to cause wifi connection issues.

Code: Select all#include <ESP8266WiFi.h>
#include <WiFiUDP.h>
#include <user_interface.h>
WiFiUDP udp;
os_timer_t mytimer;
os_timer_t latchtime;
int count=1;
int timeout=0;
bool latch=true;
bool endr=false;
unsigned int udpport = 2390;
IPAddress broadcastIP;
IPAddress broadcastIP2(255, 255, 255, 255);

void timerCallback(void *pArg) {
  endr=true;
}

void latchrelease(void *pArg){
  latch=false;
}

void setup() {
  pinMode(5, OUTPUT);
  digitalWrite(5,HIGH);
  pinMode(4, INPUT);
  os_timer_setfn(&mytimer, timerCallback, NULL);
  os_timer_setfn(&latchtime, latchrelease, NULL);
  os_timer_arm(&mytimer,2000,false);
}


void loop() {
  yield();
  if(digitalRead(4)==HIGH and latch==false){
    latch=true;
    count+=1;
    yield();
    os_timer_arm(&mytimer,2000,false);
  }
  yield();
  if(digitalRead(4)==LOW and latch==true){
    os_timer_arm(&latchtime,40,false);
  }
  yield();
  if(endr==true){
    yield();
    WiFi.begin("ssid","password");
    yield();
    while(WiFi.status() != WL_CONNECTED){
      delay(500);
      yield();
      timeout+=1;
      if(timeout==12){
        ESP.deepSleep(0, WAKE_RF_DEFAULT);
        delay(500);
      }
    }
    IPAddress ip = WiFi.localIP();
    broadcastIP = ~WiFi.subnetMask()|WiFi.gatewayIP();
    udp.begin(udpport);
    char  ReplyBuffer[5];
    String pass="";
    pass+=count;
    pass.toCharArray(ReplyBuffer,5);
    udp.beginPacket(broadcastIP,udpport);
    udp.write(ReplyBuffer);
    udp.endPacket();
    udp.beginPacket(broadcastIP2,udpport);
    udp.write(ReplyBuffer);
    udp.endPacket();
    delay(500);
    digitalWrite(5, LOW);
    ESP.deepSleep(0, WAKE_RF_DEFAULT);
    delay(500);
  }
}
Last edited by Vulcnar on Tue Aug 09, 2016 9:49 am, edited 1 time in total.
User avatar
By Vulcnar
#52557 Ok, in case there are any curious lurkers out there, I solved this issue. Turns out that the WiFi was automatically connecting in the background even before I had called the WiFi.begin() method. Somehow since it was already connected, calling the WiFi.begin() method and waiting for it to connect stuck it in an endless loop where WL_CONNECTED would not return true. I might add that this was despite having a call to WiFi.persistence(false) when i suspected it might be auto connecting.
To solve the issue I simply added a check to WiFi.status() before calling WiFi.begin(). So now everything works fine.