tired of looking Anyone Please

Good evening all. My name is jim, im starting to try and learn coding on the arduino , but old age and lack of memory does not make it easy.
Please can someone help me?
I have made a circuit to monitor battery voltage on my car. I have used example code and even squeezed in some bits i have worked out for myself, and managed to get it to send battery voltage to thingspeak, but its early days in the learning curve and now im stuck.
Board works great when its near enough to my wifi, but when parked to far away, it continually tries to connect to my wifi, and it wont finish the program and go to sleep for x amount of time like it does after a normal connect/transmit. I know its just one or two lines of code, and your all laughing about now, but ive not been able to grasp the basics yet.
I just want to add code that allows it to try to connect a few times, or for a few seconds, and when it fails, go to sleep for x amount of time before it tries again. The whole point is to monitor for intermittent heavy power drain, and having it making endless connection attempts doesn't help. Any help would be greatly appreciated.
jim

Try here

Sorry guys, its been a really long day. dont know how to attach so …
*

ESP8266 → ThingSpeak Channel

This sketch sends the value of Analog Input (A0) to a ThingSpeak channel
using the ThingSpeak API (https://www.mathworks.com/help/thingspeak).

Requirements:

Tutorial: http://nothans.com/measure-wi-fi-signal-levels-with-the-esp8266-and-thingspeak



PROGRAM BEGINS HERE
*/
int voltrefPin=A0; //can be any analog input pin you specify
int readVal;
float V0; //V is for volts and 0 is the pin

#include <ESP8266WiFi.h>
#include “ThingSpeak.h”
#include “secrets.h”

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

char ssid = SECRET_SSID; // your network SSID (name)
char pass = SECRET_PASS; // your network password
int keyIndex = 0; // your network key index number (needed only for WEP)
WiFiClient client;

void setup() { //1 open curly at the start of commands
Serial.begin(115200);
delay(100);
pinMode(voltrefPin, INPUT);
WiFi.mode(WIFI_STA);

ThingSpeak.begin(client);
} //and 1 closed curly after all the commands

void loop() {
//put your main code here, to run repeatedly:

readVal=analogRead(voltrefPin);
V0= (19./1050.)*readVal;
Serial.print("Battery Voltage is ");
Serial.println (V0);
// Connect or reconnect to WiFi
if (WiFi.status() != WL_CONNECTED) {
Serial.print("Attempting to connect to SSID: ");
Serial.println(SECRET_SSID);
while (WiFi.status() != WL_CONNECTED) {

WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
Serial.print(".");
delay(5000);
}
Serial.println("\nConnected.");
}

// Write value to Field 1 of a ThingSpeak Channel
int httpCode = ThingSpeak.writeField(myChannelNumber, 1, (V0), myWriteAPIKey);

if (httpCode == 200) {
Serial.println(“Channel write successful.”);
}
else {
Serial.println("Problem writing to channel. HTTP error code " + String(httpCode));
}

// ESP.deepSleep(600 * 1000000);}
Serial.println(“Going to sleep now night night.”);
ESP.deepSleep(20 * 1000000);}//Used for quick testing

Can we see all your code, or is it secret?
In code tags?

sorry, tired . is this it.?

/*
   MY CAR MONITOR WITH 5 MINUTE DELAY


   ESP8266 --> ThingSpeak Channel
 
   This sketch sends the value of Analog Input (A0) to a ThingSpeak channel
    using the ThingSpeak API (https://www.mathworks.com/help/thingspeak).
 
 Requirements:
 
   * ESP8266 Wi-Fi Device
   * Arduino 1.8.8+ IDE
   * Additional Boards URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
   * Library: esp8266 by ESP8266 Community
   * Library: ThingSpeak by MathWorks
  
 Tutorial: http://nothans.com/measure-wi-fi-signal-levels-with-the-esp8266-and-thingspeak

 --------------------------------------------------------------------------------------
 --------------------------------------------------------------------------------------
                                         PROGRAM BEGINS HERE
*/
int voltrefPin=A0; //can be any analog input pin you specify
int readVal;
float V0;               //V is for volts and 0 is the pin

#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include "secrets.h"

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;

char ssid[] = SECRET_SSID;   // your network SSID (name)
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key index number (needed only for WEP)
WiFiClient  client;

void setup() {               //1 open curly at the start of commands
  Serial.begin(115200);
  delay(100);
pinMode(voltrefPin, INPUT);
  WiFi.mode(WIFI_STA);

  ThingSpeak.begin(client);  
}                            //and 1 closed curly after all the commands

void loop() {
  //put your main code here, to run repeatedly:
  
  readVal=analogRead(voltrefPin);
V0= (19./1050.)*readVal;
Serial.print("Battery Voltage is ");
Serial.println (V0);
  // Connect or reconnect to WiFi
  if (WiFi.status() != WL_CONNECTED) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    while (WiFi.status() != WL_CONNECTED) {

                     
      WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected.");
  }


                  // Write value to Field 1 of a ThingSpeak Channel
  int httpCode = ThingSpeak.writeField(myChannelNumber, 1, (V0), myWriteAPIKey);

  if (httpCode == 200) {
    Serial.println("Channel write successful.");
  }
  else {
    Serial.println("Problem writing to channel. HTTP error code " + String(httpCode));
  }

                                               
    // ESP.deepSleep(600 * 1000000);}
    Serial.println("Going to sleep now night night.");
    ESP.deepSleep(20 * 1000000);}//Used for quick testing

Yes, thank you. Hint: you could have something like this (I’m not giving you the entire thing, just a clue…)

  if (WiFi.status() != WL_CONNECTED and numberOfRetries <= MAX_RETRIES) {

Ive been making projects the old fashioned way with 555 and 741s for years, but its my first attempt at micro controllers. its not easy learning a new language at my age, especially when your not that into PCs

hondajill:
Ive been making projects the old fashioned way with 555 and 741s for years, but its my first attempt at micro controllers. its not easy learning a new language at my age, especially when your not that into PCs

…and?

Thanks, i told you c++ means about as much to me as chinglish or spanglish..until ive grasped the basics your answer dosent help me any more than you could rebuild an engine if i gave you the spanners. Sorry. It would be more helpful, and you would feel better if you gave me all of it and explained what each bit does ..that would go a long way to helping me learn.

Sorry, this is the wrong forum to ask for ready to run code, or ready to run feature enhancements. It's for help with coding. If you want it all done for you, ask a moderator to move this to the "gigs and collaborations" forum.

You said,

I have used example code and even squeezed in some bits i have worked out for myself

and I have posted a solution for you for free. So take some time with it.

Im not asking for ready to run code. There's loads i've worked out and put in already and loads more to go in that ill figure out eventually, but a bit of help to get it going wouldn't have gone a miss.

hondajill:
Im not asking for ready to run code.

You said,

you would feel better if you gave me all of it

...and I definitely did give you help!

Programming takes time. There is no way you could have properly considered my reply in the time it took you to come back with that.

Sorry, you just added more confusion. when you dont understand the syntax it means nothing

The bottom line is, very very few helpers here consider it worthwhile to help someone fix a sketch that the person don't understand at all. Mainly, because it really doesn't help you learn anything. If you're a hobbyist and you just like to tinker, then find projects that are more on your level of understanding and work your way up through them just like everyone else did. Nobody just jumped into stuff like that without some background and succeeded, except for instances of blind luck.

You can study the syntax, the language has been around since about 1970(?) and with the internet there are a plethora of tutorials and on line language references that you can access.

I dont have time to mess about. Ive got a car with a problem that means it can flatten the battery overnight thats on going. I have a wife of 54 years in hospital that needs me by here side whenever i can. I cant afford for this car to die again.I have looked around for three days now and found nothing to help me get this project up and running in a timely manner. I found and joined this forum to try and get the same kind of help i give others on a daily basis
I spend a huge amount of time on a jaguar cars forum helping common people like me find and fix the electrical problems that these cars are famous for. I do it for free without obligation to help people out of the SHxx. i dont tell them half a story just for fun or to bump up my post count.
If you wont help me sort it now then shame on you

I can’t test this because I don’t have the same hardware as you, but the connect to WiFi should be in the setup(). I have implemented Aarg’s suggestion to try five times.
At the top of the sketch:

int numberOfRetries;
const int MAX_RETRIES = 5;

In setup:

 while (WiFi.status() != WL_CONNECTED and numberOfRetries <= MAX_RETRIES) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
    Serial.print(".");
    delay(5000);
  }

And at the top of main()

 if (WiFi.status() == WL_CONNECTED) {

So, this will try to connect five times then continue to the loop(). The loop tests the connection, and if not connected, falls through to the sleep code.

Agin, this is untested and you will have to debug it. It would help tremendously if you use CTRL-T to format the code in the IDE. If you have mismatched braces (you don’t), CTRL-T will highlight them quickly. And it makes the code easier to read.

Also, give pins names, not numbers. It makes the code much, much more readable. (And portable if you want to use a different processor board in the future).

/*
   MY CAR MONITOR WITH 5 MINUTE DELAY


   ESP8266 --> ThingSpeak Channel

   This sketch sends the value of Analog Input (A0) to a ThingSpeak channel
    using the ThingSpeak API (https://www.mathworks.com/help/thingspeak).

  Requirements:

     ESP8266 Wi-Fi Device
     Arduino 1.8.8+ IDE
     Additional Boards URL: http://arduino.esp8266.com/stable/package_esp8266com_index.json
     Library: esp8266 by ESP8266 Community
     Library: ThingSpeak by MathWorks

  Tutorial: http://nothans.com/measure-wi-fi-signal-levels-with-the-esp8266-and-thingspeak

  --------------------------------------------------------------------------------------
  --------------------------------------------------------------------------------------
                                         PROGRAM BEGINS HERE
*/
int voltrefPin = A0; //can be any analog input pin you specify
int readVal;
float V0;               //V is for volts and 0 is the pin

#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
#include "secrets.h"

unsigned long myChannelNumber = SECRET_CH_ID;
const char * myWriteAPIKey = SECRET_WRITE_APIKEY;
int numberOfRetries;
const int MAX_RETRIES = 5;

char ssid[] = SECRET_SSID;   // your network SSID (name)
char pass[] = SECRET_PASS;   // your network password
int keyIndex = 0;            // your network key index number (needed only for WEP)
WiFiClient  client;

void setup() {               //1 open curly at the start of commands
  Serial.begin(115200);
  delay(100);
  pinMode(voltrefPin, INPUT);
  WiFi.mode(WIFI_STA);

  ThingSpeak.begin(client);


  while (WiFi.status() != WL_CONNECTED and numberOfRetries <= MAX_RETRIES) {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(SECRET_SSID);
    WiFi.begin(ssid, pass); // Connect to WPA/WPA2 network. Change this line if using open or WEP network
    Serial.print(".");
    delay(5000);
  }
}                            //and 1 closed curly after all the commands

void loop() {
  //put your main code here, to run repeatedly:

  if (WiFi.status() == WL_CONNECTED) {
    Serial.println("\nConnected.");
    readVal = analogRead(voltrefPin);
    V0 = (19. / 1050.) * readVal;
    Serial.print("Battery Voltage is ");
    Serial.println (V0);

    // Write value to Field 1 of a ThingSpeak Channel
    int httpCode = ThingSpeak.writeField(myChannelNumber, 1, (V0), myWriteAPIKey);

    if (httpCode == 200) {
      Serial.println("Channel write successful.");
    }
    else {
      Serial.println("Problem writing to channel. HTTP error code " + String(httpCode));
    }

  }
  // ESP.deepSleep(600 * 1000000);}
  Serial.println("Going to sleep now night night.");
  ESP.deepSleep(20 * 1000000);
}//Used for quick testing

Why don’t you just get a new battery for the car?

Or one of these.

OP, where do you live?
It might be hard during the pandemic, but you could gain a lot by looking for a ‘buddy’ nearby that can help you get over the learning bump.

I’ve offered a few times over the years, but I suspect you’re a bit far away from me...!
I’m sure there’s someone closer by...
Try the Gigs & Collaboration section with a topic like [London] looking for Arduino mentor.

SteveMan, many thanks, and thanks to everyone else for there input.

The car has an intermittent short circuit. The battery can go flat, as in 2 volts in less than 4 hours. The dealers have had it three times and found nothing. £240 + vat each time. Opening a door seems to delay the problem for around 3 hours. I set the alarm clock to wake me every 4 hours so i can open a door.I have had 3 new batteries in 9 months. I cant afford to change the car. I live 35 mile from the hospital, and a taxi to see my terminally ill wife costs £45 round trip.
This device would at least let me get some sleep by alerting me to an imminent problem.

Hi,
Can you fit a battery isolator switch or just lift the battery lead from the battery every night?

Tom... :slight_smile: