Advertisement
Guest User

Untitled

a guest
Jan 13th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.93 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <WiFiClient.h>
  3. #include <WebServer.h>
  4. #include <Wire.h>
  5. #include <SPI.h>
  6. #include <TinyDHT.h>
  7. #include <ESP.h>
  8. #include "CORE_BME280_SPI.h"
  9. #include "OneWire.h"
  10. #define ONEWIRE_PIN           16    // OneWire Dallas sensors are connected to this pin
  11. #define MAX_NUMBER_OF_SENSORS 1    // maximum number of Dallas sensors
  12. OneWire  ds( ONEWIRE_PIN );
  13. struct sensorStruct
  14. {
  15.   byte addr[8];
  16.   float temp;
  17.   String name;
  18. } sensor[MAX_NUMBER_OF_SENSORS];
  19.  
  20. byte numberOfFoundSensors;
  21. int shtcount = 0;
  22.  
  23. //BME280
  24. //#define SENSE_SCK 5;// Set Serial Clock
  25. //#define SENSE_MISO 2;// Set Serial Data Out
  26. //#define SENSE_MOSI 4;// Set Serial Data In
  27. //#define SENSE_CS 19 ;// Set Chip Select
  28. BME280_SPI bme(19, 4, 2, 5);
  29.  
  30.  
  31. // SHT25 I2C address is 0x40(64)
  32. #define sht 0x40
  33.  
  34. //DHT11
  35. #define DHTPIN 17
  36. DHT dht11(DHTPIN, DHT11, 15);
  37.  
  38. //Multiplay_9014_2
  39. //wolfenstein
  40. //iPhone (Adam)
  41. //12345678
  42. WebServer webServer(80);
  43. const char* ssid1 = "Multiplay_9014_2";
  44. const char* password1 = "wolfenstein";
  45. String apiKey = "MOBU2YTSKA0KEOY3";
  46. const char* server = "api.thingspeak.com";
  47. bool connectedWifi = false;
  48.  
  49. WiFiClient client;
  50.  
  51. //main page
  52. void handleLogin() {
  53.   //String msg;
  54.   if (webServer.hasArg("USERNAME") && webServer.hasArg("PASSWORD")) {
  55.     //if (webServer.arg("USERNAME") == "admin" &&  webServer.arg("PASSWORD") == "12345678" ){
  56.     webServer.sendHeader("Location", "/");
  57.     webServer.sendHeader("Cache-Control", "no-cache");
  58.     webServer.sendHeader("Set-Cookie", "ESPSESSIONID=1");
  59.     webServer.send(301);
  60.     Serial.println("Log in Successful");
  61.     String ssidL = webServer.arg("SSID");
  62.     String passwordL = webServer.arg("PASSWORD");
  63.     apiKey = webServer.arg("API");
  64.     ssid1 = ssidL.c_str();
  65.     password1 = passwordL.c_str();
  66.     Serial.println("Data recived ");
  67.     Serial.println(ssid1);
  68.     Serial.println(password1);
  69.     Serial.println(apiKey);
  70.     WiFi.mode(WIFI_STA);
  71.     WiFi.begin(ssid1, password1);
  72.     while ( WiFi.status() != WL_CONNECTED )
  73.     {
  74.       vTaskDelay( 250 / portTICK_PERIOD_MS );
  75.       Serial.print( "." );
  76.     }
  77.     Serial.println("Connected to Wifi" );
  78.     connectedWifi = true;
  79.     return;
  80.  
  81.     //msg = "Wrong username/password! try again.";
  82.     //Serial.println("Log in Failed");
  83.   }
  84.   String content = "<html><body><form action='/' method='POST'>Put wifi creditials here and thingSprark Api<br>";
  85.   content += "Ssid:<input type='text' name='SSID' placeholder='ssid'><br>";
  86.   content += "User:<input type='text' name='USERNAME' placeholder='username'><br>";
  87.   content += "Password:<input type='password' name='PASSWORD' placeholder='password'><br>";
  88.   content += "Api:<input type='text' name='API' placeholder='api'><br>";
  89.   content += "<input type='submit' name='SUBMIT' value='Submit'></form><br>";
  90.   //content += "<input type='submit' name='SUBMIT' value='Submit'></form>" + msg + "<br>";
  91.   content += "You also can go <a href='/inline'>here</a></body></html>";
  92.   webServer.send(200, "text/html", content);
  93.  
  94. }
  95.  
  96. //no need authentification
  97. void handleNotFound() {
  98.   String message = "Page Not Found\n\n";
  99.   webServer.send(404, "text/plain", message);
  100. }
  101. const char *ssid = "ESP32ap";
  102. const char *password = "12345678";
  103.  
  104. void setup() {
  105.   Wire.begin();
  106.   Serial.begin(115200);
  107.   //d18b20
  108.   xTaskCreatePinnedToCore(
  109.     tempTask,                       /* Function to implement the task */
  110.     "tempTask ",                    /* Name of the task */
  111.     4000,                           /* Stack size in words */
  112.     NULL,                           /* Task input parameter */
  113.     5,                              /* Priority of the task */
  114.     NULL,                           /* Task handle. */
  115.     1);                             /* Core where the task should run */
  116.   //sht
  117.   Wire.beginTransmission(sht);
  118.   Wire.endTransmission();
  119.   delay(300);
  120.   //dht
  121.   dht11.begin();
  122.   //bme
  123.   if (!bme.begin()) {
  124.     Serial.println("Error: BME280 sensor, Check Wiring > ");
  125.     while (1);
  126.   }
  127.  
  128.   bme.setTempCal(-1);//Set Sensor was reading high so offset by 1 degree C
  129.  
  130.   //wifi
  131.   Serial.println();
  132.   Serial.print("Configuring access point...");
  133.   /* You can remove the password parameter if you want the AP to be open. */
  134.   //WiFi.softAP(ssid, password);
  135.  
  136.   //IPAddress myIP = WiFi.softAPIP();
  137.   // Serial.print("AP IP address: ");
  138.   //Serial.println(myIP);
  139.  
  140.  
  141.   //webServer.on("/", handleLogin);
  142.  
  143.  
  144.   //webServer.onNotFound(handleNotFound);
  145.   //webServer.begin();
  146.   //Serial.println("HTTP server started");
  147.   WiFi.begin(ssid1, password1);
  148.   while ( WiFi.status() != WL_CONNECTED )
  149.   {
  150.     vTaskDelay( 250 / portTICK_PERIOD_MS );
  151.     Serial.print( "." );
  152.   }
  153.  
  154. }
  155. // webServer.handleClient();
  156. // if(connectedWifi)
  157.  
  158. void loop()
  159. {
  160.   webServer.handleClient();
  161.   if (WL_CONNECTED) {
  162.  
  163.     if (client.connect(server, 80)) { // "184.106.153.149" or api.thingspeak.com
  164.       float t;
  165.       //d18b20
  166.       if ( numberOfFoundSensors ) {
  167.  
  168.         t = sensor[0].temp / 16.0 ;
  169.  
  170.         Serial.print("Temperature d18b20: ");
  171.         Serial.print(t);
  172.       }
  173.       else
  174.       {
  175.         Serial.println( "No Dallas sensors." );
  176.       }
  177.  
  178.       //read dht11
  179.       float hum11 = dht11.readHumidity();
  180.       float temp11 = dht11.readTemperature();
  181.       if (isnan(hum11)  ) {
  182.         hum11 = 255;
  183.       }
  184.       if (isnan(temp11)) {
  185.         temp11 = 255;
  186.       }
  187.       Serial.print("Temperature dht11: ");
  188.       Serial.print(temp11);
  189.       Serial.print("Humidity dht11: ");
  190.       Serial.print(hum11);
  191.       //read bme
  192.       bme.readSensor();
  193.  
  194.       float humBme = bme.getHumidity();
  195.       float tempBme = bme.getTemperature_C();
  196.       Serial.print("Temperature Bme: ");
  197.       Serial.print(tempBme);
  198.       Serial.print("Humidity Bme: ");
  199.       Serial.print(humBme);
  200.  
  201.       //sht 25
  202.       unsigned int data[2];
  203.  
  204.       Wire.beginTransmission(sht);
  205.       //Send humidity measurement command
  206.       Wire.write(0xF5);
  207.       Wire.endTransmission();
  208.       delay(500);
  209.  
  210.       // Request 2 bytes of data
  211.       Wire.requestFrom(sht, 2);
  212.       // Read 2 bytes of data to get humidity
  213.       if (Wire.available() == 2)
  214.       {
  215.         data[0] = Wire.read();
  216.         data[1] = Wire.read();
  217.       }
  218.  
  219.       // Convert the data
  220.       float humidity  = ((data[0] * 256.0) + data[1]);
  221.       humidity = ((125 * humidity) / 65536.0) - 6;
  222.  
  223.       Wire.beginTransmission(sht);
  224.       // Send temperature measurement command
  225.       Wire.write(0xF3);
  226.       Wire.endTransmission();
  227.       delay(500);
  228.  
  229.       // Request 2 bytes of data
  230.       Wire.requestFrom(sht, 2);
  231.  
  232.       // Read 2 bytes of data for temperature
  233.       if (Wire.available() == 2)
  234.       {
  235.         data[0] = Wire.read();
  236.         data[1] = Wire.read();
  237.       }
  238.  
  239.       // Convert the data
  240.       float temp  = ((data[0] * 256.0) + data[1]);
  241.       float celsTemp = ((175.72 * temp) / 65536.0) - 46.85;
  242.       Serial.print("Temperature sht: ");
  243.       Serial.print(celsTemp);
  244.       Serial.print("Humidity sht: ");
  245.       Serial.print(humidity);
  246.  
  247.       /*if (shtcount == 2) {
  248.         shtcount = 0;
  249.         client.stop();
  250.         WiFi.disconnect();
  251.         ESP.reset();
  252.       }
  253.       if (celsTemp > 500 || humidity > 500) {
  254.         shtcount++;
  255.       }
  256.       if (celsTemp < 500 || humidity < 500) {
  257.         shtcount = 0;
  258.       }*/
  259.  
  260.       String postStr1 = apiKey;
  261.       postStr1 += "&field1=";
  262.       postStr1 += String(temp11);
  263.       postStr1 += "\r\n\r\n";
  264.       String postStr2 = apiKey;
  265.       postStr2 += "&field2=";
  266.       postStr2 += String(hum11);
  267.       postStr2 += "\r\n\r\n";
  268.       String postStr3 = apiKey;
  269.       postStr3 += "&field3=";
  270.       postStr3 += String(t);
  271.       postStr3 += "\r\n\r\n";
  272.       String postStr4 = apiKey;
  273.       postStr4 += "&field4=";
  274.       postStr4 += String(celsTemp);
  275.       postStr4 += "\r\n\r\n";
  276.       String postStr5 = apiKey;
  277.       postStr5 += "&field5=";
  278.       postStr5 += String(humidity);
  279.       postStr5 += "\r\n\r\n";
  280.       String postStr6 = apiKey;
  281.       postStr6 += "&field6=";
  282.       postStr6 += String(tempBme);
  283.       postStr6 += "\r\n\r\n";
  284.       String postStr7 = apiKey;
  285.       postStr7 += "&field7=";
  286.       postStr7 += String(humBme);
  287.       postStr7 += "\r\n\r\n";
  288.       String postStr8 = apiKey;
  289.       postStr8 += "&field7=";
  290.       postStr8 += String(humBme);
  291.       postStr8 += "\r\n\r\n";
  292.  
  293.       client.print("POST /update HTTP/1.1\n");
  294.       client.print("Host: api.thingspeak.com\n");
  295.       client.print("Connection: close\n");
  296.       client.print("X-THINGSPEAKAPIKEY: " + apiKey + "\n");
  297.       client.print("Content-Type: application/x-www-form-urlencoded\n");
  298.       client.print("Content-Length: ");
  299.       client.print(postStr1.length() + postStr2.length() + postStr3.length() + postStr4.length() + postStr5.length() + postStr6.length() + postStr7.length() + postStr8.length());
  300.       client.print("\n\n");
  301.       client.print(postStr1);
  302.       client.print("\n\n");
  303.       client.print(postStr2);
  304.       client.print("\n\n");
  305.       client.print(postStr3);
  306.       client.print("\n\n");
  307.       client.print(postStr4);
  308.       client.print("\n\n");
  309.       client.print(postStr5);
  310.       client.print("\n\n");
  311.       client.print(postStr6);
  312.       client.print("\n\n");
  313.       client.print(postStr7);
  314.       client.print("\n\n");
  315.       client.print(postStr8);
  316.       client.print("\n\n");
  317.  
  318.       Serial.println("% send to Thingspeak");
  319.     }
  320.     client.stop();
  321.  
  322.     Serial.println("Waiting?");
  323.     // thingspeak needs minimum 15 sec delay between updates
  324.     delay(20000);
  325.  
  326.  
  327.     //vTaskDelay( 500 / portTICK_PERIOD_MS );
  328.   }
  329.   else {
  330.     ESP.restart() ;
  331.   }
  332. }
  333.  
  334. void tempTask( void * pvParameters )
  335. {
  336.   numberOfFoundSensors = 0;
  337.   byte currentAddr[8];
  338.   while ( ds.search( currentAddr ) && numberOfFoundSensors < MAX_NUMBER_OF_SENSORS )
  339.   {
  340.     //Serial.write( "Sensor "); Serial.print( counter ); Serial.print( ":" );
  341.     for ( byte i = 0; i < 8; i++)
  342.     {
  343.       //Serial.write(' ');
  344.       //Serial.print( currentAddr[i], HEX );
  345.       sensor[numberOfFoundSensors].addr[i] = currentAddr[i];
  346.     }
  347.     //sensor[numberOfFoundSensors].name = 'T ' + char( numberOfFoundSensors );
  348.     numberOfFoundSensors++;
  349.   }
  350.   Serial.printf( "%i Dallas sensors found.\n", numberOfFoundSensors );
  351.  
  352.   if ( !numberOfFoundSensors )
  353.   {
  354.     vTaskDelete( NULL );
  355.   }
  356.  
  357.   /* main temptask loop */
  358.  
  359.   while (1)
  360.   {
  361.     for ( byte thisSensor = 0; thisSensor < numberOfFoundSensors; thisSensor++)
  362.     {
  363.       ds.reset();
  364.       ds.select( sensor[thisSensor].addr );
  365.       ds.write( 0x44, 0);        // start conversion, with parasite power off at the end
  366.     }
  367.  
  368.     vTaskDelay( 750 / portTICK_PERIOD_MS); //wait for conversion ready
  369.  
  370.     for ( byte thisSensor = 0; thisSensor < numberOfFoundSensors; thisSensor++)
  371.     {
  372.       byte data[12];
  373.       ds.reset();
  374.       ds.select( sensor[thisSensor].addr );
  375.       ds.write( 0xBE );         // Read Scratchpad
  376.  
  377.       //Serial.print( "Sensor " );Serial.print( thisSensor ); Serial.print("  Data = ");
  378.       //Serial.println( present, HEX );
  379.       //Serial.print(" ");
  380.       for ( byte i = 0; i < 9; i++)
  381.       { // we need 9 bytes
  382.         data[i] = ds.read(  );
  383.         //Serial.print(data[i], HEX);
  384.         //Serial.print(" ");
  385.       }
  386.       //Serial.println();
  387.  
  388.       byte type_s;
  389.       // the first ROM byte indicates which chip
  390.       switch ( sensor[thisSensor].addr[0] )
  391.       {
  392.         case 0x10:
  393.           //Serial.println("  Chip = DS18S20");  // or old DS1820
  394.           type_s = 1;
  395.           break;
  396.         case 0x28:
  397.           //Serial.println("  Chip = DS18B20");
  398.           type_s = 0;
  399.           break;
  400.         case 0x22:
  401.           //Serial.println("  Chip = DS1822");
  402.           type_s = 0;
  403.           break;
  404.         default:
  405. #ifdef SHOW_DALLAS_ERROR
  406.           Serial.println("Device is not a DS18x20 family device.");
  407. #endif
  408.           return;
  409.       }
  410.  
  411.       int16_t raw;
  412.       if ( OneWire::crc8(data, 8) != data[8])
  413.       {
  414. #ifdef SHOW_DALLAS_ERROR
  415.         // CRC of temperature reading indicates an error, so we print a error message and discard this reading
  416.         Serial.print( millis() / 1000.0 ); Serial.print( " - CRC error from device " ); Serial.println( thisSensor );
  417. #endif
  418.       }
  419.       else
  420.       {
  421.         raw = (data[1] << 8) | data[0];
  422.         if (type_s)
  423.         {
  424.           raw = raw << 3; // 9 bit resolution default
  425.           if (data[7] == 0x10)
  426.           {
  427.             // "count remain" gives full 12 bit resolution
  428.             raw = (raw & 0xFFF0) + 12 - data[6];
  429.           }
  430.         }
  431.         else
  432.         {
  433.           byte cfg = (data[4] & 0x60);
  434.           // at lower res, the low bits are undefined, so let's zero them
  435.           if (cfg == 0x00) raw = raw & ~7;  // 9 bit resolution, 93.75 ms
  436.           else if (cfg == 0x20) raw = raw & ~3; // 10 bit res, 187.5 ms
  437.           else if (cfg == 0x40) raw = raw & ~1; // 11 bit res, 375 ms
  438.           //// default is 12 bit resolution, 750 ms conversion time
  439.         }
  440.         sensor[thisSensor].temp = raw;
  441.       }
  442.     }
  443.   }
  444. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement