/****************************************** I2C Adresses 0x38 Expander 1; Wind 0x39 Expander 2; Rain 0x48 ADS1115; Light, Moisture 0x4A ADS1015; Winddirection 0x57 ?? 0x68 RTC 0x76 BME280 ******************************************/ #include #include #include #include "RTClib.h" #include #include #include #include "Seeed_BME280.h" #include // WiFi const char* password = "abc123"; const char* ssid = "WLAN-123456"; //thingspeak String apiKey = "ApI-kEy"; const char* server = "api.thingspeak.com"; //RTC RTC_DS3231 rtc; char daysOfTheWeek[7][12] = {"Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"}; //Telegram #define BOTtoken "123:XXX" //token from Botfather #define BOTname "Name" #define BOTusername "Name_bot" TelegramBOT bot(BOTtoken, BOTname, BOTusername); int Bot_mtbs = 1000; //mean time between scan messages long Bot_lasttime; //last time messages' scan has been done bool Start = true; //ADS1115 Adafruit_ADS1115 ads1115; Adafruit_ADS1115 ads1015(0x4A); //BME280 BME280 bme280; //PCF8574 Expander #define expander1 0x38 // Adresse des PCF8574 Wind #define expander2 0x39 // Adresse des PCF8574 Regen #define bucket_size 0.5; #define anemometer_size 0.5; //Variables float ldr = 0; float soil = 0; float winddirection = 0; int winddegree = 1000; int windmin; int windmax; float ldr1 = 0; float soil1 = 0; int windcounter = 0; int time1s = 0; //Now Time Seconds int time1m = 0; //Now Time Minutes int time2s = 0; //Now Time Minutes int time2m = 0; int time1; int time2; int xtime; int windspeed = 0; int windspeedkmh = 0; int v = 0;//variable to calculate windchill int windchill = 0; float voltage; int wlancntr = 0; int zahl; static char windstr[8]; static char rainstr[8]; WiFiClient client; void setup() { pinMode(13, OUTPUT); //Rain Reset pinMode(12, OUTPUT); //Mosfet Serial.begin(115200); delay(10); digitalWrite(12, HIGH); delay(1000); Wire.begin(4, 5); rtc.begin(); DateTime now = rtc.now(); time1m = now.minute(); time1s = now.second(); delay(100); Serial.print(time1m); Serial.print(":"); Serial.print(time1s); WiFi.begin(ssid, password); time1=time1m*60+time1s; Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); wlancntr = 0; while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); wlancntr++; } Serial.println(""); Serial.println("WiFi connected"); while (!Serial); // for Leonardo/Micro/Zero if (! rtc.begin()) { Serial.println("Couldn't find RTC"); while (1); } else{ Serial.println("RTC Connected"); } // if (! rtc.isrunning()) { //Serial.println("RTC is NOT running!"); // } // following line sets the RTC to the date & time this sketch was compiled //rtc.adjust(DateTime(F(__DATE__), F(__TIME__))); // This line sets the RTC with an explicit date & time, for example to set // January 21, 2014 at 3am you would call: // rtc.adjust(DateTime(2014, 1, 21, 3, 0, 0)); Serial.println("Connect to Expander 1"); Wire.beginTransmission(expander1); Wire.write(B11111111); //Schreiben des Bytes. Jede Null stellt ein Bit an den Pins des PCF8574 dar (Auch möglich in Hex Schreibweise) Wire.endTransmission(); //Schließen der Verbindung Serial.println("Connect to Expander 2"); Wire.beginTransmission(expander2); Wire.write(B11111111); //Schreiben des Bytes. Jede Null stellt ein Bit an den Pins des PCF8574 dar (Auch möglich in Hex Schreibweise) Wire.endTransmission(); Serial.println("Connect to ADS1115"); ads1115.begin(); ads1015.begin(); if(!bme280.init()){ Serial.println("Device error!"); } Serial.println("Start loop"); delay(100); float h = bme280.getHumidity(); float t = bme280.getTemperature(); float p = bme280.getPressure()/100.0; Serial.println(h); Serial.println(t); Serial.println(p); int16_t ldr, soil, adc2, adc3; ldr = ads1115.readADC_SingleEnded(0); soil = ads1115.readADC_SingleEnded(1); adc2 = ads1115.readADC_SingleEnded(2);//defect adc3 = ads1115.readADC_SingleEnded(3); int16_t adc4, adc5, adc6, winddirection; adc4 = ads1015.readADC_SingleEnded(0); adc5 = ads1015.readADC_SingleEnded(1); adc6 = ads1015.readADC_SingleEnded(2); winddirection = ads1015.readADC_SingleEnded(3); Serial.println("ADS1015"); if(winddirection > 7800){ winddegree = map(winddirection, 7800, 14850, 10, 350); } else{ winddirection = 0; } Serial.println(winddirection); Serial.println(winddegree); ldr1 = map(ldr, 0, 16385, 0, 100); soil1 = map(soil, 0, 6807, 0, 100); Serial.print("Winddirection: "); Serial.println(winddirection); Serial.print("WInddegree: "); Serial.println(winddegree); Serial.print("LDR: "); Serial.println(ldr); Serial.print("Soil: "); Serial.println(soil); Wire.requestFrom(expander2, 1); zahl = Wire.read(); rainstr[8]; rainstr[7]=48+(zahl&1);zahl>>=1; rainstr[6]=48+(zahl&1);zahl>>=1; rainstr[5]=48+(zahl&1);zahl>>=1; rainstr[4]=48+(zahl&1);zahl>>=1; rainstr[3]=48+(zahl&1);zahl>>=1; rainstr[2]=48+(zahl&1);zahl>>=1; rainstr[1]=48+(zahl&1);zahl>>=1; rainstr[0]=48+(zahl&1); Serial.println(" "); Serial.println(rainstr[7]); Serial.println(rainstr[6]); Serial.println(rainstr[5]); Serial.println(rainstr[4]); Serial.println(rainstr[3]); Serial.println(rainstr[2]); Serial.println(rainstr[1]); Serial.println(rainstr[0]); int x7 = rainstr[7]-48; int x6 = rainstr[6]-48; int x5 = rainstr[5]-48; int x4 = rainstr[4]-48; int x3 = rainstr[3]-48; int x2 = rainstr[2]-48; int x1 = rainstr[1]-48; int x0 = rainstr[0]-48; int raincounter = x7+x6*2+x5*4+x4*8+x3*16+x2*32+x1*64+x0*128; int rain = raincounter * bucket_size; Serial.print(" "); Serial.print(rain); Serial.print("mm"); digitalWrite(13, HIGH); delay(50); digitalWrite(13, LOW); Wire.requestFrom(expander1, 1); zahl = Wire.read(); windstr[8]; windstr[7]=48+(zahl&1);zahl>>=1; windstr[6]=48+(zahl&1);zahl>>=1; windstr[5]=48+(zahl&1);zahl>>=1; windstr[4]=48+(zahl&1);zahl>>=1; windstr[3]=48+(zahl&1);zahl>>=1; windstr[2]=48+(zahl&1);zahl>>=1; windstr[1]=48+(zahl&1);zahl>>=1; windstr[0]=48+(zahl&1); Serial.println(" "); Serial.println(windstr[7]); Serial.println(windstr[6]); Serial.println(windstr[5]); Serial.println(windstr[4]); Serial.println(windstr[3]); Serial.println(windstr[2]); Serial.println(windstr[1]); Serial.println(windstr[0]); x7 = windstr[7]-48; x6 = windstr[6]-48; x5 = windstr[5]-48; x4 = windstr[4]-48; x3 = windstr[3]-48; x2 = windstr[2]-48; x1 = windstr[1]-48; x0 = windstr[0]-48; int windcounter = x7+x6*2+x5*4+x4*8+x3*16+x2*32+x1*64+x0*128; int wind = windcounter*anemometer_size; // =revoluitons*anemometer_size per 20 seconds -> windspeed in meter per second DateTime second = rtc.now(); time2m = second.minute(); time2s = second.second(); if (time2m == 00){ time2m = 60; } time2=time2m*60; time2=time2+time2s; Serial.println(time1); Serial.println(time2); xtime=time2-time1; Serial.println(xtime); wind = wind / xtime; Serial.print(" "); Serial.print(wind); Serial.print("m/s"); Serial.println(""); voltage = analogRead(0); voltage = voltage*11; voltage = voltage*0.0009765625; Serial.print(voltage); Serial.print("V"); if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com Serial.println("Connect to thingspeak"); String postStr = apiKey; postStr +="&field1="; postStr += String(t); postStr +="&field2="; postStr += String(h); postStr +="&field3="; postStr +=String(rain); postStr +="&field4="; postStr +=String(wind); postStr +="&field5="; postStr += String(soil); postStr +="&field6="; postStr += String(ldr); postStr +="&field7="; postStr += String(voltage); postStr +="&field8="; postStr += String(p); postStr += "\r\n\r\n"; Serial.println(postStr); Serial.println("Send data"); client.print("POST /update HTTP/1.1\n"); client.print("Host: api.thingspeak.com\n"); client.print("Connection: close\n"); client.print("X-THINGSPEAKAPIKEY: "+apiKey+"\n"); client.print("Content-Type: application/x-www-form-urlencoded\n"); client.print("Content-Length: "); client.print(postStr.length()); client.print("\n\n"); client.print(postStr); } client.stop(); Serial.println("connection Closed"); //Print data to php String data = "http://path.to/your/wetterstation/dataCollector.php?"; data += "temperature="; data += String(t); data += "&humidity="; data += String(h); data += "&pressure="; data += String(p); data += "&windspeed="; data += String(wind); data += "&winddirection="; data += String(winddegree); data += "&windchill="; data += String (windchill); data += "&rain="; data += String (rain); data += "&batvoltage="; data += String (voltage); data += "&soil="; data += String(soil); data += "&ldr"; data += String(ldr); Serial.println(data); //Send GET-Command, SRC: https://techtutorialsx.com/2016/07/17/esp8266-http-get-requests/, php-sketch by https://www.hackster.io/detox/send-esp8266-data-to-your-webpage-no-at-commands-7ebfec HTTPClient http; //Declare an object of class HTTPClient http.begin(data); //Specify request destination int httpCode = http.GET(); //Send the request if (httpCode > 0) { //Check the returning code String payload = http.getString(); //Get the request response payload Serial.println(payload); //Print the response payload } http.end(); //Close connection delay(2000); } void loop() { Serial.println("Waiting"); digitalWrite(12, LOW); /* bool Start = true; bot.getUpdates(bot.message[0][1]); for (int i = 1; i < bot.message[0][0].toInt() + 1; i++){ bot.message[i][5]=bot.message[i][5].substring(1,bot.message[i][5].length()); Serial.println(bot.message[i][5]); if(bot.message[i][5] == "update"){ String postTelegram = String(t); postTelegram += "°C, "; //postTelegram += String(h); //postTelegram += ""; //delay(10); //postTelegram += "Percent Humidity "; postTelegram += "and "; postTelegram += String(windchill); postTelegram += "°C felt temperature"; Serial.println(postTelegram); bot.sendMessage(bot.message[i][4], String(postTelegram),""); } else{ Serial.println("not update"); } if (bot.message[i][5] == "start") { String welcome = "Welcome to Universal Arduino Telegram Bot library.\n"; bot.sendMessage(bot.message[i][4], welcome, ""); Start = true; } } for (int i = 1; i < bot.message[0][0].toInt() + 1; i++) { bot.sendMessage(bot.message[i][4], bot.message[i][5], ""); } bot.message[0][0] = ""; // All messages have been replied - reset new messages */ // thingspeak needs minimum 15 sec delay between updates ESP.deepSleep(1200000000, WAKE_RF_DEFAULT);//1000000=1s delay(100); }