top of page

debugging!!!!

hello tom


This week I've been building my enclosure and proto boards for the voting machine.




Power

  • I threw a 9V power source into the 3.3V power line and killed the WiFi board on an Arduino -cry-

  • Marlon gave me a really nifty power supply that has a variable voltage and a bunch of different tips! I switched this between 5V and 3.3V and couldn't figure out how to power the Arduino through my proto board. I talked with Josh and he suggested I try wiring the power supply to Vin instead of 5V, and when I tried it the next day, it worked! So I had to do some resoldering and some minor adjustments. But it works!

Enclosure

  • The enclosure I designed was meant to accommodate 2 wires coming out for power, but there was a moment when I thought I might have to throw a DC barrel or a USB micro port through the hole, which wasn't big enough. I cut the walls to give it more room, and even cut a hole through the bottom to see if the power supply would fit better if the circuit was arranged that way.

  • Ultimately, I did not design enough wiggle room for my circuit. It's pretty snug, and can be tricky to install onto the bucket.

  • Overall, the enclosure design is 85% successful, it just needs to be a bit bigger. The slot I designed for the power supply ended up working out once I decided to spend more time figuring out the proper way to do it.




RTC

  • Today I added rtc functions to get the time through Wifi.

  • It worked for a minute but doesn't want to work anymore -cry-


#include "arduino_secrets.h"
#include <WiFiNINA.h>  //for uno wifi, nano 33 iot
#include <ArduinoMqttClient.h>
#include <RTCZero.h>
#include <SPI.h>
#include <WiFiUdp.h>

//create an RTC object
RTCZero rtc;

int status = WL_IDLE_STATUS;

const int GMT = -5; //change timezone as req'd. NYC = -5

//initialize WiFi connection
WiFiClient wifi;
MqttClient mqttClient(wifi);

//details for MQTT Client
char broker[] = "public.cloud.shiftr.io";
int port = 1883;
char topic[] = "conndev/fireflie";
char clientID[] = "voter02";

//variables for time tracking:
long lastTimeSent = 0;

//establish sample window
int sampleWindow = 50;

void setup() {
  // put your setup code here, to run once:
  Serial.begin(115200);


  if (!Serial) delay(3000);

  //establish MAX4466 sensor
  pinMode(A0, INPUT);
  pinMode(LED_BUILTIN, OUTPUT);

  //initialize WiFi, if not connected:
  while (WiFi.status() != WL_CONNECTED) {
    Serial.print("Connecting to ");
    Serial.println(SECRET_SSID);
    WiFi.begin(SECRET_SSID, SECRET_PASS);
    delay(2000);

  }

  while (WiFi.status() !=WL_CONNECTED){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(500);
    digitalWrite(LED_BUILTIN,LOW);
    delay (500);
  }

  //print IP address once connected:
  Serial.print("Connected. My IP address: ");
  Serial.println(WiFi.localIP());

  //set the credentials for the mqtt client:
  mqttClient.setId(clientID);
  //if you need a password:
  mqttClient.setUsernamePassword(SECRET_MQTT_USER, SECRET_MQTT_PASS);

  //try to connect to the MQTT broker once you're connected to WiFi:
  while (!connectToBroker()) {
    for(int l = 0; l < 10; l++){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(300);
    digitalWrite(LED_BUILTIN, LOW);
    delay(300);
    }
    Serial.println("Attempting to connect to broker...");
    delay(1000);
  }

  Serial.println("Connected to broker.");
  for(int l = 0; l < 3; l++){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);
    delay(100);
  }

  //start rtc
  rtc.begin();

  unsigned long epoch;
  int numberOfTries = 0, maxTries = 6;

  do{
    epoch = WiFi.getTime();
    numberOfTries++;
  }

  while((epoch == 0) && (numberOfTries < maxTries));

  if (numberOfTries == maxTries){
    Serial.println("NTC unreachable!");
    while(1); 
  } else {
    Serial.print("Epoch received:");
    Serial.println(epoch);
    rtc.setEpoch(epoch);
  }
}

void loop() {
  // put your main code here, to run repeatedly:
  //If not connected to the broker, try to connect:
  if (!mqttClient.connected()) {
    Serial.println("Reconnecting...");
    connectToBroker();
  }

  //establish math for the sound sensor
  double sound = soundDiff();
  //this is the actual reading you rwant
  double soundCalc = (sound * 3.3) / 1024;
  //debug
  // Serial.println(soundCalc);
  // delay(100);


  if (soundCalc > 1.6) {    
    //light on
    for (int l = 0; l < 2; l++){
      digitalWrite(LED_BUILTIN, HIGH);
      delay(100);
      digitalWrite(LED_BUILTIN, LOW);
      delay(100);
    }

    //format the message for JSON
    String message = "{\"SOMEONE VOTED YES!\"}";

    //start a new MQTT message on the topic:
    mqttClient.beginMessage(topic);           //start a message
    mqttClient.print(message + " on ");       //add the value:
    mqttClient.print(rtc.getMonth());
    mqttClient.print("/");
    mqttClient.print(rtc.getDay());
    mqttClient.print("/");
    mqttClient.print(rtc.getYear());
    mqttClient.print(" at ");
    mqttClient.print(rtc.getHours());
    mqttClient.print(":");
    mqttClient.print(rtc.getMinutes());
    mqttClient.print(":");
    mqttClient.println(rtc.getSeconds());
    mqttClient.endMessage();    //send the message:

    Serial.println(message);

    //Take note of the time you make your request:
    lastTimeSent = millis();

    //pause
    delay(3000);
    String reset = "{\" ~ waiting for action ~ \"}";
    mqttClient.beginMessage(topic);  //start a new message on the topic
    mqttClient.println(reset);       //add the message
    mqttClient.endMessage();         //send the message
    Serial.println(reset);

    if (!mqttClient.connected()) {
      for(int l = 0; l < 2; l++){
        digitalWrite(LED_BUILTIN, HIGH);
        delay(300);
        digitalWrite(LED_BUILTIN, LOW);
        delay(300);
      }
      Serial.println("Reconnecting...");
      connectToBroker();
    }
  } else {
    digitalWrite(LED_BUILTIN, LOW);
  }
}

boolean connectToBroker() {
  //if the MQTT client is not connected:
  if (!mqttClient.connect(broker, port)) {
    for(int l = 0; l < 2; l++){
    digitalWrite(LED_BUILTIN, HIGH);
    delay(100);
    digitalWrite(LED_BUILTIN, LOW);
    delay(100);
    }
    //print out the error message:
    Serial.print("MQTT connection failed. Error no: ");
    Serial.println(mqttClient.connectError());
    //return that you're not connected:
    return false;
  }
  mqttClient.subscribe(topic);
  //once you're connected, return:
  return true;
}

double soundDiff() {
  //record start time
  double startMillis = millis();
  int signalMax = 0;  //high peak, top range
  int signalMin = 1024;   //low peak, bottom range:
  int sample;    //sample period:
  //collect data for sample length:
  while ((millis() - startMillis) < sampleWindow) {
    sample = analogRead(A0);
    if (sample < signalMin) {
      if (sample > signalMax) {
        signalMax = sample;
      } else if (sample < signalMin) {
        signalMin = sample;
      }
    }
  }
  int peakDiff = signalMax - signalMin;
  return peakDiff;
}

6 views0 comments

Recent Posts

See All

Over spring break, we let the Arduinos quietly send data to the internet. I happened to be on the floor every day so I was monitoring my project and its environment. I was also monitoring MQTT Explore

bottom of page