Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#include <LowPower.h>
#include <SoftwareSerial.h>
#include "DHT.h"
#include <avr/wdt.h>
#include <avr/sleep.h>
#include <avr/pgmspace.h>
#include <stdlib.h>
/////////////////////////////////
#define sensorId "30"
/////////////////////////////////
boolean chipGood = false;
boolean debugOutputNeeded = false;
#define SSID ""
#define PASS ""
#define DST_IP "192.168.1.13" //baidu.com
#define DHTPIN 2 // what pin we're connected to
#define DHTTYPE DHT22
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
pinMode(13, OUTPUT);
pinMode(12, OUTPUT);
pinMode(8, OUTPUT);
pinMode(3, OUTPUT);
turnSensorOff(true);
turnChipOff(false);
digitalWrite(12, HIGH);
digitalWrite(13, HIGH);
delay(700);
digitalWrite(13, LOW);
digitalWrite(12, LOW);
Serial.begin(9600);
Serial.setTimeout(5000);
while(chipGood == false){
Serial.println("AT+RST");
delay(1000);
if(Serial.find("ready"))
{
chipGood = true;
}
else
{
turnChipOff(true);
turnChipOff(false);
delay(100);
}
}
delay(500);
//connect to the wifi
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){
while(1);
} else {
digitalWrite(12, HIGH);
delay(300);
digitalWrite(12, LOW);
delay(300);
digitalWrite(12, HIGH);
delay(300);
digitalWrite(12, LOW);
delay(300);
digitalWrite(12, HIGH);
delay(300);
digitalWrite(12, LOW);
turnSensorOff(false);
}
delay(2000);
Serial.println("AT+CIFSR");
while (Serial.available()){
Serial.read();
}
Serial.println("AT+CIPMUX=0");
}
void sleep1()
{
turnChipOff(true);
for (int i = 0; i < 75; i++) { // 75 = 10min
LowPower.powerDown(SLEEP_8S, ADC_OFF, BOD_OFF);
}
turnChipOff(false);
}
////////////////////////////////////////////
void loop()
////////////////////////////////////////////
{
turnSensorOff(false);
delay(300);
String temperatureData = readTemp();
turnSensorOff(true);
int temperatureDataStringLength = temperatureData.length();
bool success = false;
if (temperatureDataStringLength > 1){
success = sendStuff(temperatureData);
}
if (success){
sleep1();
} else {
delay(1000);
}
}
boolean sendStuff(String temperatureData)
{
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += DST_IP;
cmd += "\",8080";
Serial.println(cmd);
if(Serial.find("Error")){
return false;
}
cmd = "GET /update.php"+temperatureData;
cmd += " HTTP/1.0\r\nHost: ";
cmd += DST_IP;
cmd += "\r\n\r\n";
Serial.print("AT+CIPSEND=");
Serial.println(cmd.length());
if(Serial.find(">"))
{
digitalWrite(12, HIGH);
delay(100);
digitalWrite(12, LOW);
}else
{
Serial.println("AT+CIPCLOSE");
delay(500);
return false;
}
Serial.print(cmd);
delay(500);
return true;
}
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
delay(2000);
if(Serial.find("OK"))
{
digitalWrite(7, HIGH);
return true;
}else
{
digitalWrite(7, LOW);
return false;
}
}
void turnChipOff(boolean turnOff)
{
if (turnOff){
digitalWrite(8, LOW);
delay(100);
} else {
digitalWrite(8, HIGH);
delay(100);
}
}
void turnSensorOff(boolean turnOff)
{
if (turnOff){
digitalWrite(3, LOW);
delay(100);
} else {
digitalWrite(3, HIGH);
delay(2400);
}
}
String readTemp ()
{
char tBuff[6];
char hBuff[6];
float h = dht.readHumidity();
float t = dht.readTemperature();
if ((!isnan(t) && !isnan(h)) && h > 0 && t > 0) {
String tString = dtostrf(t, 2, 2, tBuff);
String hString = dtostrf(h, 2, 2, hBuff);
String sId = String(sensorId);
String voltage = String(readVcc());
String outString = "?sid="+sId+"&temperature="+tString+"&humidity="+hString+"&voltage="+voltage;
return outString;
// Serial.println("data: "+ outString);
} else {
digitalWrite(13, HIGH);
delay(500);
digitalWrite(13, LOW);
return "";
}
}
//-----------------------------------------------------------------------------
//Code from https://code.google.com/p/tinkerit/wiki/SecretVoltmeter
//-----------------------------------------------------------------------------
int readVcc() {
long result;
// Read 1.1V reference against AVcc
ADMUX = _BV(REFS0) | _BV(MUX3) | _BV(MUX2) | _BV(MUX1);
delay(2); // Wait for Vref to settle
ADCSRA |= _BV(ADSC); // Convert
while (bit_is_set(ADCSRA,ADSC));
result = ADCL;
result |= ADCH<<8;
result = 1126400L / result; // Back-calculate AVcc in mV
return (int)result;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.