Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <WebServer.h>
- #include <Wire.h>
- #include <SPI.h>
- #include <DHT.h>
- #include <ESP.h>
- #include "CORE_BME280_SPI.h"
- #include "OneWire.h"
- #define ONEWIRE_PIN 16 // OneWire Dallas sensors are connected to this pin
- #define MAX_NUMBER_OF_SENSORS 1 // maximum number of Dallas sensors
- OneWire ds( ONEWIRE_PIN );
- struct sensorStruct
- {
- byte addr[8];
- float temp;
- String name;
- } sensor[MAX_NUMBER_OF_SENSORS];
- byte numberOfFoundSensors;
- int shtcount=0;
- //BME280
- //#define SENSE_SCK 5;// Set Serial Clock
- //#define SENSE_MISO 2;// Set Serial Data Out
- //#define SENSE_MOSI 4;// Set Serial Data In
- //#define SENSE_CS 19 ;// Set Chip Select
- BME280_SPI bme(19,4,2,5);
- // SHT25 I2C address is 0x40(64)
- #define sht 0x40
- //DHT11
- #define DHTPIN 17
- DHT dht11(DHTPIN,DHT11,15);
- WebServer webServer(80);
- const char* ssid1 ="Multiplay_9014_2";
- const char* password1 ="wolfenstein";
- String apiKey ="MOBU2YTSKA0KEOY3";
- const char* server = "api.thingspeak.com";
- bool connectedWifi = false;
- WiFiClient client;
- //main page
- void handleLogin(){
- //String msg;
- if (webServer.hasArg("USERNAME") && webServer.hasArg("PASSWORD")){
- //if (webServer.arg("USERNAME") == "admin" && webServer.arg("PASSWORD") == "12345678" ){
- webServer.sendHeader("Location","/");
- webServer.sendHeader("Cache-Control","no-cache");
- webServer.sendHeader("Set-Cookie","ESPSESSIONID=1");
- webServer.send(301);
- Serial.println("Log in Successful");
- String ssidL = webServer.arg("SSID");
- String passwordL = webServer.arg("PASSWORD");
- apiKey = webServer.arg("API");
- ssid1 = ssidL.c_str();
- password1 =passwordL.c_str();
- Serial.println("Data recived ");
- Serial.println(ssid1);
- Serial.println(password1);
- Serial.println(apiKey);
- WiFi.mode(WIFI_STA);
- WiFi.begin(ssid1, password1);
- while ( WiFi.status() != WL_CONNECTED )
- {
- vTaskDelay( 250 /portTICK_PERIOD_MS );
- Serial.print( "." );
- }
- Serial.println("Connected to Wifi" );
- connectedWifi=true;
- return;
- //msg = "Wrong username/password! try again.";
- //Serial.println("Log in Failed");
- }
- String content = "<html><body><form action='/' method='POST'>Put wifi creditials here and thingSprark Api<br>";
- content += "Ssid:<input type='text' name='SSID' placeholder='ssid'><br>";
- content += "User:<input type='text' name='USERNAME' placeholder='username'><br>";
- content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
- content += "Api:<input type='text' name='API' placeholder='api'><br>";
- content += "<input type='submit' name='SUBMIT' value='Submit'></form><br>";
- //content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
- content += "You also can go <a href='/inline'>here</a></body></html>";
- webServer.send(200, "text/html", content);
- }
- //no need authentification
- void handleNotFound(){
- String message = "Page Not Found\n\n";
- webServer.send(404, "text/plain", message);
- }
- const char *ssid = "ESP32ap";
- const char *password = "12345678";
- void setup() {
- Wire.begin();
- Serial.begin(115200);
- //d18b20
- xTaskCreatePinnedToCore(
- tempTask, /* Function to implement the task */
- "tempTask ", /* Name of the task */
- 4000, /* Stack size in words */
- NULL, /* Task input parameter */
- 5, /* Priority of the task */
- NULL, /* Task handle. */
- 1); /* Core where the task should run */
- //sht
- Wire.beginTransmission(sht);
- Wire.endTransmission();
- delay(300);
- //dht
- dht11.begin();
- //bme
- if (!bme.begin()) {
- Serial.println("Error: BME280 sensor, Check Wiring > ");
- while (1);
- }
- bme.setTempCal(-1);//Set Sensor was reading high so offset by 1 degree C
- //wifi
- Serial.println();
- Serial.print("Configuring access point...");
- /* You can remove the password parameter if you want the AP to be open. */
- //WiFi.softAP(ssid, password);
- //IPAddress myIP = WiFi.softAPIP();
- // Serial.print("AP IP address: ");
- //Serial.println(myIP);
- //webServer.on("/", handleLogin);
- //webServer.onNotFound(handleNotFound);
- //webServer.begin();
- //Serial.println("HTTP server started");
- WiFi.begin(ssid1, password1);
- while ( WiFi.status() != WL_CONNECTED )
- {
- vTaskDelay( 250 /portTICK_PERIOD_MS );
- Serial.print( "." );
- }
- }
- // webServer.handleClient();
- // if(connectedWifi)
- void loop()
- {
- webServer.handleClient();
- if(WL_CONNECTED){
- if (client.connect(server,80)) { // "184.106.153.149" or api.thingspeak.com
- float t;
- //d18b20
- if ( numberOfFoundSensors ){
- t =sensor[0].temp / 16.0 ;
- Serial.print("Temperature d18b20: ");
- Serial.print(t);
- }
- else
- {
- Serial.println( "No Dallas sensors." );
- }
- //read dht11
- float hum11 = dht11.readHumidity();
- float temp11 = dht11.readTemperature();
- if (isnan(hum11) ) {
- hum11=255;
- }
- if(isnan(temp11)){
- temp11=255;
- }
- Serial.print("Temperature dht11: ");
- Serial.print(temp11);
- Serial.print("Humidity dht11: ");
- Serial.print(hum11);
- //read bme
- bme.readSensor();
- float humBme=bme.getHumidity();
- float tempBme=bme.getTemperature_C();
- Serial.print("Temperature Bme: ");
- Serial.print(tempBme);
- Serial.print("Humidity Bme: ");
- Serial.print(humBme);
- //sht 25
- unsigned int data[2];
- Wire.beginTransmission(sht);
- //Send humidity measurement command
- Wire.write(0xF5);
- Wire.endTransmission();
- delay(500);
- // Request 2 bytes of data
- Wire.requestFrom(sht, 2);
- // Read 2 bytes of data to get humidity
- if(Wire.available() == 2)
- {
- data[0] = Wire.read();
- data[1] = Wire.read();
- }
- // Convert the data
- float humidity = ((data[0] * 256.0) + data[1]);
- humidity = ((125 * humidity) / 65536.0) - 6;
- Wire.beginTransmission(sht);
- // Send temperature measurement command
- Wire.write(0xF3);
- Wire.endTransmission();
- delay(500);
- // Request 2 bytes of data
- Wire.requestFrom(sht, 2);
- // Read 2 bytes of data for temperature
- if(Wire.available() == 2)
- {
- data[0] = Wire.read();
- data[1] = Wire.read();
- }
- // Convert the data
- float temp = ((data[0] * 256.0) + data[1]);
- float celsTemp = ((175.72 * temp) / 65536.0) - 46.85;
- Serial.print("Temperature sht: ");
- Serial.print(celsTemp);
- Serial.print("Humidity sht: ");
- Serial.print(humidity);
- if(shtcount==5){
- shtcount=0;
- ESP.restart() ;
- }
- if(celsTemp>500 || humidity>500){
- shtcount++;
- }
- else{
- shtcount=0;
- }
- String postStr1 = apiKey;
- postStr1 +="&field1=";
- postStr1 += String(temp11);
- postStr1 += "\r\n\r\n";
- String postStr2 = apiKey;
- postStr2 +="&field2=";
- postStr2 += String(hum11);
- postStr2 += "\r\n\r\n";
- String postStr3 = apiKey;
- postStr3 +="&field3=";
- postStr3 += String(t);
- postStr3 += "\r\n\r\n";
- String postStr4 = apiKey;
- postStr4 +="&field4=";
- postStr4 += String(celsTemp);
- postStr4 += "\r\n\r\n";
- String postStr5 = apiKey;
- postStr5 +="&field5=";
- postStr5 += String(humidity);
- postStr5 += "\r\n\r\n";
- String postStr6 = apiKey;
- postStr6 +="&field6=";
- postStr6 += String(tempBme);
- postStr6 += "\r\n\r\n";
- String postStr7 = apiKey;
- postStr7 +="&field7=";
- postStr7 += String(humBme);
- postStr7 += "\r\n\r\n";
- String postStr8 = apiKey;
- postStr8 +="&field7=";
- postStr8 += String(humBme);
- postStr8 += "\r\n\r\n";
- 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(postStr1.length()+postStr2.length()+postStr3.length()+postStr4.length()+postStr5.length()+postStr6.length()+postStr7.length()+postStr8.length());
- client.print("\n\n");
- client.print(postStr1);
- client.print("\n\n");
- client.print(postStr2);
- client.print("\n\n");
- client.print(postStr3);
- client.print("\n\n");
- client.print(postStr4);
- client.print("\n\n");
- client.print(postStr5);
- client.print("\n\n");
- client.print(postStr6);
- client.print("\n\n");
- client.print(postStr7);
- client.print("\n\n");
- client.print(postStr8);
- client.print("\n\n");
- Serial.println("% send to Thingspeak");
- }
- client.stop();
- Serial.println("Waiting?");
- // thingspeak needs minimum 15 sec delay between updates
- delay(20000);
- //vTaskDelay( 500 / portTICK_PERIOD_MS );
- }
- else{
- ESP.restart() ;
- }
- }
- void tempTask( void * pvParameters )
- {
- numberOfFoundSensors = 0;
- byte currentAddr[8];
- while ( ds.search( currentAddr ) && numberOfFoundSensors < MAX_NUMBER_OF_SENSORS )
- {
- //Serial.write( "Sensor "); Serial.print( counter ); Serial.print( ":" );
- for ( byte i = 0; i < 8; i++)
- {
- //Serial.write(' ');
- //Serial.print( currentAddr[i], HEX );
- sensor[numberOfFoundSensors].addr[i] = currentAddr[i];
- }
- //sensor[numberOfFoundSensors].name = 'T ' + char( numberOfFoundSensors );
- numberOfFoundSensors++;
- }
- Serial.printf( "%i Dallas sensors found.\n", numberOfFoundSensors );
- if ( !numberOfFoundSensors )
- {
- vTaskDelete( NULL );
- }
- /* main temptask loop */
- while (1)
- {
- for ( byte thisSensor = 0; thisSensor < numberOfFoundSensors; thisSensor++)
- {
- ds.reset();
- ds.select( sensor[thisSensor].addr );
- ds.write( 0x44, 0); // start conversion, with parasite power off at the end
- }
- vTaskDelay( 750 / portTICK_PERIOD_MS); //wait for conversion ready
- for ( byte thisSensor = 0; thisSensor < numberOfFoundSensors; thisSensor++)
- {
- byte data[12];
- ds.reset();
- ds.select( sensor[thisSensor].addr );
- ds.write( 0xBE ); // Read Scratchpad
- //Serial.print( "Sensor " );Serial.print( thisSensor ); Serial.print(" Data = ");
- //Serial.println( present, HEX );
- //Serial.print(" ");
- for ( byte i = 0; i < 9; i++)
- { // we need 9 bytes
- data[i] = ds.read( );
- //Serial.print(data[i], HEX);
- //Serial.print(" ");
- }
- //Serial.println();
- byte type_s;
- // the first ROM byte indicates which chip
- switch ( sensor[thisSensor].addr[0] )
- {
- case 0x10:
- //Serial.println(" Chip = DS18S20"); // or old DS1820
- type_s = 1;
- break;
- case 0x28:
- //Serial.println(" Chip = DS18B20");
- type_s = 0;
- break;
- case 0x22:
- //Serial.println(" Chip = DS1822");
- type_s = 0;
- break;
- default:
- #ifdef SHOW_DALLAS_ERROR
- Serial.println("Device is not a DS18x20 family device.");
- #endif
- return;
- }
- int16_t raw;
- if ( OneWire::crc8(data, 8) != data[8])
- {
- #ifdef SHOW_DALLAS_ERROR
- // CRC of temperature reading indicates an error, so we print a error message and discard this reading
- Serial.print( millis() / 1000.0 ); Serial.print( " - CRC error from device " ); Serial.println( thisSensor );
- #endif
- }
- else
- {
- raw = (data[1] << 8) | data[0];
- if (type_s)
- {
- raw = raw << 3; // 9 bit resolution default
- if (data[7] == 0x10)
- {
- // "count remain" gives full 12 bit resolution
- raw = (raw & 0xFFF0) + 12 - data[6];
- }
- }
- else
- {
- byte cfg = (data[4] & 0x60);
- // at lower res, the low bits are undefined, so let's zero them
- if (cfg == 0x00) raw = raw & ~7; // 9 bit resolution, 93.75 ms
- else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
- else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
- //// default is 12 bit resolution, 750 ms conversion time
- }
- sensor[thisSensor].temp = raw;
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement