Hello boys are new to the forum and especially the Arduino world.
I own an Arduino Uno connected at Bluetooth module HC05.
So far so good... The program perfectly runs, python receives from arduino datas and writes to my tables on mysql ... seems perfect ... too bad he writes only 124 records and then stops.
I think it is a buffer problem, I Googled very heavily in search of a solution but all the methods that I have found not works.
In theory it would be a home automation project so it should always work ...
I post both programs but are messed up I warn you.
Sorry for my bad english.
Ps I translated most of the comments, but I have no time to translate other
Arduino sketch
#include <SoftwareSerial.h>
#define rxPin 4
#define txPin 5
SoftwareSerial mySerial(rxPin, txPin);
//dht22
#include "DHT.h"
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22 // DHT 22 (AM2302)
DHT dht(DHTPIN, DHTTYPE);
// Sensore luce
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_TSL2561_U.h>
int ledPin = 7;
/* DHT22 */
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
float h=0;
// Read temperature as Celsius
float t=0;
/* MQ-7 Carbon Monoxide Sensor Circuit */
const int AOUTcopin=0;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int DOUTcopin=8;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
const int ledPinco=11;//the anode of the LED connects to digital pin D13 of the arduino
int limitco;
int valueco;
/* MQ-2 smoke */
const int AOUTsmpin=1;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int DOUTsmpin=9;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
const int ledPinsm=12;//the anode of the LED connects to digital pin D13 of the arduino
int limitsm;
int valuesm;
/* MQ-4 gas sensor */
const int AOUTmepin=2;//the AOUT pin of the CO sensor goes into analog pin A0 of the arduino
const int DOUTmepin=10;//the DOUT pin of the CO sensor goes into digital pin D8 of the arduino
const int ledPinme=13;//the anode of the LED connects to digital pin D13 of the arduino
int limitme;
int valueme;
/* Light sensor */
Adafruit_TSL2561_Unified tsl = Adafruit_TSL2561_Unified(TSL2561_ADDR_FLOAT, 12345);
void configureSensor(void)
{
sensor_t sensor;
tsl.getSensor(&sensor);
tsl.enableAutoRange(true);
tsl.setIntegrationTime(TSL2561_INTEGRATIONTIME_13MS);
}
void setup() {
mySerial.begin(9600);
//dht22
dht.begin();
// co sensor
pinMode(DOUTcopin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPinco, OUTPUT);//sets the pin as an output of the arduino
// smoke sensor
pinMode(DOUTsmpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPinsm, OUTPUT);//sets the pin as an output of the arduino
// smoke sensor
pinMode(DOUTmepin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPinme, OUTPUT);//sets the pin as an output of the arduino
// lux
configureSensor();
sensors_event_t event;
tsl.getEvent(&event);
}
void blinkLED(int input) {
digitalWrite(ledPin, HIGH);
delay(50);
digitalWrite(ledPin, LOW);
delay(50);
}
void svuotaBuffer() {
while(mySerial.available()) mySerial.read();
}
void loop() {
/////////////////////////////////////////// DHT 22
h = dht.readHumidity();
t = dht.readTemperature();
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t)) {
Serial.println("Failed to read from DHT sensor!");
return;
}
//////////////////////////// /* MQ-7 Carbon Monoxide Sensor Circuit */
valueco= analogRead(AOUTcopin);//reads the analaog value from the CO sensor's AOUT pin
limitco= digitalRead(DOUTcopin);//reads the digital value from the CO sensor's DOUT pin
if (valueco > 400){
digitalWrite(ledPinco, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPinco, LOW);//if threshold not reached, LED remains off
}
/* MQ-2 smoke */
valuesm= analogRead(AOUTsmpin);//reads the analaog value from the CO sensor's AOUT pin
limitsm= digitalRead(DOUTsmpin);//reads the digital value from the CO sensor's DOUT pin
if (valuesm > 400){
digitalWrite(ledPinsm, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPinsm, LOW);//if threshold not reached, LED remains off
}
/* MQ-4 gas sensor */
valueme= analogRead(AOUTmepin);//reads the analaog value from the CO sensor's AOUT pin
limitme= digitalRead(DOUTmepin);//reads the digital value from the CO sensor's DOUT pin
if (valueme > 400){
delay(10);
digitalWrite(ledPinme, HIGH);//if limit has been reached, LED turns on as status indicator
}
else{
digitalWrite(ledPinme, LOW);//if threshold not reached, LED remains off
}
/* Light sensor */
configureSensor();
sensors_event_t event;
tsl.getEvent(&event);
// Operazioni per calibrare i sensori
valueco = valueco - 130; // ################ #################
valuesm = valuesm - 30; // This is not beatifull but work good for me
valueme = valueme - 70; // ################ ################
/////////////////////////// Print
// Serial.print("Humidity: ");
mySerial.print(t);
mySerial.print(" ");
// Serial.print(" %\t");
// Serial.print("Temperature: ");
mySerial.print(h);
mySerial.print(" ");
// Serial.println(" *C ");
//Serial.print("Valore CO: ");
if (valueco < 10) {
mySerial.print("00"); //allway get 3 number
mySerial.print(valueco);//prints the CO value
}
else if (valueco<100)
{
mySerial.print("0"); //allway get 3 number
mySerial.print(valueco);//prints the CO value
}
else
{
mySerial.print(valueco);
}
mySerial.print(" ");
//Serial.print("Valore fumo: ");
if (valuesm < 10) {
mySerial.print("00"); //allway get 3 number
mySerial.print(valuesm);//prints the CO value
}
else if (valuesm<100)
{
mySerial.print("0"); //allway get 3 number
mySerial.print(valuesm);//prints the CO value
}
else
{
mySerial.print(valuesm);
}
mySerial.print(" ");
//Serial.print("Valore Metano: ");
if (valueme < 10) {
mySerial.print("00"); //allway get 3 number
mySerial.print(valueme);//prints the CO value
}
else if (valueme<100)
{
mySerial.print("0"); //allway get 3 number
mySerial.print(valueme);//prints the CO value
}
else
{
mySerial.print(valueme);
}
mySerial.print(" ");
//Serial.println(" lux");
// Display the results (light is measured in lux)
if (event.light)
{
if (event.light<10){
mySerial.print(event.light);
mySerial.print("000"); //allway get 5 number
}
else if (event.light<100){
mySerial.print(event.light);
mySerial.print("00"); //allway get 5 number
}
else if (event.light<1000){
mySerial.print(event.light);
mySerial.print("0"); //allway get 5 number
}
else{mySerial.print(event.light);}
}
else
{
// If event.light = 0 lux the sensor is probably saturated
// and no reliable data could be generated!
mySerial.print("//");
mySerial.println("//");}
mySerial.println(" ");
// Serial.println("Invio si spera completato");
mySerial.flush();
svuotaBuffer();
if ( mySerial.available() > 0 ) {
// read a numbers from serial port
int input = mySerial.parseInt();
// print out the received number
if (input > 0) {
// blink the LED
blinkLED(input);}}
}