Facebook
From Mungo Moth, 4 Years ago, written in Plain Text.
This paste is a reply to Untitled from Smelly Coyote - view diff
Embed
Download Paste or View Raw
Hits: 328
  1. #define BLYNK_PRINT Serial
  2. #include <ESP8266WiFi.h>
  3. #include <BlynkSimpleEsp8266.h>
  4.  
  5.  
  6. /***************************/
  7.  
  8. #include <LiquidCrystal_I2C.h> // wyswietlacz
  9. #include <OneWire.h> //
  10. #include <DallasTemperature.h> //czyujnik temp
  11. #include <Wire.h>
  12.  
  13. String apiKey = "";     //  Enter your Write API key from ThingSpeak
  14. char auth[] = "";
  15. const char *ssid =  "";     // replace with your wifi ssid and wpa2 key
  16. const char *pass =  "";
  17. const char* server = "api.thingspeak.com";
  18.  
  19.  
  20.  
  21. WiFiClient client;
  22. BlynkTimer timer;
  23.  
  24. #define przekaznik D1
  25.  
  26. LiquidCrystal_I2C lcd(0x27,16,2); //wyswietlacz
  27. OneWire oneWire(D2 );
  28. DallasTemperature sensors(&oneWire);
  29.  
  30.  
  31. int counter = 25; // poczatkowa temp.
  32. bool bPress = false;
  33.  
  34. unsigned long timeStart = 0;
  35.  
  36.  
  37.  
  38. bool isCounting = false;
  39.  
  40. int CalcSt = 30 ;
  41.  
  42. float x = 0;
  43. const int Up = D7;
  44. const int Down = D8;
  45. int upbutt = 0;
  46. int uplastbutt = 0;
  47. int downbutt = 0;
  48. int downlastbutt = 0;
  49.  
  50. void setup(){
  51.  
  52.  
  53.   Wire.begin(D5,D6);
  54.   lcd.begin();
  55.   lcd.clear();
  56.   lcd.setCursor(0,0); // napis u gory
  57.   lcd.print("Ustaw temp: ");
  58.   lcd.print(counter);
  59.   lcd.setCursor(0,1); // napis na dole
  60.   lcd.print("T:00.00*C");
  61.  
  62.   sensors.begin();
  63.    pinMode(przekaznik, OUTPUT);
  64.   pinMode(Up, INPUT_PULLUP);
  65.   pinMode(Down, INPUT_PULLUP);
  66.   timeStart=millis();
  67.   isCounting = true;
  68.  
  69.   //////////
  70.  
  71.        Serial.begin(115200);
  72.        delay(10);
  73.        
  74.  
  75.        Serial.println("Connecting to ");
  76.        Serial.println(ssid);
  77.  
  78.  
  79.        WiFi.begin(ssid, pass);
  80.  
  81.       while (WiFi.status() != WL_CONNECTED)
  82.      {
  83.             delay(500);
  84.             Serial.print(".");
  85.      }
  86.  
  87.       Serial.println("");
  88.       Serial.println("WiFi connected");
  89.        Blynk.begin(auth, ssid, pass);
  90.  timer.setInterval(1000L, sendsen);
  91. }
  92.  
  93. void loop(){
  94.  
  95.  
  96.   warunek();
  97.   wyswietlacz();
  98.   sendsen();
  99.   checkUp();
  100.   checkDown();
  101.  
  102.  
  103.  
  104.    Blynk.run();
  105.   timer.run();
  106.   float temp = sensors.getTempCByIndex(0);
  107.                     if(timeStart2 - previousMillis2 >= t){
  108.                       previousMillis2 = timeStart2;
  109.                          if (client.connect(server,80))   //   "184.106.153.149" or api.thingspeak.com
  110.                       {  
  111.                            
  112.                              String postStr = apiKey;
  113.                              postStr +="&field1=";
  114.                              postStr += String(temp);
  115.                              postStr +="&field2=";
  116.                              postStr += String(x);
  117.                              postStr +="&field3=";
  118.                              postStr += String(CalcSt);  
  119.                                    
  120.                              postStr += "\r\n\r\n";
  121.  
  122.                              client.print("POST /update HTTP/1.1\n");
  123.                              client.print("Host: api.thingspeak.com\n");
  124.                              client.print("Connection: close\n");
  125.                              client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n");
  126.                              client.print("Content-Type: application/x-www-form-urlencoded\n");
  127.                              client.print("Content-Length: ");
  128.                              client.print(postStr.length());
  129.                              client.print("\n\n");
  130.                              client.print(postStr);
  131.  
  132.                        
  133.                              Serial.println("%. Send to Thingspeak.");
  134.                         }
  135.           client.stop();
  136.  
  137.           Serial.println("Waiting...");
  138.  
  139.   // thingspeak needs minimum 15 sec delay between updates
  140.                     }
  141. }
  142.     void sendsen(){
  143.      sensors.requestTemperatures();
  144.      float temp = sensors.getTempCByIndex(0);
  145.       Blynk.virtualWrite(V5, temp); //sending to Blynk
  146.       Blynk.virtualWrite(V6, x);
  147.       Blynk.virtualWrite(V4, CalcSt);
  148.     }
  149.  
  150.     BLYNK_WRITE(V1){
  151.        CalcSt = param.asInt();
  152.      
  153.  
  154.     }  
  155.  
  156.  
  157. void checkUp(){ // przycisk zliczajacy w gore
  158.   upbutt = digitalRead(Up); //przepisanie do zmiennej przycisku
  159.    if(upbutt !=uplastbutt){ // jeśli stan się zmienił, zwiększ licznik
  160.     if(upbutt == LOW){
  161.       bPress = true;
  162.       CalcSt ++; //dodanie liczby
  163.     }
  164.     delay(20);
  165.   }
  166.   uplastbutt = upbutt; // zapisanie ostatniego stanu
  167. }
  168.  
  169. void checkDown(){ // przycisk zliczajacy w dol
  170.   downbutt = digitalRead(Down); //przepisanie do zmiennej przycisku
  171.  if(downbutt != downlastbutt){
  172.     if(downbutt == LOW){
  173.       bPress=true;
  174.       CalcSt --;
  175.     }
  176.     delay(20);
  177.   }
  178.   downlastbutt = downbutt; // zapisanie ostatniego stanu
  179. }
  180. /*
  181.  void wyswietlacz(){
  182. float temp = sensors.getTempCByIndex(0);
  183.   lcd.setCursor(0,1);
  184.   lcd.print("T:");
  185.   lcd.print(sensors.getTempCByIndex(0)  );
  186.   Serial.println(temp);  
  187.    lcd.setCursor(0,0);
  188.    lcd.print("Ustaw temp: ");
  189.    lcd.print(CalcSt);
  190.    }
  191.  
  192.  
  193.  
  194.  
  195. void warunek() { //włącza bądź wyłącza 230v
  196.    sensors.requestTemperatures();
  197.    float temp = sensors.getTempCByIndex(0);
  198.  
  199.   if(temp < CalcSt){ // jeśli temp jest mniejsza od ustaw temp
  200.    digitalWrite(przekaznik, HIGH); // to ma włączyć
  201.  
  202.   }else if(temp > CalcSt+5){ // jeśli temp jest większa niż ustaw temp +5
  203.     digitalWrite(przekaznik, LOW); // to ma wyłączyć
  204.   }
  205.  
  206.   while(isCounting && millis() - timeStart >= (21600*1000)){ // wyłącznik czasowy
  207.     isCounting = false;
  208.     for(;;){
  209.    digitalWrite(przekaznik, LOW);
  210.     }
  211.     Serial.println("Off");
  212.   }
  213.  
  214.       for(int i = 0; i<(temp>CalcSt);i++){
  215.   if(isCounting &&((millis()-timeStart)>=(i*1000))){
  216.     isCounting = false;
  217.      x = (millis()/6000);
  218.  
  219.      lcd.setCursor(11,1);
  220.      lcd.print(x);
  221.    
  222.     }
  223.   }
  224.   */
  225.  
  226.  
  227. }