#include "ACS712.h"
// Code to use SoftwareSerial
#include <SoftwareSerial.h>
SoftwareSerial espSerial = SoftwareSerial(2,3); // arduino RX pin=2 arduino TX pin=3 connect the arduino RX pin to esp8266 module TX pin - connect the arduino TX pin to esp8266 module RX pin
ACS712 sensor(ACS712_30A, A1);
#include <DHT.h> //Attention: For new DHT11 version library you will need the Adafruit_Sensor library
#define DHTPIN 12 // Connect the signal pin of DHT11 sensor to digital pin 5
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
const int mq3 = 0;
const int AOUTpin=0;//the AOUT pin of the alcohol sensor goes into analog pin A0 of the arduino
const int DOUTpin=8;//the DOUT pin of the alcohol sensor goes into digital pin D8 of the arduino
const int ledPin=13;//the anode of the LED connects to digital pin D13 of the arduino
int limit;
int value;
String apiKey = "DVXLJUGQVUUKQXZN"; // replace with your channel's thingspeak WRITE API key
//======================================================================== showResponce
void showResponse(int waitTime){
long t=millis();
char c;
while (t+waitTime>millis()){
if (espSerial.available()){
c=espSerial.read();
if (DEBUG) Serial.print(c);
}
}
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
espSerial.println(cmd);
if (DEBUG) Serial.println(cmd);
delay(1000);
if(espSerial.find((char*)">")){
espSerial.print(getStr);
if (DEBUG) Serial.print(getStr);
}
else{
espSerial.println("AT+CIPCLOSE");
// alert user
if (DEBUG) Serial.println("AT+CIPCLOSE");
return false;
}
return true;
}
//================================================================================ setup
void setup() {
DEBUG=true; // enable debug serial
Serial.begin(9600);
pinMode(DOUTpin, INPUT);//sets the pin as an input to the arduino
pinMode(ledPin, OUTPUT);
sensor.calibrate();
dht.begin();
espSerial.begin(9600); // enable software serial
// Your esp8266 module's speed is probably at 115200.
// For this reason the first time set the speed to 115200 or to your esp8266 configured speed
// and upload. Then change to 9600 and upload again
//espSerial.println("AT+RST"); // Enable this line to reset the module;
//showResponse(1000);
//espSerial.println("AT+UART_CUR=9600,8,1,0,0"); // Enable this line to set esp8266 serial speed to 9600 bps
//showResponse(1000);
espSerial.println("AT+CWMODE=1"); // set esp8266 as client
showResponse(1000);
espSerial.println("AT+CWJAP=\""+ssid+"\",\""+password+"\""); // set your home router SSID and password
showResponse(5000);
if (DEBUG) Serial.println("Setup completed");
}
// ====================================================================== loop
void loop() {
value = analogRead(AOUTpin); //reads the analaog value from the alcohol sensor's AOUT pin
limit= digitalRead(DOUTpin);//reads the digital value from the alcohol sensor's DOUT pin
// Read sensor values
float I = sensor.getCurrentDC();
float t = dht.readTemperature();
float h = dht.readHumidity();
if (isnan(t) || isnan(h)) {
if (DEBUG) Serial.println("Failed to read from DHT");
}
else {
if (DEBUG) Serial.println("Temp="+String(t)+" *C");
if (DEBUG) Serial.println("Humidity="+String(h)+" %");
if (DEBUG) Serial.println(String("I = ") + I + " A");
// Write values to thingspeak
}
if (isnan(value) || isnan(limit)) {
if (DEBUG) Serial.println("Failed to read from Mq3");
}
else {
if (DEBUG) Serial.println("alcohol="+String(value));
if (DEBUG) Serial.println("lim="+String(limit));
thingSpeakWrite(value,limit,t,h,I); // Write values to thingspeak
}
delay(20000);
}
以下是Lora接收端的程式碼
#include <SPI.h>
#include <RH_RF95.h>
//When use LG01, uncomment this line, so print the result in Console.
//When use LoRa Mini Dev, Comment this link
//#define LG01_GATEWAY
// Setup ISM frequency
rf95.setFrequency(frequency);
// Setup Power,dBm
rf95.setTxPower(13);
// Defaults BW Bw = 125 kHz, Cr = 4/5, Sf = 128chips/symbol, CRC on
SerialPrint.print("Listening on frequency: ");
SerialPrint.println(frequency);
}
void loop()
{
if (rf95.available())
{
// Should be a message for us now
char buf[RH_RF95_MAX_MESSAGE_LEN];
char len = sizeof(buf);
if (rf95.recv(buf, &len))
{
if(buf[0] == 1 & buf[1] == 1 & buf[2] ==1) //Get sensor data from node id 111
{
int newData[6] = {0, 0, 0, 0, 0, 0};
for (int i = 0; i < 4; i++)
{
newData[i] = buf[i +3];
}
int a = newData[0];
int b = newData[1];
int c = newData[2];
float d = newData[3];
SerialPrint.println("ID = 111 :");
SerialPrint.print("Current humdity = ");
SerialPrint.print(a);
SerialPrint.print("% ");
SerialPrint.print("temperature = ");
SerialPrint.print(b);
SerialPrint.println("C ");
SerialPrint.print("酒精值:");
SerialPrint.println(c);
SerialPrint.print("I=");
SerialPrint.print(d);
SerialPrint.println("A");
}
if(buf[0] == 1 & buf[1] == 1 & buf[2] == 2) //Get sensor data from node id 112
{
int newData[4] = {0, 0, 0, 0};
for (int i = 0; i < 2; i++)
{
newData[i] = buf[i + 3];
}
int a = newData[0];
int b = newData[1];
SerialPrint.print("ID = 112 :");
SerialPrint.print("Current humdity = ");
SerialPrint.print(a);
SerialPrint.print("% ");
SerialPrint.print("temperature = ");
SerialPrint.print(b);
SerialPrint.println("C ");
}
}
else
{
SerialPrint.println("recv failed");
}
}
}