question ESP8266

9 years 10 months ago #25 by kindr
question ESP8266 was created by kindr
Hi, I want to ask if it is possible to merge Scripts for control of relays and temperature and humidity measurements into one?

Please Log in or Create an account to join the conversation.

9 years 10 months ago #26 by EasyIoT
Replied by EasyIoT on topic question ESP8266

kindr wrote: Hi, I want to ask if it is possible to merge Scripts for control of relays and temperature and humidity measurements into one?


If you mean by Scripts Arduino programs the answer is yes. You can use realay switch and temperature/humidity sensor in one Arduino node.

Please Log in or Create an account to join the conversation.

9 years 10 months ago #27 by kindr
Replied by kindr on topic question ESP8266
it would be possible to send an example? I am only a beginner for which I apologize

Please Log in or Create an account to join the conversation.

9 years 10 months ago #28 by EasyIoT
Replied by EasyIoT on topic question ESP8266

kindr wrote: it would be possible to send an example? I am only a beginner for which I apologize


Try this. Code was not tested, but you'll get the idea.

#include <Esp8266EasyIoT.h>
#include <SoftwareSerial.h>
#include <DHT.h>

#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define HUMIDITY_SENSOR_DIGITAL_PIN 2

#define RELAY_1 13 // Arduino Digital I/O pin number for first relay (second on pin+1 etc)
#define RELAY_ON 1 // GPIO value to write to turn on attached relay
#define RELAY_OFF 0 // GPIO value to write to turn off attached relay


Esp8266EasyIoT esp;

SoftwareSerial serialEsp(10, 11);


DHT dht;
float lastTemp;
float lastHum;

Esp8266EasyIoTMsg msgHum(CHILD_ID_HUM, V_HUM);
Esp8266EasyIoTMsg msgTemp(CHILD_ID_TEMP, V_TEMP);


void setup()
{
serialEsp.begin(9600);
Serial.begin(115200);

Serial.println("EasyIoTEsp init");


esp.begin(incomingMessage, 3, &serialEsp, &Serial);
//esp.begin(NULL, &serialEsp);
dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN);

pinMode(13, OUTPUT);

// Serial.println("present S_HUM");
esp.present(CHILD_ID_HUM, S_HUM);

// Serial.println("present S_TEMP");
esp.present(CHILD_ID_TEMP, S_TEMP);

pinMode(RELAY_1, OUTPUT);

esp.present(3, S_DIGITAL_OUTPUT);

}

void loop()
{
while(!esp.process());

delay(dht.getMinimumSamplingPeriod());

while(!esp.process());

float temperature = dht.getTemperature();
if (isnan(temperature)) {
Serial.println("Failed reading temperature from DHT");
}
else if (temperature != lastTemp)
{
lastTemp = temperature;
esp.send(msgTemp.set(temperature, 1));
Serial.print("T: ");
Serial.println(temperature);
}

float humidity = dht.getHumidity();
if (isnan(humidity)) {
Serial.println("Failed reading humidity from DHT");
}
else if (humidity != lastHum)
{
lastHum = humidity;
esp.send(msgHum.set(humidity, 1));
Serial.print("H: ");
Serial.println(humidity);
}
}


void incomingMessage(const Esp8266EasyIoTMsg &message) {
// We only expect one type of message from controller. But we better check anyway.

Serial.println("New message");
if (message.type==S_DIGITAL_OUTPUT) {
// Change relay state
digitalWrite(message.sensor-3+RELAY_1, message.getBool()?RELAY_ON:RELAY_OFF);

Serial.print("Incoming change for sensor:");
Serial.print(message.sensor);
Serial.print(", New status: ");
Serial.println(message.getBool());
}
}
The following user(s) said Thank You: PupazzoGnappo, kindr

Please Log in or Create an account to join the conversation.

9 years 10 months ago #29 by kindr
Replied by kindr on topic question ESP8266
It is absolutely amazing, thank you very much
and if I could have one more question can have on one node more than one temperature sensor? if so how do I modify the code for the Arduino ... again many thanks for the reply ...

Please Log in or Create an account to join the conversation.

Time to create page: 0.296 seconds

Forum latest

  • No posts to display.