Voltage reading problems

Having the laptop connected to the ESP board via USB while it is charging pulls the voltage reading down, thus switching the relay off at high voltages. This means suboptimal charging opportunities while I experiment with the solar management system.

A solution is to do away with the USB connection by utilising the WiFi capabilities of the ESP board.

The library to use is ArduinoOTA.h (OTA = Over The Air):

#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
const char *ssid = " "; // Not telling you what it is
const char *password = " "; // Not telling you what this is either
unsigned long previousMillis = 0;
const long interval = 60000;
void setup() {
  Serial.begin(230400);
  delay(10);
  Serial.println('\n');
  WiFi.softAP(ssid, password);
  Serial.print("Access Point \"");
  Serial.print(ssid);
  Serial.println("\" started");
  Serial.print("IP address:\t");
  Serial.println(WiFi.softAPIP());
  ArduinoOTA.onStart([]() {
    Serial.println("Start");
  });
  ArduinoOTA.onEnd([]() {
    Serial.println("\nEnd");
  });
  ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
    Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
  });
  ArduinoOTA.onError([](ota_error_t error) {
    Serial.printf("Error[%u]: ", error);
    if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
    else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
    else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
    else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
    else if (error == OTA_END_ERROR) Serial.println("End Failed");
  });
  ArduinoOTA.begin();
  Serial.println("Ready");
  Serial.print("IP address: ");
  Serial.println(WiFi.localIP());
  pinMode(D1, OUTPUT);
}
void loop() {
  ArduinoOTA.handle();
  unsigned long currentMillis = millis();
  if (currentMillis - previousMillis >= interval) {
    previousMillis = currentMillis;
    const int a = A0;
    float b = 0.8;
    float c = 0.0;
    int d = 0;
    int e = 0;
    for (int i = 0; i < 10; i++) {
      d = analogRead(a);
      e = d + e;
      Serial.println(d);
      delay(500);
    }
    c = (e * 2 * b) / 1023.0;
    if (c <= 12.70) {
      digitalWrite(D1, 0);
    } else {
      if (c >= 13.00) {
        digitalWrite(D1, 1);
      }
    }
    Serial.print("Average A0 input from 10 samples over 0.5s = ");
    Serial.print(c);
    Serial.println("V");
    Serial.print("Average digital output from 10 samples over 0.5s = ");
    Serial.println(e / 10);
  }
}

Eagle-eyed readers will notice that the delay() function used for delaying each set of voltage readings has been removed. A delay of more than 1000ms effectively disables ArduinoOTA.handle() because the ESP board is paused during that time. In its place the millis() function is used. This counts the number of loops and implements the voltage reading code only when the required number of loops has been reached. Therefore no pause and ArduinoOTA.handle() is kept alive.

Neat!

However, the Serial Monitor in the Arduino IDE only works via USB (the clue is in the the word “Serial”).

To enable monitoring OTA I need to do some reading: http://arduino.esp8266.com/Arduino/versions/2.0.0/doc/ota_updates/ota_updates.html

In the meantime here’s a photo of Okonomiyaki: