Advertisement
Guest User

Untitled

a guest
Apr 9th, 2020
158
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <SoftwareSerial.h>
  2. #include <ESP8266WiFi.h>
  3.  
  4. SoftwareSerial mySerial(13,15 ); //RX, TX
  5.  
  6. const char* ssid     = "Alter_3G";
  7. const char* password = "xxxxxxx";
  8.  
  9. const char* host = "www.hoppaiplurret.se";
  10.  
  11. void setup() {
  12.   Serial.begin(2400);
  13.   delay(10);
  14.  
  15.   // We start by connecting to a WiFi network
  16.   Serial.println();
  17.   Serial.println();
  18.   Serial.print("Connecting to ");
  19.   Serial.println(ssid);
  20.  
  21.   WiFi.begin(ssid, password);
  22.  
  23.   while (WiFi.status() != WL_CONNECTED) {
  24.     delay(500);
  25.     Serial.print(".");
  26.   }
  27.  
  28.   Serial.println("");
  29.   Serial.println("WiFi connected");  
  30.   Serial.println("IP address: ");
  31.   Serial.println(WiFi.localIP());
  32.  
  33.   mySerial.begin(2400);
  34. }
  35.  
  36. void loop() {
  37.   // Store previous temperatur in static variable
  38.   static String previousTemp = "0";
  39.   if(mySerial.available() > 1){//Read from HC-12 and send to serial monitor
  40.    
  41.     String currentTemp = mySerial.readString();
  42.    
  43.     // only send update if temp has changed
  44.     if (currentTemp != previousTemp){
  45.         postTemp(currentTemp);
  46.         previousTemp = currentTemp;
  47.     }
  48.   }
  49.   delay(2000);
  50. }
  51.  
  52. void postTemp(String temp) {
  53.   Serial.print("connecting to ");
  54.   Serial.println(host);
  55.  
  56.   // Use WiFiClient class to create TCP connections
  57.   WiFiClient client;
  58.   const int httpPort = 80;
  59.   if (!client.connect(host, httpPort)) {
  60.     Serial.println("connection failed");
  61.     return;
  62.   }
  63.  
  64.   // We now create a URI for the request
  65.   String url = "/esp8266/index.php?temp=" + temp;
  66.  
  67.   Serial.print("Requesting URL: ");
  68.   Serial.println(url);
  69.  
  70.   // This will send the request to the server
  71.   client.print(String("GET ") + url + " HTTP/1.1\r\n" +
  72.                "Host: " + host + "\r\n" +
  73.                "Connection: close\r\n\r\n");
  74.   delay(10);
  75.  
  76.   // Read all the lines of the reply from server and print them to Serial
  77.   Serial.println("Respond:");
  78.   while(client.available()){
  79.     String line = client.readStringUntil('\r');
  80.     Serial.print(line);
  81.   }
  82.  
  83.   Serial.println();
  84.   Serial.println("closing connection");
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement