I have a problem: my esp8266 cannot send data to narodmon.ru, but sometimes it does
Don't know why it works so weird
#include <LiquidCrystal_I2C.h>
#include <Wire.h>
#include <SFE_BMP180.h>
#include <ESP8266.h> // ITEADLIB
#include <SoftwareSerial.h>
#define SSID "AP_NAME"
#define PASSWORD "AP_PASS"
#define HOST_NAME "94.19.113.221"
#define HOST_PORT (8283)
#define PRESSURE_MEASUREMENT_INTERVAL 2000
#define LED 13 // LED on D13
#define CHANNEL 3 // oregon channel
LiquidCrystal_I2C lcd(0x27, 16, 2);
SFE_BMP180 pressure;
char mac[] = "0123456789AB";
SoftwareSerial mySerial(8, 9); /* RX:D9, TX:D8 */
ESP8266 wifi(mySerial);
boolean got_humidity, got_pressure, got_temperature = false; // logic variable to test recieving data
// temperature, humidity and battery
float t[2];
byte h[2];
byte b[2];
double temp;
byte hum;
double pres;
char temp_ch[8];
char pres_ch[8];
unsigned long dispNow = 0;
// Oregon V2 decoder added - Dominique Pierre
// New code to decode OOK signals from weather sensors, etc.
// 2010-04-11 <jcw@equi4.com> http://opensource.org/licenses/mit-license.php
// $Id: ookDecoder.pde 5331 2010-04-17 10:45:17Z jcw $
class DecodeOOK {
protected:
byte total_bits, bits, flip, state, pos, data[25];
virtual char decode (word width) = 0;
public:
enum {
UNKNOWN, T0, T1, T2, T3, OK, DONE
};
DecodeOOK () {
resetDecoder();
}
bool nextPulse (word width) {
if (state != DONE)
switch (decode(width)) {
case -1:
resetDecoder();
break;
case 1:
done();
break;
}
return isDone();
}
bool isDone () const {
return state == DONE;
}
const byte* getData (byte& count) const {
count = pos;
return data;
}
void resetDecoder () {
total_bits = bits = pos = flip = 0;
state = UNKNOWN;
}
// add one bit to the packet data buffer
virtual void gotBit (char value) {
total_bits++;
byte *ptr = data + pos;
*ptr = (*ptr >> 1) | (value << 7);
if (++bits >= 8) {
bits = 0;
if (++pos >= sizeof data) {
resetDecoder();
return;
}
}
state = OK;
}
// store a bit using Manchester encoding
void manchester (char value) {
flip ^= value; // manchester code, long pulse flips the bit
gotBit(flip);
}
// move bits to the front so that all the bits are aligned to the end
void alignTail (byte max = 0) {
// align bits
if (bits != 0) {
data[pos] >>= 8 - bits;
for (byte i = 0; i < pos; ++i)
data[i] = (data[i] >> bits) | (data[i + 1] << (8 - bits));
bits = 0;
}
// optionally shift bytes down if there are too many of 'em
if (max > 0 && pos > max) {
byte n = pos - max;
pos = max;
for (byte i = 0; i < pos; ++i)
data[i] = data[i + n];
}
}
void reverseBits () {
for (byte i = 0; i < pos; ++i) {
byte b = data[i];
for (byte j = 0; j < 8; ++j) {
data[i] = (data[i] << 1) | (b & 1);
b >>= 1;
}
}
}
void reverseNibbles () {
for (byte i = 0; i < pos; ++i)
data[i] = (data[i] << 4) | (data[i] >> 4);
}
void done () {
while (bits)
gotBit(0); // padding
state = DONE;
}
};
// 433 MHz decoders
class OregonDecoderV2 :
public DecodeOOK {
public:
OregonDecoderV2() {
}
// add one bit to the packet data buffer
virtual void gotBit (char value) {
if (!(total_bits & 0x01))
{
data[pos] = (data[pos] >> 1) | (value ? 0x80 : 00);
}
total_bits++;
pos = total_bits >> 4;
if (pos >= sizeof data) {
resetDecoder();
return;
}
state = OK;
}
virtual char decode (word width) {
if (200 <= width && width < 1200) {
byte w = width >= 700;
switch (state) {
case UNKNOWN:
if (w != 0) {
// Long pulse
++flip;
}
else if (32 <= flip) {
// Short pulse, start bit
flip = 0;
state = T0;
}
else {
// Reset decoder
return -1;
}
break;
case OK:
if (w == 0) {
// Short pulse
state = T0;
}
else {
// Long pulse
manchester(1);
}
break;
case T0:
if (w == 0) {
// Second short pulse
manchester(0);
}
else {
// Reset decoder
return -1;
}
break;
}
}
else {
return -1;
}
return total_bits == 160 ? 1 : 0;
}
};
OregonDecoderV2 orscV2;
volatile word pulse;
void ext_int_1(void) {
static word last;
pulse = micros() - last;
last += pulse;
}
long lastSend;
void reportLCD (const char* s, class DecodeOOK& decoder) {
byte pos;
const byte* data = decoder.getData(pos);
if (data[0] == 0x1A && data[1] == 0x2D)
{
if (channel(data) == CHANNEL) { // Get data from sensor which channel is CHANNEL
temp = temperature(data);
got_temperature = true;
hum = humidity(data);
got_humidity = true;
lcd.clear();
lcd.setCursor(0, 0);
lcd.print("Temp:");
lcd.setCursor(5, 0);
lcd.print(temperature(data));
lcd.setCursor(9, 0);
lcd.print("'C");
lcd.setCursor(0, 1);
lcd.print("Hum:"); // Will be smth as this |
lcd.setCursor(4, 1); // _______________V_
lcd.print(humidity(data)); // /________________/|
lcd.setCursor(6, 1); // |Temp:9.80'C_____||
lcd.print("%"); // |Hum:50%_752.22mm|/
}
}
decoder.resetDecoder();
}
long time;
void setup () {
Serial.begin(9600); // init esp8266
Serial.print("setup begin\r\n");
Serial.print("FW Version:");
Serial.println(wifi.getVersion().c_str());
if (wifi.setOprToStationSoftAP()) {
Serial.print("to station + softap ok\r\n");
} else {
Serial.print("to station + softap err\r\n");
}
if (wifi.joinAP(SSID, PASSWORD)) {
Serial.print("Join AP success\r\n");
Serial.print("IP:");
Serial.println( wifi.getLocalIP().c_str());
} else {
Serial.print("Join AP failure\r\n");
}
if (wifi.disableMUX()) {
Serial.print("single ok\r\n");
} else {
Serial.print("single err\r\n");
}
Serial.print("setup end\r\n");
pressure.begin();
lcd.init();
lcd.backlight();
lcd.clear();
lcd.setCursor(5, 0);
lcd.print("Loading...");
lcd.createChar(0, percent);
pinMode(2, INPUT); // There is 433mhz reciever
digitalWrite(2, 1); // Pull-up resistor
attachInterrupt(0, ext_int_1, CHANGE); // interrupt
}
long previousTime; // Timing for pressure sensor
void loop () {
long currentTime = millis(); // Current time
char status; // All clear
double P, T;
if (currentTime - previousTime > PRESSURE_MEASUREMENT_INTERVAL) { // Data on lcd updates once every PRESSURE_MEASUREMENT_INTERVAL
previousTime = currentTime; // Recieving bmp180 data and show it
status = pressure.startTemperature(); // Впрочем, ничего особенного
if (status != 0) {
delay(status);
status = pressure.getTemperature(T);
if (status != 0) {
status = pressure.startPressure(3);
if (status != 0) {
delay(status);
status = pressure.getPressure(P, T);
if (status != 0) {
lcd.setCursor(8, 1);
lcd.print(P * 0.75);
pres = P * 0.75;
got_pressure = true;
lcd.setCursor(14, 1);
lcd.print("mm");
}
}
}
}
}
noInterrupts();
word p = pulse;
pulse = 0;
interrupts();
if (p != 0) {
if (orscV2.nextPulse(p)) {
reportLCD("OSV2", orscV2);
Serial.print("Temp:");
Serial.println(temp);
Serial.print("Humidity:");
Serial.println(hum);
Serial.print("Pressure:");
Serial.println(pres);
Serial.print(got_temperature);
Serial.print(got_humidity);
Serial.println(got_pressure);
digitalWrite(LED, HIGH);
}
}
// If 10 minutes gone and recieved hunidity/temperature/pressure
if (millis() - lastSend > 60000 && got_humidity && got_temperature && got_pressure) {
uint8_t buffer[128] = {0};
if (wifi.createTCP(HOST_NAME, HOST_PORT)) {
Serial.print("create tcp ok\r\n"); // Sometimes
} else {
Serial.print("create tcp err\r\n"); // Almost everytime
}
char hello[128];
dtostrf(temp,3,1,temp_ch);
dtostrf(pres,3,1,pres_ch);
sprintf(hello, "\#%s\r\n\#T1#%s\r\n#H1\#%d\r\n\#P1\#%s\r\n\#\#", mac, temp_ch, hum, pres_ch);
Serial.println("Sending:");
Serial.println(hello);
wifi.send((const uint8_t*)hello, strlen(hello));// That's all
// So much weird... Wow
uint32_t len = wifi.recv(buffer, sizeof(buffer), 10000);
if (len > 0) {
Serial.print("Received:[");
for (uint32_t i = 0; i < len; i++) {
Serial.print((char)buffer[i]);
}
Serial.print("]\r\n");
}
if (wifi.releaseTCP()) {
Serial.print("release tcp ok\r\n");
} else {
Serial.print("release tcp err\r\n");
}
lastSend = millis();
}
}
float temperature(const byte* data)
{
int sign = (data[6] & 0x8) ? -1 : 1;
float temp = ((data[5] & 0xF0) >> 4) * 10 + (data[5] & 0xF) + (float)(((data[4] & 0xF0) >> 4) / 10.0);
return sign * temp;
}
byte humidity(const byte* data)
{
return (data[7] & 0xF) * 10 + ((data[6] & 0xF0) >> 4);
}
// Ne retourne qu'un apercu de l'etat de la baterie : 10 = faible
byte battery(const byte* data)
{
return (data[4] & 0x4) ? 10 : 90;
}
byte channel(const byte* data)
{
byte channel;
switch (data[2])
{
case 0x10:
channel = 1;
break;
case 0x20:
channel = 2;
break;
case 0x40:
channel = 3;
break;
}
return channel;
}
void printValues() {
char buf[16];
char tbuf[6];
char templ[] = {' ', '%', 's', char(223), 'C', ' ', '%', '3', 'd', '%', '%', ' ', ' ', ' ', '\0'};
for (int i = 0; i < 2; i++) {
if (b[i] > 50) {
templ[13] = char(219);
}
else {
templ[13] = char(161);
}
dtostrf(t[i], 5, 1, tbuf);
sprintf(buf, templ, tbuf, h[i]);
Serial.println(buf);
}
}
Watch 355 - 390 line
Output: setup begin
FW Version:0018000902
to station + softap ok
Join AP success
IP:192.168.4.1
192.168.1.2
single ok
setup end
25.60
44
744.59
111
create tcp err
Sending:
#0123456789
#T1#25.6
#H1#44
#P1#744.6
##
release tcp err
Help please