Before creating the topic
I have a keyes SIM900A stacked on arduino mega, I am trying to use blynk for data visualizing but i get “Cannot init” . I have cleaned the void loop and still nothing. This is a hydroponic system project for school.
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#include <SimpleDHT.h>
#define BLYNK_PRINT Serial
#define TINY_GSM_MODEM_SIM900
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
char auth[] = "Bbbbbbbbbbbbbbbbbbbbbbbbb";
char apn[] = "YourAPN";
char user[] = "";
char pass[] = "";
#define SerialAT Serial1
TinyGsm modem(SerialAT);
int pinDHT11 = 28;
SimpleDHT11 dht11(pinDHT11);
BlynkTimer timer;
long duration;
int distanceCm;
long duration1;
int distanceCm1;
long duration2;
int distanceCm2;
const int trigPin = 26;
const int echoPin = 27;
const int trigPin1 = 22;
const int echoPin1 = 23;
const int trigPin2 = 24;
const int echoPin2 = 25;
int ph_sensor = A1;
int Light = A0;
int temperature = 0;
int LightIntensity = 0;
int nutrition = 0;
int nutritionPH = 0;
int vLight = 0;
int pH = 0;
int humidity = 0;
//LCD code I2C
LiquidCrystal_I2C lcd = LiquidCrystal_I2C(0x27, 20, 4); // 20 SDA and 21 SCL
void setup()
{
Serial.begin(9600);
delay(10);
SerialAT.begin(9600);
delay(3000);
modem.restart();
Blynk.begin(auth, modem, apn, user, pass);
timer.setInterval(500L, distanceCm);
timer.setInterval(500L, distanceCm1);
timer.setInterval(500L, distanceCm2);
timer.setInterval(500L, pH);
timer.setInterval(500L, LightIntensity);
timer.setInterval(500L, temperature);
timer.setInterval(500L, humidity);
lcd.init();
lcd.clear();
lcd.setCursor(0,0);
lcd.backlight();
lcd.print("Hudroponic system");
delay(5000);
pinMode(trigPin, OUTPUT);
pinMode(echoPin, INPUT);
pinMode(ph_sensor,INPUT);
pinMode(Light,INPUT);
pinMode(20,OUTPUT);
pinMode(21,OUTPUT);
pinMode(22,OUTPUT);
}
void loop()
{
Blynk.run();
timer.run();
}
void sensorDataSend()
{
lcd.clear();
lcd.setCursor(0,0);
pH_sens_DHT11();
ultrasonic();
pHLevel();
dht_sens();
delay(100);
// Blynk.virtualWrite(V0, h); Master start
// Blynk.virtualWrite(V1, t); Signal
Blynk.virtualWrite(V7, humidity);
Blynk.virtualWrite(V3, temperature);
Blynk.virtualWrite(V4, pH);
Blynk.virtualWrite(V5, distanceCm);
Blynk.virtualWrite(V6, distanceCm1);
Blynk.virtualWrite(V7, distanceCm2);
lcd.print("Temp = ");
lcd.print(temperature);
lcd.setCursor(0,1);
lcd.print("pH Level: ");
lcd.print(pH);
if(LightIntensity < 400)
{
lcd.setCursor(0,3);
lcd.print("Light is OK!!");
}
delay(3000);
if(pH < 6.2)
{
while(pH < 6.2) // add solution
{
digitalWrite(20, HIGH);
delay(500);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Adding nutrition");
lcd.setCursor(0,1);
lcd.print("Wait...");
}
delay(1000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("pH Level OK");
}
if(pH > 7.3) // adding water
{
digitalWrite(21, HIGH);
delay(500);
}
while(distanceCm < 5)
{
ultrasonic();
digitalWrite(20,HIGH);
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Adding Mixed Sol ");
lcd.setCursor(0,1);
lcd.print("Wait...");
}
while(distanceCm1 <5)
{
ultrasonic();
digitalWrite(21,HIGH);
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Adding nutrition");
lcd.setCursor(0,1);
lcd.print("Concentrated");
lcd.setCursor(0,2);
lcd.print("Wait...");
}
while(distanceCm2 < 10)
{
ultrasonic();
digitalWrite(22,HIGH);
delay(3000);
lcd.clear();
lcd.setCursor(0,0);
lcd.print("Adding Water");
lcd.setCursor(0,1);
lcd.print("Wait...");
}
}
//FUNCTIONS
//PH and Temp monitor
int pH_sens_DHT11()
{
for(int a =0; a < 20 ; a++)
{
vLight = analogRead(Light);
delay(5);
LightIntensity = LightIntensity + vLight;
nutrition = analogRead(ph_sensor);
delay(5);
nutritionPH = nutritionPH + nutrition;
}
delay(2000);
Serial.print("\n");
Serial.print("\n");
Serial.print("\t\t\t Values sampled :\n");
delay(1000);
LightIntensity = LightIntensity/20;
nutritionPH = nutritionPH/20;
//pH plus light intensity
Serial.print("\n");
Serial.print("Light intensity = ");
Serial.print(LightIntensity);
Serial.print("\n");
Serial.print("\n nutritionPH = ");
Serial.print(nutritionPH);
Serial.print("\n");
delay(500);
}
//Temp control
int dht_sens()
{
byte temperature = 0;
byte humidity = 0;
int err = SimpleDHTErrSuccess;
if ((err = dht11.read(&temperature, &humidity, NULL)) != SimpleDHTErrSuccess)
{
Serial.print("DHT not found, error = "); Serial.println(err);delay(1000);
return;
}
Serial.print("Sample OK: ");
Serial.print(temperature);
Serial.print( char(167));
Serial.print(" C, ");
Serial.print(humidity);
Serial.println(" H");
return;
}
// Tank level control
int ultrasonic()
{
digitalWrite(trigPin, LOW);
delayMicroseconds(2);
digitalWrite(trigPin, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin, LOW);
duration = pulseIn(echoPin, HIGH);
distanceCm= duration*0.034/2;
Serial.print("Distance: ");
Serial.print(distanceCm);
Serial.print(" cm\n");
digitalWrite(trigPin1, LOW);
delayMicroseconds(2);
digitalWrite(trigPin1, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin1, LOW);
duration1 = pulseIn(echoPin1, HIGH);
distanceCm1 = duration*0.034/2;
Serial.print("Distance: ");
Serial.print(distanceCm1);
Serial.print(" cm\n");
digitalWrite(trigPin2, LOW);
delayMicroseconds(2);
digitalWrite(trigPin2, HIGH);
delayMicroseconds(10);
digitalWrite(trigPin2, LOW);
duration2 = pulseIn(echoPin2, HIGH);
distanceCm2 = duration*0.034/2;
Serial.print("Distance: ");
Serial.print(distanceCm2);
Serial.print(" cm\n");
delay(1000);
LightIntensity = 0;
nutritionPH = 0;
}
//pH level control
int pHLevel()
{
pH_sens_DHT11();
pH = (nutritionPH/1023.0)* 14;
delay(100);
}