• Witaj na Forum Arduino Polska! Zapraszamy do rejestracji!
  • Znajdziesz tutaj wiele informacji na temat hardware / software.
Witaj! Logowanie Rejestracja


Ocena wątku:
  • 0 głosów - średnia: 0
  • 1
  • 2
  • 3
  • 4
  • 5
Program z zegarem
#1
Witam.
Szukam pomocy z programem który ma mierzyć kilka temperatur, pomiary mają być wykonywane np co 5 minut i wyświetlane na LCD i z tym nie ma problemu. Problem zaczyna się z następną funkcją tzn ma tam być jeszcze zegar który na wyświetlaczu powinien odświeżać się co sekundę. Jak to zrobić?
 
Odpowiedź
#2
Zrób po prostu pętle co 1 sec i powtórz ja tyle razy aby była 5min. A czemu musi być pomiar co 5 min ?
 
Odpowiedź
#3
Działa ci ten program ?? jest troszkę namieszane i jakoś ciężko mi skumać jego działanie . Sprawdziłem przerwania działają jak zakładałem , sprawdź tylko czy jak włączysz zegar na pinie SQW czy się co sekundę pojawia 5V ( powinno bo w twojej bibliotece jest to ustawione domyślnie ) .
Ważne aby robić co się lubi albo lubić co się robi .
Arduino UNO, TINY, PRO MINI
Pomoc nagradzamy punktami reputacji Wink
 
Odpowiedź
#4
Program działa a zegar sprawdzę.
 
Odpowiedź
#5
Niestety na przerwaniach to działać za chiny ludowe nie chce ( i nie będzie taki urok przerwań ) , poniżej masz na opóźniaczach czasowych . Zegar będzie dostawał czkawki w momencie wysyłania danych przez wifi i tutaj to już inny problem . Sprawdź i daj znać .

Kod:
#include<stdlib.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BACKLIGHT_PIN 3
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);

#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define SSID ""
#define PASS ""
#define IP "184.106.153.148"
String GET = "GET /update?key=asdfghytrewqlk&field1=";
SoftwareSerial monitor(10, 11);

long pm1 = 0;
long pm2 = 0;
long pm3 = 0;

const char *monthName[12] = {
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12"
};
byte temp[8] = //ikona temperatury
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};

byte dol[8] = //ikona wilgotnosci
{
B00100,
B00100,
B00100,
B00100,
B00100,
B10101,
B01110,
B00100,
};

byte stopnie[8] = //ikona stopni
{
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000,
B00000
};

byte dom[8] = //ikona stopni
{
B00000,
B00100,
B01110,
B11111,
B01010,
B01010,
B01110,
B01110
};
byte gora[8] = //ikona stopni
{
B00100,
B01110,
B10101,
B00100,
B00100,
B00100,
B00100,
B00100
};

DeviceAddress P1 = { 0x28, 0x80, 0x9B, 0x1E, 0x07, 0x00, 0x00, 0x64 }; // "4"
DeviceAddress P2 = { 0x28, 0x08, 0xC2, 0x1F, 0x07, 0x00, 0x00, 0xC6 }; // "5"
DeviceAddress P3 = { 0x28, 0x01, 0xD3, 0x1F, 0x07, 0x00, 0x00, 0xE1 }; // "4" Again for test
DeviceAddress P4 = { 0x28, 0xD5, 0xB8, 0x1F, 0x07, 0x00, 0x00, 0x98 };
DeviceAddress P5 = { 0x28, 0x07, 0xD1, 0x1F, 0x07, 0x00, 0x00, 0xD0 };
void setup()
{
monitor.begin(115200);
Serial.begin(115200);
sensors.begin();
Wire.begin();
lcd.begin(20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.createChar(0, temp);
lcd.createChar(1, dol);
lcd.createChar(3, stopnie);
lcd.createChar(4, dom);
lcd.createChar(5, gora);
sendDebug("AT");
delay(5000);
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
connectWiFi();
}
}

