Ik probeer de S0 kwh meter uit te lezen en te versturen .
De sketch werkt wel met de ethernet aansluiting, nu heb ik geprobeerd dit aan te passen naar wifi.
Echter er worden geen gegevens verstuurt naar de database, serial print word het wel weergegeven.
Ik zie het niet, graag jullie hulp.
Het P2 bestand werkt en is bedoeld om de verbinding met de datebase te leggen en gegevens op te halen , en de database inlog is ook goed.
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
//SSID of your network
char ssid[] = "V**********F"; //SSID of your Wi-Fi router
char pass[] = "V**********S"; //Password of your Wi-Fi router
char serverName[] = "www.w********.com";
WiFiClient client;
const int requestPin = 5;
char input; // incoming serial data (byte)
bool readnextLine = false;
#define BUFSIZE 75
char buffer[BUFSIZE]; //Buffer for serial data to find \n .
int bufpos = 0;
long kwh = 0;
long lastTime = 0; // will store last time
long interval = 60000; // interval at which to blink (milliseconds)60000
volatile unsigned long rpk = 0; // raw pulse kwh counter (private)
volatile unsigned long rpk_old = 0; // value of last read (private)
volatile boolean CS_kwh = false; // Critical Section for kwh meter (private)
// The interrupt routine
void Kirq()
{
rpk++; // just increment raw pulse counter.
if (rpk > 1000000000) // reset pulse counter after 10e9 pulse = 500.000 KW
{
if (false == CS_kwh) // in critical section? // assumption IRQ-call is handled atomic on arduino.
{
rpk -= rpk_old;
rpk_old = 0;
}
}
}
// returns kwh's since last reset
float readkwh1()
{
return rpk / 1000.0; // one pulse = 0.5 watt.
}
// returns kwh's since last call
float readkwh()
{
CS_kwh = true; // Start Critical Section - prevent interrupt Kirq() from changing rpk & rpk_old ;
long t = rpk; // store the raw pulse counter in a temp var.
long k = t - rpk_old; // subtract last measure to get delta
rpk_old = t; // remember old value
CS_kwh = false; // End Critical Section
return k / 1000.0; // return delta, one pulse = 0.5 watt.
}
void setup() {
Serial.begin(115200);
delay(10);
delay(1000);
pinMode(4, OUTPUT); // SD select pin
digitalWrite(4, HIGH); // Explicitly disable SD
// kwh interrupt attached to IRQ 0 = pin2
attachInterrupt(0, Kirq, FALLING);
}
void loop() {
httpRequest();
delay(30000);
} //Einde loop
bool a;
void httpRequest() {
// if there's a successful connection:
if (a = client.connect(serverName, 80)) {
Serial.print("GET /datalogger/p2.php?kwh=");
float kwh = readkwh();
float sum = readkwh1();
Serial.print("kwhpuls: ");
Serial.print(kwh, 5);
Serial.print("kwhsum: ");
Serial.print(sum, 5);
Serial.println("Host: w***********.com");
Serial.println("User-Agent: arduino-ethernet");
Serial.println("Connection: stop/close");
Serial.println();
client.print("GET /datalogger/p2.php?kwh=");
client.print(kwh, 5);
client.print("&sum=");
client.print(sum, 5);
client.println(" HTTP/1.1");
client.println("Host: w********.com");
client.println("User-Agent: arduino-ethernet");
client.println("Connection: close");
client.println();
// Connect to Wi-Fi network
Serial.println();
Serial.println();
Serial.print("Connecting to...");
Serial.println(ssid);
WiFi.begin(ssid, pass);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("Wi-Fi connected successfully");
Serial.println(" HTTP/1.1");
//Request complete; empty recieve buffer
while (client.available()) { //data available
char c = client.read(); //gets byte from ethernet buffer
}
client.println();
Serial.println(a);
}
else {
client.stop();
}
}