Sensors coding

Hi guys I need help with my project I got two blink codes for two different sensors I want to compile them together in one code
fist code for temperature sensor

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <OneWire.h>
#include <DallasTemperature.h>

OneWire oneWire(D2); // digital D2 pin
DallasTemperature sensors(&oneWire);
BlynkTimer timer;
// You should get Auth Token in the Blynk App.
char auth[] = "kZ3kWJq4s42Nq8fcey43RNx-v7AOxOFE";
// Your WiFi credentials.
char ssid[] = "Shahrul";
char pass[] = "azlanDazman";

float  temp = 0;

void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  sensors.begin();
// Setup a function to be called every second  
  timer.setInterval(1000L, sendTemps);
}

void sendTemps()
{
 sensors.requestTemperatures();
 temp = sensors.getTempCByIndex(0);
 Serial.println(String("Sıcaklik=")+temp+ String(" C"));
 Blynk.virtualWrite(V1, temp);
}

void loop()
{
  Blynk.run();
  timer.run();
}

second code for PH sensor


#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <SoftwareSerial.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>

char auth[] = " jg_jGwBe3YXwxVV7xS-628sYP1uibX1M";
// Your WiFi credentials.
char ssid[] = "Utemhomies";
char pass[] = "38tu28697";

WidgetLCD lcd(V0);
float calibration_value = 26.84 - 0.7;
int phval = 0; 
unsigned long int avgval; 
int buffer_arr[10],temp;
 
float ph_act;
 
void setup() 
{
  Wire.begin();
 Serial.begin(9600);
 Blynk.begin(auth, ssid, pass);
}
void loop() {
 for(int i=0;i<10;i++) 
 { 
 buffer_arr[i]=analogRead(A0);
 delay(30);
 }
 for(int i=0;i<9;i++)
 {
 for(int j=i+1;j<10;j++)
 {
 if(buffer_arr[i]>buffer_arr[j])
 {
 temp=buffer_arr[i];
 buffer_arr[i]=buffer_arr[j];
 buffer_arr[j]=temp;
 }
 }
 }
 avgval=0;
 for(int i=2;i<8;i++)
 avgval+=buffer_arr[i];
 float volt=(float)avgval*5.0/1024/6; 
  ph_act = -4.6 * volt + calibration_value;
 
 Serial.print("'Group 5 IDP' pH Val: ");
 Serial.println(ph_act);
 lcd.print(0,0,"pH Value:");
 lcd.print(0,1,ph_act);
 delay(1000);
}

thank you

Your second sketch is a great example of how not to write code for Blynk.
It has a cluttered void loop, uses delays, and doesn’t use a timer.

I’d suggest that you amend your first sketch and add-in the relevant code from the second sketch. But, instead of putting the code from the second void loop into the void loop of your new sketch, put it in a function and call it with a timer.

Also, instead of using variable names like temp to indicate that it’s a temporary buffer:

use something that won’t be confused as an abbreviation for temperature and clash with the variable used to hold the temperature i the first sketch:

Pete.

1 Like