void loop(){
  
long cm = millis();
  
  tmElements_t tm;

if ( cm - pm > 1000 ){

if (RTC.read) {

lcd.setCursor(0,0);
// lcd.print("TIME :");
// lcd.setCursor(6,0);
print2digits(tm.Hour);
lcd.print(':');
print2digits(tm.Minute);
lcd.print(':');
print2digits(tm.Second);

lcd.setCursor(10,0);
lcd.print("DATE :");
lcd.print(tm.Day);
lcd.print('/');
lcd.print(tm.Month);
lcd.print('/');
lcd.print(tmYearToCalendar(tm.Year));
pm1 = cm ;
}}
  
if ( (cm/1000) - (pm2/1000) >= 5 ){
  
sensors.requestTemperatures();
float tempC1 = sensors.getTempC(P1);
float tempC2 = sensors.getTempC(P2);
float tempC3 = sensors.getTempC(P3);
float tempC4 = sensors.getTempC(P4);
float tempC5 = sensors.getTempC(P5);

lcd.setCursor (0, 1);
lcd.write(byte(4));
lcd.write(byte(5));
lcd.print("=");
lcd.print(tempC1, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (10, 1);
lcd.write(byte(4));
lcd.write(byte(1));
lcd.print("=");
lcd.print(tempC2, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (0, 2);
lcd.write(byte(0));
lcd.write(byte(5));
lcd.print("=");
lcd.print(tempC3, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (10, 2);
lcd.write(byte(0));
lcd.write(byte(1));
lcd.print("=");
lcd.print(tempC4, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (0, 3);
lcd.write(byte(0));
lcd.print("=");
lcd.print(tempC5, 1);
lcd.write(byte(3));
lcd.print("C");
pm2 = cm ;
}

if ( (cm/1000) - (pm3/1000) >= 60 ){

//tempC = DallasTemperature::toFahrenheit(tempC);
char buffer[10];
String tempT1 = dtostrf(tempC1, 4, 1, buffer);
String tempT2 = dtostrf(tempC2, 4, 1, buffer);
String tempT3 = dtostrf(tempC3, 4, 1, buffer);
String tempT4 = dtostrf(tempC4, 4, 1, buffer);
String tempT5 = dtostrf(tempC5, 4, 1, buffer);
updateTemp(tempT1, tempT2, tempT3, tempT4, tempT5);
pm3 = cm ;
  
  }

}



void updateTemp(String tempT1, String tempT2, String tempT3, String tempT4, String tempT5){

String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if(Serial.find("Error")){
monitor.print("RECEIVED: Error");
return;
}
cmd = GET;
cmd += tempT1;
cmd += "&field2=";
cmd += tempT2;
cmd += "&field3=";
cmd += tempT3;
cmd += "&field4=";
cmd += tempT4;
cmd += "&field5=";
cmd += tempT5;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
monitor.print(">");
monitor.print(cmd);
Serial.print(cmd);
}else{
sendDebug("AT+CIPCLOSE");
}
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
}else{
monitor.println("RECEIVED: Error");
}
}


void sendDebug(String cmd){
monitor.print("SEND: ");
monitor.println(cmd);
Serial.println(cmd);
}


boolean connectWiFi(){
Serial.println("AT+CIPMUX=0");
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
return true;
}else{
monitor.println("RECEIVED: Error");
return false;
  }
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.print('0');
}
lcd.print(number);
}
Ważne aby robić co się lubi albo lubić co się robi .
Arduino UNO, TINY, PRO MINI
Pomoc nagradzamy punktami reputacji Wink
 
Odpowiedź
#6
Nie chce się to kompilować, wywala takie błędy:
esp.ino: In function 'void loop()':
121: error: 'pm' was not declared in this scope
198: error: 'tempC1' was not declared in this scope
199: error: 'tempC2' was not declared in this scope
200: error: 'tempC3' was not declared in this scope
201: error: 'tempC4' was not declared in this scope
202: error: 'tempC5' was not declared in this scope

'pm' was not declared in this scope
 
Odpowiedź
#7
Poprawiane

Kod:
#include<stdlib.h>
#include <DS1307RTC.h>
#include <Time.h>
#include <SoftwareSerial.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <LiquidCrystal_I2C.h>
#define BACKLIGHT_PIN 3
LiquidCrystal_I2C lcd(0x27,2,1,0,4,5,6,7);

#define ONE_WIRE_BUS 8
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
#define SSID ""
#define PASS ""
#define IP "184.106.153.148"
String GET = "GET /update?key=asdfghytrewqlk&field1=";
SoftwareSerial monitor(10, 11);

long pm1 = 0;
long pm2 = 0;
long pm3 = 0;

float tempC1;
float tempC2;
float tempC3;
float tempC4;
float tempC5;

const char *monthName[12] = {
"01", "02", "03", "04", "05", "06",
"07", "08", "09", "10", "11", "12"
};
byte temp[8] = //ikona temperatury
{
B00100,
B01010,
B01010,
B01110,
B01110,
B11111,
B11111,
B01110
};

byte dol[8] = //ikona wilgotnosci
{
B00100,
B00100,
B00100,
B00100,
B00100,
B10101,
B01110,
B00100,
};

byte stopnie[8] = //ikona stopni
{
B01100,
B10010,
B10010,
B01100,
B00000,
B00000,
B00000,
B00000
};

byte dom[8] = //ikona stopni
{
B00000,
B00100,
B01110,
B11111,
B01010,
B01010,
B01110,
B01110
};
byte gora[8] = //ikona stopni
{
B00100,
B01110,
B10101,
B00100,
B00100,
B00100,
B00100,
B00100
};

