Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Wire.h>
- #include <ESP8266_Lib.h>
- #include "WiFiEsp.h"
- #include "SHTSensor.h"
- SHTSensor sht;
- WiFiEspClient client;
- char ssid[] = "WiFi"; // SSID
- char pass[] = "Pass"; // wifipassord
- // your network password
- int status = WL_IDLE_STATUS;
- char server[] = "website";
- #define EspSerial Serial1
- #define ESP8266_BAUD 9600
- ESP8266 wifi(&EspSerial);
- long sendetid;
- long rssi;
- double rssidouble;
- double temp;
- double fuktighet;
- void setup()
- {
- Wire.begin();
- Serial.begin(9600);
- // initialize serial for ESP module
- Serial1.begin(9600);
- // initialize ESP module
- WiFi.init(&Serial1);
- delay(1600);
- // check for the presence of the shield
- if (WiFi.status() == WL_NO_SHIELD) {
- Serial.println("WiFi shield not present");
- // don't continue
- while (true);
- }
- // attempt to connect to WiFi network
- while ( status != WL_CONNECTED) {
- Serial.print("Attempting to connect to WPA SSID: ");
- Serial.println(ssid);
- // Connect to WPA/WPA2 network
- status = WiFi.begin(ssid, pass);
- }
- Serial.println("You're connected to the network");
- if (sht.init()) {
- Serial.print("init(): success\n");
- } else {
- Serial.print("init(): failed\n");
- }
- sht.setAccuracy(SHTSensor::SHT_ACCURACY_MEDIUM);
- printWifiStatus();
- sendetid = millis();
- sjekker();
- sender();
- }
- void loop()
- {
- if ((millis() - sendetid) > 10350L) {
- sjekker();
- sender();
- rssi = WiFi.RSSI();
- rssidouble = (0 - rssi) / 10.0;
- }
- }
- // this method makes a HTTP connection to the server
- void sender()
- {
- sendetid = millis();
- Serial.println("Skal sende..");
- client.stop();
- delay(400);
- if (client.connect(server, 80)) {
- // if there's a successful connection
- Serial.println("Connecting...");
- // send the HTTP PUT request
- client.print(F("GET /Sensors/write_data.php"));
- client.print("?temperatur=");
- client.print(temp);
- client.print("&fuktighet=");
- client.print(fuktighet);
- client.print("&rssi=");
- client.print(rssi);
- client.println(F(" HTTP/1.0"));
- client.println(F("Host: website"));
- client.println("Connection: close");
- client.println();
- client.stop();
- // note the time that the connection was made
- }
- else {
- // if you couldn't make a connection
- Serial.println("Connection failed");
- }
- Serial.println("Har sendt!");
- }
- void printWifiStatus()
- {
- // print the SSID of the network you're attached to
- Serial.print("SSID: ");
- Serial.println(WiFi.SSID());
- // print your WiFi shield's IP address
- IPAddress ip = WiFi.localIP();
- Serial.print("IP Address: ");
- Serial.println(ip);
- // print the received signal strength
- rssi = WiFi.RSSI();
- rssidouble = (0 - rssi) / 10.0;
- Serial.print("Signal strength (RSSI):");
- Serial.print(rssi);
- Serial.println(" dBm");
- }
- void sjekker()
- {
- if (sht.readSample()) {
- temp = sht.getTemperature(); // get temp from SHT
- fuktighet = sht.getHumidity(); // get fukt from SHT
- } else {
- Serial.print("Error in readSample()\n");
- }
- delay(500);
- Serial.print("Temperatur = ");
- Serial.print(temp);
- Serial.print(", Fuktighet = ");
- Serial.print(fuktighet);
- Serial.print(", RSSI = ");
- Serial.println(rssi);
- }
Advertisement
Add Comment
Please, Sign In to add comment