mirror of
https://bitbucket.org/myhomie/mycorerepository.git
synced 2025-12-06 09:41:19 +00:00
157 lines
3.5 KiB
C++
157 lines
3.5 KiB
C++
#include <SPI.h>
|
|
#include <Wire.h>
|
|
#include <Adafruit_GFX.h>
|
|
#include <Adafruit_SSD1306.h>
|
|
#include <ESP8266WiFi.h>
|
|
#include <WiFiClient.h>
|
|
#include <ESP8266WebServer.h>
|
|
#include <ESP8266mDNS.h>
|
|
#include <ArduinoJson.h>
|
|
#include <ESP8266HTTPClient.h>;
|
|
|
|
|
|
Adafruit_SSD1306 display = Adafruit_SSD1306();
|
|
|
|
#define BUTTON_A 0
|
|
#define BUTTON_B 16
|
|
#define BUTTON_C 2
|
|
#define LED 0
|
|
|
|
const char* ssid = "ESP8266 - TEST";
|
|
const char* password = "Coconuts08";
|
|
|
|
|
|
/*const char* ssid = "ESP8266";
|
|
const char* password = "password";*/
|
|
|
|
const char* host = "192.168.101.18";
|
|
|
|
ESP8266WebServer server(80);
|
|
|
|
const int Led = 14;
|
|
|
|
|
|
#if (SSD1306_LCDHEIGHT != 32)
|
|
#error("Height incorrect, please fix Adafruit_SSD1306.h!");
|
|
#endif
|
|
|
|
void setup() {
|
|
pinMode(Led, OUTPUT);
|
|
Serial.begin(115200);
|
|
WiFi.begin(ssid, password);
|
|
Serial.println("");
|
|
digitalWrite(Led, LOW);
|
|
|
|
// Wait for connection
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.print("Connected to ");
|
|
Serial.println(ssid);
|
|
Serial.print("IP address: ");
|
|
Serial.println(WiFi.localIP());
|
|
String IP = WiFi.localIP()+"";
|
|
digitalWrite(Led, HIGH);
|
|
|
|
if (MDNS.begin("esp8266")) {
|
|
Serial.println("MDNS responder started");
|
|
}
|
|
|
|
// by default, we'll generate the high voltage from the 3.3v line internally! (neat!)
|
|
display.begin(SSD1306_SWITCHCAPVCC, 0x3C); // initialize with the I2C addr 0x3C (for the 128x32)
|
|
// init done
|
|
|
|
// Show image buffer on the display hardware.
|
|
// Since the buffer is intialized with an Adafruit splashscreen
|
|
// internally, this will display the splashscreen.
|
|
display.display();
|
|
delay(1000);
|
|
|
|
// Clear the buffer.
|
|
display.clearDisplay();
|
|
display.display();
|
|
|
|
Serial.println("IO test");
|
|
|
|
pinMode(BUTTON_A, INPUT_PULLUP);
|
|
pinMode(BUTTON_B, INPUT_PULLUP);
|
|
pinMode(BUTTON_C, INPUT_PULLUP);
|
|
|
|
// text display tests
|
|
display.setTextSize(1);
|
|
display.setTextColor(WHITE);
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
|
/* if (! digitalRead(BUTTON_A)) display.print("A");
|
|
if (! digitalRead(BUTTON_B)) display.print("B");
|
|
if (! digitalRead(BUTTON_C)) display.print("C");
|
|
delay(10);
|
|
yield();
|
|
display.display();*/
|
|
|
|
if(WiFi.status() != WL_CONNECTED){
|
|
digitalWrite(Led, LOW);
|
|
Serial.println("Trying to reconnect !");
|
|
while (WiFi.status() != WL_CONNECTED) {
|
|
delay(500);
|
|
Serial.print(".");
|
|
}
|
|
Serial.println("");
|
|
Serial.println("Connected !");
|
|
}else{
|
|
if(digitalRead(Led)==LOW){
|
|
digitalWrite(Led, HIGH);
|
|
}
|
|
|
|
//if(!digitalRead(BUTTON_A)){
|
|
display.clearDisplay();
|
|
display.setCursor(0,0);
|
|
display.println("Connect to hum sensor");
|
|
display.println("");
|
|
|
|
WiFiClient client;
|
|
const int httpPort = 80;
|
|
if (!client.connect(host, httpPort)) {
|
|
Serial.println("connection failed");
|
|
digitalWrite(Led, LOW);
|
|
return;
|
|
}
|
|
|
|
HTTPClient http;
|
|
http.begin("http://192.168.101.18/temp");
|
|
|
|
int httpCode = http.GET();
|
|
if(httpCode == HTTP_CODE_OK){
|
|
Serial.print("HTTP response code ");
|
|
Serial.println(httpCode);
|
|
String response = http.getString();
|
|
Serial.println(response);
|
|
StaticJsonBuffer<200> jsonBuffer;
|
|
JsonObject& root = jsonBuffer.parseObject(response);
|
|
String temp = root["temperature"];
|
|
String hum = root["humidity"];
|
|
Serial.println("Temperature :"+temp);
|
|
Serial.println("Humidity :"+hum);
|
|
|
|
display.println("Temp: "+temp+"C - Hum: "+hum+"%");
|
|
display.display();
|
|
}
|
|
else
|
|
{
|
|
Serial.println("Error in HTTP request");
|
|
}
|
|
|
|
http.end();
|
|
|
|
delay(5000);
|
|
|
|
// }
|
|
|
|
}
|
|
}
|