DeviceAddress P1 = { 0x28, 0x80, 0x9B, 0x1E, 0x07, 0x00, 0x00, 0x64 }; // "4"
DeviceAddress P2 = { 0x28, 0x08, 0xC2, 0x1F, 0x07, 0x00, 0x00, 0xC6 }; // "5"
DeviceAddress P3 = { 0x28, 0x01, 0xD3, 0x1F, 0x07, 0x00, 0x00, 0xE1 }; // "4" Again for test
DeviceAddress P4 = { 0x28, 0xD5, 0xB8, 0x1F, 0x07, 0x00, 0x00, 0x98 };
DeviceAddress P5 = { 0x28, 0x07, 0xD1, 0x1F, 0x07, 0x00, 0x00, 0xD0 };
void setup()
{
monitor.begin(115200);
Serial.begin(115200);
sensors.begin();
Wire.begin();
lcd.begin(20, 4);
lcd.setBacklightPin(BACKLIGHT_PIN,POSITIVE);
lcd.setBacklight(HIGH);
lcd.createChar(0, temp);
lcd.createChar(1, dol);
lcd.createChar(3, stopnie);
lcd.createChar(4, dom);
lcd.createChar(5, gora);
sendDebug("AT");
delay(5000);
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
connectWiFi();
}
}

void loop(){
  
long cm = millis();
  
  tmElements_t tm;

if ( cm - pm1 > 1000 ){

if (RTC.read) {

lcd.setCursor(0,0);
// lcd.print("TIME :");
// lcd.setCursor(6,0);
print2digits(tm.Hour);
lcd.print(':');
print2digits(tm.Minute);
lcd.print(':');
print2digits(tm.Second);

lcd.setCursor(10,0);
lcd.print("DATE :");
lcd.print(tm.Day);
lcd.print('/');
lcd.print(tm.Month);
lcd.print('/');
lcd.print(tmYearToCalendar(tm.Year));
pm1 = cm ;
}}
  
if ( (cm/1000) - (pm2/1000) >= 5 ){
  
sensors.requestTemperatures();
tempC1 = sensors.getTempC(P1);
tempC2 = sensors.getTempC(P2);
tempC3 = sensors.getTempC(P3);
tempC4 = sensors.getTempC(P4);
tempC5 = sensors.getTempC(P5);

lcd.setCursor (0, 1);
lcd.write(byte(4));
lcd.write(byte(5));
lcd.print("=");
lcd.print(tempC1, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (10, 1);
lcd.write(byte(4));
lcd.write(byte(1));
lcd.print("=");
lcd.print(tempC2, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (0, 2);
lcd.write(byte(0));
lcd.write(byte(5));
lcd.print("=");
lcd.print(tempC3, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (10, 2);
lcd.write(byte(0));
lcd.write(byte(1));
lcd.print("=");
lcd.print(tempC4, 1);
lcd.write(byte(3));
lcd.print("C");

lcd.setCursor (0, 3);
lcd.write(byte(0));
lcd.print("=");
lcd.print(tempC5, 1);
lcd.write(byte(3));
lcd.print("C");
pm2 = cm ;
}

if ( (cm/1000) - (pm3/1000) >= 60 ){

//tempC = DallasTemperature::toFahrenheit(tempC);
char buffer[10];
String tempT1 = dtostrf(tempC1, 4, 1, buffer);
String tempT2 = dtostrf(tempC2, 4, 1, buffer);
String tempT3 = dtostrf(tempC3, 4, 1, buffer);
String tempT4 = dtostrf(tempC4, 4, 1, buffer);
String tempT5 = dtostrf(tempC5, 4, 1, buffer);
updateTemp(tempT1, tempT2, tempT3, tempT4, tempT5);
pm3 = cm ;
  
  }

}



void updateTemp(String tempT1, String tempT2, String tempT3, String tempT4, String tempT5){

String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
delay(2000);
if(Serial.find("Error")){
monitor.print("RECEIVED: Error");
return;
}
cmd = GET;
cmd += tempT1;
cmd += "&field2=";
cmd += tempT2;
cmd += "&field3=";
cmd += tempT3;
cmd += "&field4=";
cmd += tempT4;
cmd += "&field5=";
cmd += tempT5;
cmd += "\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">")){
monitor.print(">");
monitor.print(cmd);
Serial.print(cmd);
}else{
sendDebug("AT+CIPCLOSE");
}
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
}else{
monitor.println("RECEIVED: Error");
}
}


void sendDebug(String cmd){
monitor.print("SEND: ");
monitor.println(cmd);
Serial.println(cmd);
}


boolean connectWiFi(){
Serial.println("AT+CIPMUX=0");
Serial.println("AT+CWMODE=1");
delay(2000);
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
delay(5000);
if(Serial.find("OK")){
monitor.println("RECEIVED: OK");
return true;
}else{
monitor.println("RECEIVED: Error");
return false;
  }
}

void print2digits(int number) {
if (number >= 0 && number < 10) {
lcd.print('0');
}
lcd.print(number);
}
Ważne aby robić co się lubi albo lubić co się robi .
Arduino UNO, TINY, PRO MINI
Pomoc nagradzamy punktami reputacji Wink
 
Odpowiedź
#8
Dziękuję za pomoc, niestety jest problem z odświeżaniem zegara. Nie odświeża się co sekundę, nie działa też połączenie z serwerem.
 
Odpowiedź
  


Skocz do:


Przeglądający: 1 gości