0% found this document useful (0 votes)
18 views3 pages

New Text Document

This document contains code to connect an ESP8266 WiFi module to a WiFi network, read voltage values from analog pins, calculate the voltages using calibration constants, and send the voltage readings to a ThingSpeak channel. The code connects to WiFi, initializes the analog pins as inputs, takes multiple readings to calculate the average voltages, applies calibration to determine actual voltages, and writes the voltage values to multiple fields in a ThingSpeak channel.

Uploaded by

hddhanushka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views3 pages

New Text Document

This document contains code to connect an ESP8266 WiFi module to a WiFi network, read voltage values from analog pins, calculate the voltages using calibration constants, and send the voltage readings to a ThingSpeak channel. The code connects to WiFi, initializes the analog pins as inputs, takes multiple readings to calculate the average voltages, applies calibration to determine actual voltages, and writes the voltage values to multiple fields in a ThingSpeak channel.

Uploaded by

hddhanushka
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 3

#include <ESP8266WiFi.

h>

#include "ThingSpeak.h"

unsigned long chanelId = 1837630;


//String apiWritekey = "7YCUG5T0QVDEXZUV";
const char *rightAPIkey = "7YCUG5T0QVDEXZUV";
const char *ssid = "Dialog 4G 302";
const char *password = "9D831Ce1";
//float sensor0=0;
//float sensor1=0;

float input_voltage1 = 0.0;


float input_voltage2 = 0.0;
float input_voltage3 =0.0;

//Seperate value mechrment


float m1=0.0895;
float c1=1.2746;
//Total voltage mecherment values
float m2=0.0916;
float c2=1.0076;

float x1=0.0;
float x2=0.0;
float x3=0.0;
int tol=5;
//float k=2.7118769913;
float analog_value1 = 0.0;
float analog_value2 = 0.0;

#define S0 D4
#define S1 D3
#define S2 D2
#define S3 D1
const char* server = "api.thingspeak.com";

WiFiClient client;

void setup() {
Serial.begin(9600);
pinMode(S0,OUTPUT);
pinMode(S1,OUTPUT);
pinMode(S2,OUTPUT);
pinMode(S3,OUTPUT);

WiFi.disconnect();
delay(10);
WiFi.begin(ssid, password);

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.print("NodeMcu connected to wifi...");
Serial.println(ssid);
Serial.println();
ThingSpeak.begin(client);

}
void loop() {

digitalWrite(S0,LOW); digitalWrite(S1,LOW); digitalWrite(S2,LOW);


digitalWrite(S3,LOW);
analog_value1 = analogRead(A0);
digitalWrite(S0,HIGH); digitalWrite(S1,LOW); digitalWrite(S2,LOW);
digitalWrite(S3,LOW);
analog_value2 = analogRead(A0);

int loops1 = 100; // number of samples


float vtot1 = 0.0;
// loop multiple times and get average reading
for (int i=0; i < loops1; i++) {
vtot1 = vtot1 + analog_value1;
}
x1 = (vtot1/loops1)-tol;
input_voltage1 = m1*x1+c1 ;
///////////////////////////////////////////////////////////////////////////////////
//////////////////////
int loops2 = 100; // number of samples
float vtot2 = 0.0;
// loop multiple times and get average reading
for (int i=0; i < loops2; i++) {
vtot2 = vtot2 + analog_value2;
}
x2 = (vtot2/loops2)-tol;
input_voltage2 = m2*x2+c2 ;
//////////////////////////////////////////////////////////////////////////////////
////////////////////
x3=x2-x1-tol;
input_voltage3=x3*m1+c1 ;

ThingSpeak.setField(1,input_voltage1);
ThingSpeak.setField(2,input_voltage2);
ThingSpeak.setField(3,input_voltage3);
ThingSpeak.setField(4,x1);
ThingSpeak.setField(5,x2);
ThingSpeak.setField(6,x3);
int respons = ThingSpeak.writeFields(chanelId,rightAPIkey);

Serial.println(x1);
Serial.println(x3);
Serial.println(x2);
Serial.println(input_voltage3);
Serial.println(input_voltage1);
Serial.println(input_voltage2);

Serial.println();
delay(1000);
}

You might also like