Skip to content

Instantly share code, notes, and snippets.

@whatnick
Created September 3, 2015 13:08
Show Gist options
  • Star 12 You must be signed in to star a gist
  • Fork 5 You must be signed in to fork a gist
  • Save whatnick/73c244694466355f6a14 to your computer and use it in GitHub Desktop.
Save whatnick/73c244694466355f6a14 to your computer and use it in GitHub Desktop.
ESP8266 Energy Monitor
/*
* This sketch sends ads1115 current sensor data via HTTP POST request to thingspeak server.
* It needs the following libraries to work (besides the esp8266 standard libraries supplied with the IDE):
*
* - https://github.com/adafruit/Adafruit_ADS1X15
*
* designed to run directly on esp8266-01 module, to where it can be uploaded using this marvelous piece of software:
*
* https://github.com/esp8266/Arduino
*
* 2015 Tisham Dhar
* licensed under GNU GPL
*/
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <Adafruit_ADS1015.h>
// replace with your channel's thingspeak API key,
String apiKey = "XXXXXXXXXXXXX";
//WIFI credentials go here
const char* ssid = "XXXXXXXXXXX";
const char* password = "XXXXXXXXXXXXXXXXXXXXX";
Adafruit_ADS1115 ads; /* Use this for the 16-bit version */
const char* server = "api.thingspeak.com";
WiFiClient client;
double offsetI;
double filteredI;
double sqI,sumI;
int16_t sampleI;
double Irms;
double squareRoot(double fg)
{
double n = fg / 2.0;
double lstX = 0.0;
while (n != lstX)
{
lstX = n;
n = (n + fg / n) / 2.0;
}
return n;
}
double calcIrms(unsigned int Number_of_Samples)
{
/* Be sure to update this value based on the IC and the gain settings! */
float multiplier = 0.125F; /* ADS1115 @ +/- 4.096V gain (16-bit results) */
for (unsigned int n = 0; n < Number_of_Samples; n++)
{
sampleI = ads.readADC_Differential_0_1();
// Digital low pass filter extracts the 2.5 V or 1.65 V dc offset,
// then subtract this - signal is now centered on 0 counts.
offsetI = (offsetI + (sampleI-offsetI)/1024);
filteredI = sampleI - offsetI;
//filteredI = sampleI * multiplier;
// Root-mean-square method current
// 1) square current values
sqI = filteredI * filteredI;
// 2) sum
sumI += sqI;
}
Irms = squareRoot(sumI / Number_of_Samples)*multiplier;
//Reset accumulators
sumI = 0;
//--------------------------------------------------------------------------------------
return Irms;
}
void setup() {
Serial.begin(115200);
delay(10);
// We start by connecting to a WiFi network
Serial.println();
Serial.println();
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());
ads.setGain(GAIN_ONE); // 1x gain +/- 4.096V 1 bit = 2mV 0.125mV
ads.begin();
}
void loop() {
//Serial.print("Differential: "); Serial.print(results); Serial.print("("); Serial.print(trans_volt); Serial.println("mV)");
double current = calcIrms(2048);
if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
String postStr = apiKey;
postStr +="&field1=";
postStr += String(current);
postStr += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(postStr.length());
client.print("\n\n");
client.print(postStr);
}
client.stop();
//Serial.println("Waiting...");
// thingspeak needs minimum 15 sec delay between updates
delay(20000);
}
@Jaizan
Copy link

Jaizan commented Oct 16, 2021

Thanks for writing this.
Mine is crashing. It seems to go wrong at the line: double current = calcIrms(2048);
I determined that by changing it to double current =1; and this results in the code working and uploading the value 1 to Thingspeak.
Any ideas where I am going wrong ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment