Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <Wire.h>
- extern "C" {
- #include <espnow.h>
- }
- //Thermistor related:
- /* Here we have a few constants that make editing the code easier. I will go
- through them one by one.
- A reading from the ADC might give one value at one sample and then a little
- different the next time around. To eliminate noisy readings, we can sample
- the ADC pin a few times and then average the samples to get something more
- solid. This constant is utilized in the readThermistor function.
- */
- const int SAMPLE_NUMBER = 10;
- /* In order to use the Beta equation, we must know our other resistor
- within our resistor divider. If you are using something with large tolerance,
- like at 5% or even 1%, measure it and place your result here in ohms. */
- const double BALANCE_RESISTOR = 9970.0;
- // This helps calculate the thermistor's resistance (check article for details).
- const double MAX_ADC = 1023.0;
- /* This is thermistor dependent and it should be in the datasheet, or refer to the
- article for how to calculate it using the Beta equation.
- I had to do this, but I would try to get a thermistor with a known
- beta if you want to avoid empirical calculations. */
- const double BETA = 3950.0;
- /* This is also needed for the conversion equation as "typical" room temperature
- is needed as an input. */
- const double ROOM_TEMP = 298.15; // room temperature in Kelvin
- /* Thermistors will have a typical resistance at room temperature so write this
- down here. Again, needed for conversion equations. */
- const double RESISTOR_ROOM_TEMP = 10000.0;
- //===============================================================================
- // Variables
- //===============================================================================
- // Here is where we will save the current temperature
- double currentTemperature = 0;
- //===============================================================================
- // Pin Declarations
- //===============================================================================
- //Inputs:
- int thermistorPin = 0; // Where the ADC samples the resistor divider's output
- // this is the MAC Address of the remote ESP server which receives these sensor readings
- uint8_t remoteMac[] = {0x86, 0xF3, 0xEB, 0X62, 0x71, 0xEC};
- #define WIFI_CHANNEL 1
- #define SLEEP_SECS 5 // 15 minutes
- #define SLEEP_TIME 36e8 //6e7 // has to be number of seconds * 1000 * 1000 i.e. 10s is 10 * 1000 * 1000 = 10000000 or 1e7 ( 1 + 7 zeros) for testing 1e7, 6e7 = 60s & 36e8 is 1 hour
- #define SEND_TIMEOUT 245 // 245 millis seconds timeout
- unsigned long entry;
- // keep in sync with ESP_NOW sensor struct
- struct SensorData {
- String id; //Sensor Name
- int fieldT; // ThingSpeak Field Temperature
- int fieldH; // ThingSpeak Field Humidity
- float temp; // Temperature Variable
- int hum; // Humidity Variable
- } sD;
- volatile boolean callbackCalled;
- unsigned long entry1 = millis();
- void setup() {
- Serial.begin(115200);
- sensor.begin();
- Serial.println();
- Serial.println();
- Serial.println("ESP_Now Controller");
- Serial.println();
- WiFi.mode(WIFI_STA);
- WiFi.disconnect();
- Serial.printf("This mac: %s, ", WiFi.macAddress().c_str());
- Serial.printf("target mac: ");
- char macString[50] = {0};
- sprintf(macString,"%02X:%02X:%02X:%02X:%02X:%02X", remoteMac[0], remoteMac[1], remoteMac[2], remoteMac[3], remoteMac[4], remoteMac[5]);
- Serial.print(macString);
- Serial.printf(", on channel: %i\n", WIFI_CHANNEL);
- if (esp_now_init() != 0) {
- Serial.println("*** ESP_Now init failed");
- gotoSleep();
- }
- esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
- unsigned long entry2 = millis();
- esp_now_add_peer(remoteMac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
- unsigned long entry3 = millis();
- esp_now_register_send_cb([](uint8_t* mac, uint8_t sendStatus) {
- Serial.println();
- Serial.printf("send_cb, send done, status = %i\n", sendStatus);
- callbackCalled = true;
- });
- unsigned long entry4 = millis();
- callbackCalled = false;
- }
- void loop() {
- currentTemperature = readThermistor();
- sD.id = "H";
- sD.fieldT = 4;
- sD.temp = currentTemperature;
- u8 bs[sizeof(sD)];
- memcpy(bs, &sD, sizeof(sD));
- unsigned long entry = millis();
- esp_now_send(NULL, bs, sizeof(sD)); // NULL means send to all peers
- Serial.print("Overall Time: ");
- Serial.println(millis() - entry1);
- Serial.print(sD.id); Serial.printf(" Temp = %0.2f", sD.temp); Serial.print("\u2103");
- ESP.deepSleep(SLEEP_TIME, WAKE_RF_DEFAULT);
- ESP.restart();
- }
- void gotoSleep() {
- // add some randomness to avoid collisions with multiple devices
- int sleepSecs = SLEEP_SECS;// + ((uint8_t)RANDOM_REG32/2);
- Serial.printf("Up for %i ms, going to sleep for %i secs...\n", millis(), sleepSecs);
- ESP.deepSleep(sleepSecs * 1000000, RF_NO_CAL);
- }
- double readThermistor()
- {
- // variables that live in this function
- double rThermistor = 0; // Holds thermistor resistance value
- double tKelvin = 0; // Holds calculated temperature
- double tCelsius = 0; // Hold temperature in celsius
- double adcAverage = 0; // Holds the average voltage measurement
- int adcSamples[SAMPLE_NUMBER]; // Array to hold each voltage measurement
- /* Calculate thermistor's average resistance:
- As mentioned in the top of the code, we will sample the ADC pin a few times
- to get a bunch of samples. A slight delay is added to properly have the
- analogRead function sample properly */
- for (int i = 0; i < SAMPLE_NUMBER; i++)
- {
- adcSamples[i] = analogRead(thermistorPin); // read from pin and store
- delay(1); // wait 10 milliseconds
- }
- /* Then, we will simply average all of those samples up for a "stiffer"
- measurement. */
- for (int i = 0; i < SAMPLE_NUMBER; i++)
- {
- adcAverage += adcSamples[i]; // add all samples up . . .
- }
- adcAverage /= SAMPLE_NUMBER; // . . . average it w/ divide
- /* Here we calculate the thermistor’s resistance using the equation
- discussed in the article. */
- rThermistor = BALANCE_RESISTOR * ( (MAX_ADC / adcAverage) - 1);
- /* Here is where the Beta equation is used, but it is different
- from what the article describes. Don't worry! It has been rearranged
- algebraically to give a "better" looking formula. I encourage you
- to try to manipulate the equation from the article yourself to get
- better at algebra. And if not, just use what is shown here and take it
- for granted or input the formula directly from the article, exactly
- as it is shown. Either way will work! */
- tKelvin = (BETA * ROOM_TEMP) /
- (BETA + (ROOM_TEMP * log(rThermistor / RESISTOR_ROOM_TEMP)));
- /* I will use the units of Celsius to indicate temperature. I did this
- just so I can see the typical room temperature, which is 25 degrees
- Celsius, when I first try the program out. I prefer Fahrenheit, but
- I leave it up to you to either change this function, or create
- another function which converts between the two units. */
- tCelsius = tKelvin - 273.15; // convert kelvin to celsius
- tCelsius = round(tCelsius*10)/10.0;
- return tCelsius; // Return the temperature in Celsius
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement