Hi.
I always get an HTTP error when my program uses analogRead.
Here is "console":
6:59:42.116 -> WiFi connected.
16:59:42.116 -> IP address:
16:59:42.162 -> 192.168.1.125
16:59:42.162 -> http://192.168.1.92:81/Handler1.ashx?action=dator&namn=PowerCounter&ip=192.168.1.125&start=2021-09-28
16:59:42.302 -> HTTP Response code: 200
16:59:42.302 -> OK
17:00:42.281 -> http://192.168.1.92:81/Handler1.ashx?action=skickaKW&namn=PowerCounter&count=22&start=2021-09-28
17:00:42.374 -> Error code: -1
If I cut and paste the URL on firefox, then it will work. In the log for iis server there is nothing.
If I remark the analogRead statement and hardcode a value then it work. Here is the "console":
6:59:42.116 -> WiFi connected.
16:59:42.116 -> IP address:
16:59:42.162 -> 192.168.1.125
16:59:42.162 -> http://192.168.1.92:81/Handler1.ashx?action=dator&namn=PowerCounter&ip=192.168.1.125&start=2021-09-28
16:59:42.302 -> HTTP Response code: 200
16:59:42.302 -> OK
17:00:42.281 -> http://192.168.1.92:81/Handler1.ashx?action=skickaKW&namn=PowerCounter&count=22&start=2021-09-28
17:00:42.374 -> Error code: -1
And my program looks like this:
#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
// Replace with your network credentials
const char* ssid = "****";
const char* password = "********";
String header;
int sensorPin = A0; //define analog pin 0
int value = 0;
int flagg = 0;
int ledFlagg = 0;
int i = 0;
unsigned long myTime;
unsigned long nyTime;
void setup() {
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected.");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
String serverName = "http://192.168.1.92:81/Handler1.ashx";
String IP = WiFi.localIP().toString().c_str();
String serverPath = serverName + "?action=dator&namn=PowerCounter&ip=" + IP + "&start=2021-09-28";
sendtoweb(serverPath);
myTime = millis();
}
void sendtoweb(String webstring) {
Serial.println(webstring);
WiFiClient client;
HTTPClient http;
http.begin(client, webstring.c_str());
// Send HTTP GET request
int httpResponseCode = http.GET();
if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();
}
void loop() {
value = analogRead(sensorPin);
if (value < 600) {
flagg = 0;
}
if (flagg == 0 && value > 599 ) {
i++;
if (ledFlagg == 1) {
digitalWrite(LED_BUILTIN,LOW);
ledFlagg = 0;
}else{
digitalWrite(LED_BUILTIN,HIGH);
ledFlagg = 1;
}
flagg = 1;
}
nyTime = millis();
if ((nyTime - myTime) > 60000) {
int i = 22; // HARDCODED for the test.
String serverName = "http://192.168.1.92:81/Handler1.ashx";
String serverPath = serverName + "?action=skickaKW&namn=PowerCounter&count=" + i + "&start=2021-09-28";
sendtoweb(serverPath);
myTime = millis();
i = 0;
}
}
Anything missing??
/Stefan