Last active
April 9, 2020 07:44
-
-
Save billerby/dc619fbfae6709d7e525a9af6209b177 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <SoftwareSerial.h> | |
#include <ESP8266WiFi.h> | |
SoftwareSerial mySerial(13,15 ); //RX, TX | |
const char* ssid = "Alter_3G"; | |
const char* password = "xxxxxxx"; | |
const char* host = "www.hoppaiplurret.se"; | |
void setup() { | |
Serial.begin(2400); | |
delay(10); | |
// We start by connecting to a WiFi network | |
Serial.println(); | |
Serial.println(); | |
Serial.print("Connecting to "); | |
Serial.println(ssid); | |
WiFi.begin(ssid, password); | |
while (WiFi.status() != WL_CONNECTED) { | |
delay(500); | |
Serial.print("."); | |
} | |
Serial.println(""); | |
Serial.println("WiFi connected"); | |
Serial.println("IP address: "); | |
Serial.println(WiFi.localIP()); | |
mySerial.begin(2400); | |
} | |
void loop() { | |
// Store previous temperatur in static variable | |
static String previousTemp = "0"; | |
if(mySerial.available() > 1){//Read from HC-12 and send to serial monitor | |
String currentTemp = mySerial.readString(); | |
// only send update if temp has changed | |
if (currentTemp != previousTemp){ | |
postTemp(currentTemp); | |
previousTemp = currentTemp; | |
} | |
} | |
delay(2000); | |
} | |
void postTemp(String temp) { | |
Serial.print("connecting to "); | |
Serial.println(host); | |
// Use WiFiClient class to create TCP connections | |
WiFiClient client; | |
const int httpPort = 80; | |
if (!client.connect(host, httpPort)) { | |
Serial.println("connection failed"); | |
return; | |
} | |
// We now create a URI for the request | |
String url = "/esp8266/index.php?temp=" + temp; | |
Serial.print("Requesting URL: "); | |
Serial.println(url); | |
// This will send the request to the server | |
client.print(String("GET ") + url + " HTTP/1.1\r\n" + | |
"Host: " + host + "\r\n" + | |
"Connection: close\r\n\r\n"); | |
delay(10); | |
// Read all the lines of the reply from server and print them to Serial | |
Serial.println("Respond:"); | |
while(client.available()){ | |
String line = client.readStringUntil('\r'); | |
Serial.print(line); | |
} | |
Serial.println(); | |
Serial.println("closing connection"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment