But I have noticed that the following occurs
Connect TO WIFI
After 3 seconds it disconnects
After about 6 seconds it says there is no access point
Then next time it comes to upload I reconnect (since it is not connected) and the same thing happens time and time again.
Connecting to CROWE
......
WiFi connected
IP address:
192.168.2.128
Interval=1, WiFi.status=WL_CONNECTED=3
Interval=2, WiFi.status=WL_CONNECTED=3
Interval=3, WiFi.status=WL_CONNECTED=3
Interval=4, WiFi.status=WL_CONNECTED=3
Interval=5, WiFi.status=WL_DISCONNECTED=6
Interval=6, WiFi.status=WL_DISCONNECTED=6
Interval=7, WiFi.status=WL_DISCONNECTED=6
Interval=8, WiFi.status=WL_DISCONNECTED=6
Interval=9, WiFi.status=WL_DISCONNECTED=6
Interval=10, WiFi.status=WL_DISCONNECTED=6
Interval=11, WiFi.status=WL_DISCONNECTED=6
Interval=12, WiFi.status=WL_NO_SSID_AVAIL=1
Interval=13, WiFi.status=WL_NO_SSID_AVAIL=1
Interval=14, WiFi.status=WL_NO_SSID_AVAIL=1
Interval=15, WiFi.status=WL_NO_SSID_AVAIL=1
Interval=16, WiFi.status=WL_NO_SSID_AVAIL=1
Interval=17, WiFi.status=WL_NO_SSID_AVAIL=1
Interval=18, WiFi.status=WL_NO_SSID_AVAIL=1
The code is very simple as I have removed other stuff except WIFI
#include <ESP8266WiFi.h>
const char* ssid = "CROWE";
const char* password = "abcd";
const char* host = "192.168.2.122";
String Statuses[] = { "WL_IDLE_STATUS=0", "WL_NO_SSID_AVAIL=1", "WL_SCAN_COMPLETED=2", "WL_CONNECTED=3", "WL_CONNECT_FAILED=4", "WL_CONNECTION_LOST=5", "WL_DISCONNECTED=6"};
unsigned long lastMillis = 0;
unsigned long lastMillis2 = 0;
int IntervalInSeconds = 1;
int Errors = 0;
int min = 1024;
int max = -1024;
int samples = 0;
void setup() {
Serial.begin(115200);
Serial.println("ESP8266 Power Monitor");
WiFi.mode(WIFI_STA);
WiFi.disconnect();
Connect();
}
void Connect()
{
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
int Attempt = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Attempt++;
Serial.print(".");
if (Attempt == 50)
{
Serial.println();
Serial.println("Could not connect to WIFI");
return;
}
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
void loop()
{
yield() ; // Allow WIFI to do stuff
unsigned long CurrentMillis = millis();
if (CurrentMillis - lastMillis2 > 3)
{
lastMillis2 = CurrentMillis;
double sensorValue = 0;
for (int reading = 0; reading < 5; reading++)
{
sensorValue += analogRead(A0);
}
sensorValue /= 5;
if ((int)sensorValue < min)
min = (int)sensorValue;
if ((int)sensorValue > max)
max = (int)sensorValue;
samples++;
}
if (CurrentMillis - lastMillis > 900)
{
lastMillis = CurrentMillis;
Serial.print("Interval=");
Serial.print(IntervalInSeconds);
Serial.print(", WiFi.status=");
Serial.print(Statuses[WiFi.status()]);
if (IntervalInSeconds >= 60)
{
IntervalInSeconds = 1;
Serial.print("connecting to ");
Serial.println(host);
if (WiFi.status() != WL_CONNECTED)
Connect();
if (WiFi.status() == WL_CONNECTED)
{
// Use WiFiClient class to create TCP connections
WiFiClient client;
const int httpPort = 8080;
if (!client.connect(host, httpPort)) {
Serial.println("connection failed");
return;
}
// We now create a URI for the request
String url = "/Power/?Watts=";
url += "SOMENUMBER";
url += "&DeviceID=HomeTheatre";
Serial.print("Requesting URL: ");
Serial.println(url);
// This will send the request to the server
client.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(50);
// Read all the lines of the reply from server and print them to Serial
while (client.available()) {
String line = client.readStringUntil('\r');
Serial.print(line);
}
Serial.println();
}
else
{
Serial.print("Error Not Connected To WIFI, Error Count: ");
Serial.println(++Errors);
}
}
else
Serial.println();
IntervalInSeconds = IntervalInSeconds + 1;
min = 1024;
max = -1024;
samples = 0;
}
}
Now I have found two things:
1) The analogRead() is buggy in the current release of the Arduino IDE and I have modified the code for analogRead as per the discussion at the following:
https://github.com/esp8266/Arduino/issues/338
The code I changed it to was this
core_esp8266_wiring_analog.c in the folder C:\Users\ccrowe.AP\AppData\Roaming\Arduino15\packages\esp8266\hardware\esp8266\1.6.4-673-g8cd3697\cores\esp8266
extern int __analogRead(uint8_t pin) {
if (pin == 17){
return 0xffff & system_adc_read();
}
return digitalRead(pin) * 1023;
}
This code change fixed the issue with reading erroneous values from the ADC.
But if I comment out the analogRead() in my script the wifi does not disconnect, it stays connected.
So either there is a problem with the system_adc_read() function or something else is going on....
Chris