Доброго времени суток. Помогите разобраться с кодом для Nodemcu + модуль 433 мгц. Ардуино ловит данные с беспроводных датчиков 433мгц и будет отправлять в телегу.
Имеется код который работает в ардуине нано, при переделке его под esp8266 выдает циклическую перезагрузку. Как исправить проблему
Имеется код который работает в ардуине нано, при переделке его под esp8266 выдает циклическую перезагрузку. Как исправить проблему
C++:
#include <ESP8266WiFi.h>
#include <UniversalTelegramBot.h>
/* WiFi credentials */
char ssid[] = "****";
char password[] = "*****";
// Initialize Telegram BOT
#define BOTtoken "****:******" // your Bot Token (Get from Botfather)
WiFiClientSecure client;
UniversalTelegramBot bot(BOTtoken, client);
//Checks for new messages every 1 second.
int botRequestDelay = 1000;
unsigned long lastTimeBotRan;
// 433 начало
int RxPin = 14; // The number of signal from the Rx
// Variables for Manchester Receiver Logic:
word sDelay = 242; // Small Delay about 1/4 of bit duration
word lDelay = 484; // Long Delay about 1/2 of bit duration, 1/4 + 1/2 = 3/4
byte polarity = 1; // 0 for lo->hi==1 or 1 for hi->lo==1 for Polarity, sets tempBit at start
byte tempBit = 1; // Reflects the required transition polarity
boolean firstZero = false; // flags when the first '0' is found.
// Variables for Header detection
byte headerBits = 10; // The number of ones expected to make a valid header
byte headerHits = 0; // Counts the number of "1"s to determine a header
// Variables for Byte storage
boolean sync0In = true; // Expecting sync0 to be inside byte boundaries, set to false for sync0 outside bytes
byte dataByte = 0; // Accumulates the bit information
byte nosBits = 6; // Counts to 8 bits within a dataByte
byte maxBytes = 6; // Set the bytes collected after each header. NB if set too high, any end noise will cause an error
byte nosBytes = 0; // Counter stays within 0 -> maxBytes
// Variables for multiple packets
byte bank = 0; // Points to the array of 0 to 3 banks of results from up to 4 last data downloads
byte nosRepeats = 3; // Number of times the header/data is fetched at least once or up to 4 times
// Banks for multiple packets if required (at least one will be needed)
byte manchester[7]; // Array to store 7 bytes of manchester pattern decoded on the fly
// Variables to prepare recorded values for Ambient
byte stnId = 0; // Identifies the channel number
int dataType = 0; // Identifies the Ambient Thermo-Hygrometer code
int differencetemp = 0;
int differencehum = 0;
int Newtemp = 0;
int Newhum = 0;
int chTemp[8];
int chHum[8];
unsigned long chLastRecv[8];
char displaystr[20];
float siTemp;
volatile boolean isrcalled;
volatile unsigned long EdgeTime; // zur Speicherung der Zeit
unsigned long LastEdgeTime = 0;
unsigned long DiffEdgeTime = 0;
unsigned long Dstop = 0;
byte rxstate = 0;
// 433 конец
void setup()
{
Serial.begin(115200);
Serial.println(" ");
Serial.println("Температура с метеодатчиков");
client.setInsecure();
// Set WiFi to station mode and disconnect from an AP if it was Previously
// connected
WiFi.mode(WIFI_STA);
WiFi.disconnect();
delay(100);
// Attempt to connect to Wifi network:
Serial.print("Connecting Wifi: ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println("");
Serial.println("WiFi connected");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(RxPin, INPUT);
eraseManchester(); // clear the array to different nos cause if all zeroes it might think that is a valid 3 packets ie all equal
chTemp[0] = chTemp[1] = chTemp[2] = chTemp[3] = chTemp[4] = chTemp[5] = chTemp[6] = chTemp[7] = 720;
chHum[0] = chHum[1] = chHum[2] = chHum[3] = chHum[4] = chHum[5] = chHum[6] = chHum[7] = 0;
chLastRecv[0] = chLastRecv[1] = chLastRecv[2] = chLastRecv[3] = chLastRecv[4] = chLastRecv[5] = chLastRecv[6] = chLastRecv[7] = 0;
pinMode(RxPin, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(RxPin), isr, CHANGE); // interrupt 0 is pin 2
}
// 433 начало
// Interrupt Service Routine for a falling edge
void isr()
{
if (!isrcalled)
{
EdgeTime = micros();
isrcalled = true;
}
} // end of isr
// 433 конец
void loop() {
//ToDo init values depends on the state: new packet
//state 0 = init = new packet
//state 1 = wait for first change
//state 2 = sDelay
//state 3 = lDelay
//state 4 = processBit
//digitalWrite(7,LOW); //is for Oszi trigger
switch (rxstate)
{
case 0: // new packet
tempBit = polarity; // these begin the same for a packet
firstZero = false;
headerHits = 0;
nosBits = 6;
nosBytes = 0;
rxstate++; //next state
isrcalled = false;
//break;
case 1: //waiting for edge
if ((digitalRead(RxPin) == tempBit))
{
Dstop = EdgeTime + sDelay;
//isrcalled = false; //double check this state handling race condition, noise, does it fit for the timing in all cases?
rxstate = 2;
}
else
break;
case 2: // sDelay
if (micros() > Dstop)
{ // delay passed
// 3/4 the way through, if RxPin has changed it is definitely an error
if (digitalRead(RxPin) != tempBit)
{
rxstate = 0;
break; // something has gone wrong, polarity has changed too early, ie always an error
} // exit and retry
Dstop+=lDelay; //next stop for state 3
rxstate = 3;
} else break; //we have to wait in state 2
case 3:
if (micros() > Dstop) {
//delay passed
// now 1 quarter into the next bit pattern,
rxstate = 4;
} else break; //we have to wait in state 3
case 4:
if (digitalRead(RxPin) == tempBit)
{ // if RxPin has not swapped, then bitWaveform is swapping
// If the header is done, then it means data change is occuring ie 1->0, or 0->1
// data transition detection must swap, so it loops for the opposite transitions
tempBit = tempBit ^ 1;
} // end of detecting no transition at end of bit waveform, ie end of previous bit waveform same as start of next bitwaveform
//****************************//
// Now process the tempBit state and make data definite 0 or 1's, allow possibility of Pos or Neg Polarity
byte bitState = tempBit ^ polarity; // if polarity=1, invert the tempBit or if polarity=0, leave it alone.
if (bitState == 1)
{ // 1 data could be header or packet
if (!firstZero)
{
headerHits++;
}
else
{
add(bitState); // already seen first zero so add bit in
}
} // end of dealing with ones
else
{ // bitState==0 could first error, first zero or packet
// if it is header there must be no "zeroes" or errors
if (headerHits < headerBits)
{
// Still in header checking phase, more header hits required
rxstate = 0;
break; // landing here means header is corrupted, so it is probably an error
} // end of detecting a "zero" inside a header
else
{
firstZero = true;
add(bitState);
} // end of dealing with a first zero
} // end of dealing with zero's (in header, first or later zeroes)
rxstate = 1; //next bit
isrcalled = false; //double check this state handling race condition, noise, does it fit for the timing in all cases?
} // case statement
// end of mainloop
}
// Read the binary data from the bank and apply conversions where necessary to scale and format data
void add(byte bitData)
{
dataByte = (dataByte << 1) | bitData;
nosBits++;
if (nosBits == 8)
{
nosBits = 0;
manchester[nosBytes] = dataByte;
nosBytes++;
}
if (nosBytes == maxBytes)
{
rxstate = 0; // we got all bytes lets start for the next packet
// Subroutines to extract data from Manchester encoding and error checking
// Identify channels 0 to 7 by looking at 3 bits in byte 3
int stnId = (manchester[3] & B01110000) / 16;
// Identify sensor by looking for sensorID in byte 1 (F007th Ambient Thermo-Hygrometer = 0x45)
dataType = manchester[1];
// Gets raw temperature from bytes 3 and 4 (note this is neither C or F but a value from the sensor)
Newtemp = float((manchester[3] & B00000111) * 256) + manchester[4];
// Gets humidity data from byte 5
Newhum = manchester[5];
// Checks sensor is a F007th with a valid humidity reading equal or less than 100
if (dataType == 0x45 && Newhum <= 100)
{
saveReading(stnId, Newtemp, Newhum);
}
}
}
void saveReading(int stnId, int newTemp, int newHum)
{
bool sendData = false;
if (stnId >= 0 && stnId <= 7)
{
// If the raw temperature is 720 (default when sketch started so first reading), accept the new readings as the temperature and humidity on channel 1
if (chTemp[stnId] == 720)
{
chTemp[stnId] = newTemp;
chHum[stnId] = newHum;
sendData = true;
}
// If the raw temperature is other than 720 (so a subsequent reading), check that it is close to the previous reading before accepting as the new channel 1 reading
else
{
differencetemp = newTemp - chTemp[stnId];
differencehum = newHum - chHum[stnId];
if ((differencetemp < 20 && differencetemp > -20) && (differencehum < 5 && differencehum > -5))
{
chTemp[stnId] = newTemp;
chHum[stnId] = newHum;
sendData = true;
}
}
unsigned long now = millis();
if (sendData && (chLastRecv[stnId] > now || (now - chLastRecv[stnId]) > 1000))
{
// checks above seems to avoid too many outpuut
//digitalWrite(7,HIGH); //is for Oszi trigger
//delay(1);
Serial.print(" ID= ");
Serial.print(stnId + 1);
Serial.print(": ");
Serial.print(" TEMP= ");
//Serial.print(newTemp - 400); // in F
siTemp = 0.0556 * (newTemp - 720);
Serial.print(siTemp);
Serial.print("C");
Serial.print(" : ");
Serial.print(" HUM= ");
Serial.print(newHum);
Serial.println("%");
chLastRecv[stnId] = now;
}
}
}
void eraseManchester()
{
for (int j = 0; j < 4; j++)
{
manchester[j] = j;
}
}