Skip to content

Instantly share code, notes, and snippets.

@AutomationD
Created July 7, 2015 22:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AutomationD/1f512ef4cbfb09ac918c to your computer and use it in GitHub Desktop.
Save AutomationD/1f512ef4cbfb09ac918c to your computer and use it in GitHub Desktop.
Arduino 2 Nodemcu Passthrough
#include <dht.h>
#define DHT11_PIN 4
dht DHT;
HardwareSerial trm = Serial2;
HardwareSerial esp = Serial1;
HardwareSerial dbg = Serial;
const int moistureAO = 7;
float moisture = 0.0;
float temperature = 0.0;
float humidity = 0.0;
boolean flash = false;
void setup() {
dbg.begin(9600);
esp.begin(9600);
trm.begin(9600);
pinMode(moistureAO, INPUT);
delay(2000);
dbg.println("Restarting esp8266");
execLuaCommand("node.restart()");
while (!esp.find("IP:")) {
dbg.println("Waiting for nodemcu IP");
delay(1000);
}
dbg.println("Aquired IP address");
dbg.println("Executing main program");
execLuaCommand("run()");
dbg.println("Executed. Entering loop.");
}
void loop() {
if (flash) {
if (trm.available()) {
char inByte = trm.read();
esp.print(inByte);
}
if (esp.available()) {
char inByte = esp.read();
trm.print(inByte);
}
} else {
if (esp.available()) {
if (esp.find("GET DATA")) {
dbg.println("GET DATA");
getDHT();
getMoisture();
postData();
}
}
delay(1000);
}
}
void getDHT() {
int chk = DHT.read11(DHT11_PIN);
switch (chk)
{
case DHTLIB_OK:
temperature = DHT.temperature;
humidity = DHT.humidity;
break;
case DHTLIB_ERROR_CHECKSUM:
dbg.println("error('Checksum error')");
break;
case DHTLIB_ERROR_TIMEOUT:
dbg.println("error('Time out error'");
break;
default:
dbg.println("error('Unknown error')");
break;
}
}
void getMoisture () {
int tmp=analogRead( moistureAO );
if ( tmp != moisture )
{
moisture=tmp;
}
}
void postData() {
esp.print("{\"");
esp.print("t");
esp.print("\":");
esp.print(temperature,1);
esp.print(",");
esp.print("\"");
esp.print("h");
esp.print("\":");
esp.print(humidity,1);
esp.print(",");
esp.print("\"");
esp.print("m");
esp.print("\":");
esp.print(moisture,1);
esp.print("}");
esp.print("\n");
dbg.print("{\"");
dbg.print("t");
dbg.print("\":");
dbg.print(temperature,1);
dbg.print(",");
dbg.print("\"");
dbg.print("h");
dbg.print("\":");
dbg.print(humidity,1);
dbg.print(",");
dbg.print("\"");
dbg.print("m");
dbg.print("\":");
dbg.print(moisture,1);
dbg.print("}");
dbg.print("\n");
}
void execLuaCommand(char* command) {
delay(1000);
Serial1.println("");
Serial1.println(command);
delay(1000);
}
boolean find(char* command) {
return Serial1.find(command);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment