Pleiadian

Temperature to website using the Arduino Mega

May 1st, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.32 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <ESP8266_Lib.h>
  3. #include "WiFiEsp.h"
  4. #include "SHTSensor.h"
  5. SHTSensor sht;
  6. WiFiEspClient client;
  7.  
  8.  
  9. char ssid[] = "WiFi";               // SSID
  10. char pass[] = "Pass";               // wifipassord
  11.  
  12.  
  13. // your network password
  14. int status = WL_IDLE_STATUS;
  15. char server[] = "website";
  16. #define EspSerial Serial1
  17. #define ESP8266_BAUD 9600
  18.  
  19. ESP8266 wifi(&EspSerial);
  20. long sendetid;
  21. long rssi;
  22. double rssidouble;
  23. double temp;
  24. double fuktighet;
  25.  
  26. void setup()
  27. {
  28.  
  29.   Wire.begin();
  30.   Serial.begin(9600);
  31.   // initialize serial for ESP module
  32.   Serial1.begin(9600);
  33.   // initialize ESP module
  34.   WiFi.init(&Serial1);
  35.   delay(1600);
  36.  
  37.   // check for the presence of the shield
  38.   if (WiFi.status() == WL_NO_SHIELD) {
  39.     Serial.println("WiFi shield not present");
  40.     // don't continue
  41.     while (true);
  42.   }
  43.  
  44.   // attempt to connect to WiFi network
  45.  
  46.   while ( status != WL_CONNECTED) {
  47.     Serial.print("Attempting to connect to WPA SSID: ");
  48.     Serial.println(ssid);
  49.     // Connect to WPA/WPA2 network
  50.     status = WiFi.begin(ssid, pass);
  51.   }
  52.  
  53.   Serial.println("You're connected to the network");
  54.  
  55.  
  56.  
  57.   if (sht.init()) {
  58.     Serial.print("init(): success\n");
  59.   } else {
  60.     Serial.print("init(): failed\n");
  61.   }
  62.  
  63.  
  64.  
  65.   sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM);
  66.   printWifiStatus();
  67.   sendetid = millis();
  68.   sjekker();
  69.   sender();
  70. }
  71.  
  72. void loop()
  73. {
  74.  
  75.  
  76.  
  77.   if ((millis() - sendetid) > 10350L) {
  78.     sjekker();
  79.     sender();
  80.     rssi = WiFi.RSSI();
  81.     rssidouble = (0 - rssi) / 10.0;
  82.   }
  83.  
  84. }
  85.  
  86. // this method makes a HTTP connection to the server
  87. void sender()
  88. {
  89.   sendetid = millis();
  90.   Serial.println("Skal sende..");
  91.  
  92.   client.stop();
  93.   delay(400);
  94.  
  95.  
  96.   if (client.connect(server, 80)) {
  97.  
  98.  
  99.  
  100.  
  101.  
  102.     // if there's a successful connection
  103.     Serial.println("Connecting...");
  104.  
  105.     // send the HTTP PUT request
  106.     client.print(F("GET /Sensors/write_data.php"));
  107.     client.print("?temperatur=");
  108.     client.print(temp);
  109.     client.print("&fuktighet=");
  110.     client.print(fuktighet);
  111.     client.print("&rssi=");
  112.     client.print(rssi);
  113.     client.println(F(" HTTP/1.0"));
  114.     client.println(F("Host: website"));
  115.     client.println("Connection: close");
  116.     client.println();
  117.     client.stop();
  118.     // note the time that the connection was made
  119.  
  120.   }
  121.   else {
  122.     // if you couldn't make a connection
  123.     Serial.println("Connection failed");
  124.   }
  125.   Serial.println("Har sendt!");
  126. }
  127.  
  128.  
  129.  
  130.  
  131.  
  132.  
  133. void printWifiStatus()
  134. {
  135.   // print the SSID of the network you're attached to
  136.   Serial.print("SSID: ");
  137.   Serial.println(WiFi.SSID());
  138.  
  139.   // print your WiFi shield's IP address
  140.   IPAddress ip = WiFi.localIP();
  141.   Serial.print("IP Address: ");
  142.   Serial.println(ip);
  143.  
  144.   // print the received signal strength
  145.   rssi = WiFi.RSSI();
  146.   rssidouble = (0 - rssi) / 10.0;
  147.   Serial.print("Signal strength (RSSI):");
  148.   Serial.print(rssi);
  149.   Serial.println(" dBm");
  150. }
  151.  
  152.  
  153. void sjekker()
  154. {
  155.  
  156.   if (sht.readSample()) {
  157.  
  158.     temp = sht.getTemperature();  // get temp from SHT
  159.     fuktighet = sht.getHumidity(); // get fukt from SHT
  160.  
  161.   } else {
  162.     Serial.print("Error in readSample()\n");
  163.   }
  164.   delay(500);
  165.  
  166.  
  167.   Serial.print("Temperatur = ");
  168.   Serial.print(temp);
  169.   Serial.print(", Fuktighet = ");
  170.   Serial.print(fuktighet);
  171.   Serial.print(", RSSI = ");
  172.   Serial.println(rssi);
  173. }
Advertisement
Add Comment
Please, Sign In to add comment