Components and supplies
ESP8266 ESP-01
SparkFun Atmospheric Sensor Breakout - BME280
Pocket Solder- 60/40 Rosin Core 0.031" diameter
Arduino UNO
Jumper wires (generic)
Tools and machines
Soldering iron (generic)
Apps and platforms
Arduino IDE
Project description
Code
Final Code
arduino
1#include <SoftwareSerial.h> 2#define RX 10// Пин подключения к esp8266 3#define TX 11// Пин подключения к esp8266 4String AP = "Aleks"; // Имя сети 5String PASS = "1brui47ci881"; // Пароль 6String HOST = "192.168.0.185";//ваш локальный ип 7String PORT = "80";//порт апача 8String field = "field1"; 9int countTrueCommand; 10int countTimeCommand; 11boolean found = false; 12int valSensor = 1; 13SoftwareSerial esp8266(RX,TX); 14#include <Wire.h> 15#include <SPI.h> 16#include <Adafruit_Sensor.h> 17#include <Adafruit_BME280.h> 18 19#define BME_SCK 13 20#define BME_MISO 12 21#define BME_MOSI 11 22#define BME_CS 10 23 24#define SEALEVELPRESSURE_HPA (1013.25) 25 26Adafruit_BME280 bme; // I2C 27//Adafruit_BME280 bme(BME_CS); // hardware SPI 28//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI 29 30unsigned long delayTime; 31 32void setup() { 33 Serial.begin(9600); 34 35 36 bool status; 37 38 // default settings 39 // (you can also pass in a Wire library object like &Wire2) 40 status = bme.begin(); 41 if (!status) { 42 Serial.println("Could not find a valid BME280 sensor, check wiring!"); 43 while (1); 44 } 45 46 47 esp8266.begin(115200); 48 sendCommand("AT",5,"OK"); 49 sendCommand("AT+CWMODE=1",5,"OK"); 50 sendCommand("AT+CWJAP=\\""+ AP +"\\",\\""+ PASS +"\\"",20,"OK"); 51 sendCommand("AT+CIPMUX=1",5,"OK"); 52 53 54} 55 56 57void loop() { 58 float vlah=bme.readPressure() / 100.0F; 59 sendCommand("AT+CIPSTART=0,\\"TCP\\",\\""+ HOST +"\\","+ PORT,15,"OK"); 60 String cmd = "GET /?temp="+String(bme.readTemperature())+"&vlah="+String(vlah)+"&davlen="+String(bme.readHumidity())+" HTTP/1.1\ \ 61Host: 192.168.0.185\ \ 62\ \ 63";// GET запрос на локальный сервер,temp,vlah,davlen переменые в которые заносяться данные с датчика и заносяться в бд. 64 sendCommand("AT+CIPSEND=0," +String(cmd.length()+4),4,">"); 65 esp8266.println(cmd);delay(1500);countTrueCommand++; 66 sendCommand("AT+CIPCLOSE=0",5,"OK"); 67 delay(100000); 68} 69 70 71void printValues() { 72 Serial.print("Temperature = "); 73 Serial.print(bme.readTemperature()); 74 Serial.println(" *C"); 75 76 Serial.print("Pressure = "); 77 78 Serial.print(bme.readPressure() / 100.0F); 79 Serial.println(" hPa"); 80 81 82 Serial.print("Humidity = "); 83 Serial.print(bme.readHumidity()); 84 Serial.println(" %"); 85 86 Serial.println(); 87} 88void sendCommand(String command, int maxTime, char readReplay[]) { 89 Serial.print(countTrueCommand); 90 Serial.print(". at command => "); 91 Serial.print(command); 92 Serial.print(" "); 93 while(countTimeCommand < (maxTime*1)) 94 { 95 esp8266.println(command);//at+cipsend 96 if(esp8266.find(readReplay))//ok 97 { 98 found = true; 99 break; 100 } 101 102 countTimeCommand++; 103 } 104 105 if(found == true) 106 { 107 Serial.println("OYI"); 108 countTrueCommand++; 109 countTimeCommand = 0; 110 } 111 112 if(found == false) 113 { 114 Serial.println("Fail"); 115 countTrueCommand = 0; 116 countTimeCommand = 0; 117 } 118 119 found = false; 120 }
Final Code
arduino
1#include <SoftwareSerial.h> 2#define RX 10// Пин подключения к esp8266 3#define TX 11// Пин подключения к esp8266 4String AP = "Aleks"; // Имя сети 5String PASS = "1brui47ci881"; // Пароль 6String HOST = "192.168.0.185";//ваш локальный ип 7String PORT = "80";//порт апача 8String field = "field1"; 9int countTrueCommand; 10int countTimeCommand; 11boolean found = false; 12int valSensor = 1; 13SoftwareSerial esp8266(RX,TX); 14#include <Wire.h> 15#include <SPI.h> 16#include <Adafruit_Sensor.h> 17#include <Adafruit_BME280.h> 18 19#define BME_SCK 13 20#define BME_MISO 12 21#define BME_MOSI 11 22#define BME_CS 10 23 24#define SEALEVELPRESSURE_HPA (1013.25) 25 26Adafruit_BME280 bme; // I2C 27//Adafruit_BME280 bme(BME_CS); // hardware SPI 28//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI 29 30unsigned long delayTime; 31 32void setup() { 33 Serial.begin(9600); 34 35 36 bool status; 37 38 // default settings 39 // (you can also pass in a Wire library object like &Wire2) 40 status = bme.begin(); 41 if (!status) { 42 Serial.println("Could not find a valid BME280 sensor, check wiring!"); 43 while (1); 44 } 45 46 47 esp8266.begin(115200); 48 sendCommand("AT",5,"OK"); 49 sendCommand("AT+CWMODE=1",5,"OK"); 50 sendCommand("AT+CWJAP=\\""+ AP +"\\",\\""+ PASS +"\\"",20,"OK"); 51 sendCommand("AT+CIPMUX=1",5,"OK"); 52 53 54} 55 56 57void loop() { 58 float vlah=bme.readPressure() / 100.0F; 59 sendCommand("AT+CIPSTART=0,\\"TCP\\",\\""+ HOST +"\\","+ PORT,15,"OK"); 60 String cmd = "GET /?temp="+String(bme.readTemperature())+"&vlah="+String(vlah)+"&davlen="+String(bme.readHumidity())+" HTTP/1.1\ \ 61Host: 192.168.0.185\ \ 62\ \ 63";// GET запрос на локальный сервер,temp,vlah,davlen переменые в которые заносяться данные с датчика и заносяться в бд. 64 sendCommand("AT+CIPSEND=0," +String(cmd.length()+4),4,">"); 65 esp8266.println(cmd);delay(1500);countTrueCommand++; 66 sendCommand("AT+CIPCLOSE=0",5,"OK"); 67 delay(100000); 68} 69 70 71void printValues() { 72 Serial.print("Temperature = "); 73 Serial.print(bme.readTemperature()); 74 Serial.println(" *C"); 75 76 Serial.print("Pressure = "); 77 78 Serial.print(bme.readPressure() / 100.0F); 79 Serial.println(" hPa"); 80 81 82 Serial.print("Humidity = "); 83 Serial.print(bme.readHumidity()); 84 Serial.println(" %"); 85 86 Serial.println(); 87} 88void sendCommand(String command, int maxTime, char readReplay[]) { 89 Serial.print(countTrueCommand); 90 Serial.print(". at command => "); 91 Serial.print(command); 92 Serial.print(" "); 93 while(countTimeCommand < (maxTime*1)) 94 { 95 esp8266.println(command);//at+cipsend 96 if(esp8266.find(readReplay))//ok 97 { 98 found = true; 99 break; 100 } 101 102 countTimeCommand++; 103 } 104 105 if(found == true) 106 { 107 Serial.println("OYI"); 108 countTrueCommand++; 109 countTimeCommand = 0; 110 } 111 112 if(found == false) 113 { 114 Serial.println("Fail"); 115 countTrueCommand = 0; 116 countTimeCommand = 0; 117 } 118 119 found = false; 120 }
Downloadable files
Final Scheme
Final Scheme
Comments
Only logged in users can leave comments