код позаимствовал https://geektimes.ru/post/272090/
в majordomo данные передает скрипту espdata http://majordomo.smartliving.ru/forum/v ... f=8&t=2830
питание от зарядки для сотового
Код в Arduino IDE
СпойлерПоказать
Код: Выделить всё
#include <SoftwareSerial.h>;
// пин встроенного светодиода
#define LedPin 2
// соединяем перемычкой gpio 16 и ресет
#define ResetPin 16
SoftwareSerial mySerial(13, 15); // gpio 13 - к TX сенсора, gpio 15 - к RX
byte cmd[9] = {0xFF,0x01,0x86,0x00,0x00,0x00,0x00,0x00,0x79};
unsigned char response[9];
#include <ESP8266WiFi.h>
char ssid[] = "SSID"; // your network SSID (name)
char pass[] = "PASS"; // your network password
int status = WL_IDLE_STATUS;
WiFiClient rclient;
byte ip[] = { 192, 168, 1, 55 }; // задается только для информации
// ip-адрес удалённого сервера
byte server[] = { 192, 168, 1, 100 }; //Адрес сервера MAJORDOMO
char buf[80];
char ipbuff[16];
void sendHTTPRequest() {
Serial.println(buf);
if (rclient.connect(server, 80)) {
rclient.print(buf);
rclient.println(" HTTP/1.0");
Serial.println("Send http get request");
Serial.println(buf);
rclient.print("Host: ");
sprintf(ipbuff, "%u.%u.%u.%u", ip[0], ip[1], ip[2], ip[3]);
rclient.println(ipbuff); // ip адрес нашего контроллера в текстовом виде
rclient.print("Content-Type: text/html\n");
rclient.println("Connection: close\n");
delay(2000);
rclient.stop();
} else {
Serial.println("FAILED");
}
}
void setup() {
WiFi.mode(WIFI_STA) ;
WiFi.begin(ssid, pass);
Serial.begin(9600);
mySerial.begin(9600);
pinMode(LedPin, OUTPUT);
pinMode(ResetPin, OUTPUT);
digitalWrite(LedPin, HIGH); //гасим светодиод (у него инвесное управление)
digitalWrite(ResetPin, HIGH);
}
void loop()
{
mySerial.write(cmd, 9);
memset(response, 0, 9);
mySerial.readBytes(response, 9);
int i;
byte crc = 0;
for (i = 1; i < 8; i++) crc+=response[i];
crc = 255 - crc;
crc++;
if ( !(response[0] == 0xFF && response[1] == 0x86 && response[8] == crc) ) {
Serial.println("CRC error: " + String(crc) + " / "+ String(response[8]));
digitalWrite(LedPin, LOW); // зажигаем светодиод при ошибке
delay(200);
digitalWrite(ResetPin, LOW); // ресетим ESP8266 если ошибка ( если не ресетить то в режим нормального приема- передачи ESP8266 не переходило в течении суток)
} else {
unsigned int responseHigh = (unsigned int) response[2];
unsigned int responseLow = (unsigned int) response[3];
unsigned int ppm = (256*responseHigh) + responseLow;
Serial.println(ppm);
// при инициализации первое значение часто равно 5000
if ( ppm != 5000 ) {
sprintf(buf, "GET /objects/?script=espdata&idesp=MON-CO2&bmpp=%i",ppm); //данные передаются в переменную bmpp ( можно выбрать любую другую)
sendHTTPRequest();
}
}
delay(10000);
}
СпойлерПоказать