Help with my coding I am new
Posted: Tue Oct 27, 2015 7:43 am
Good Day
I baught the WiFi Serial Transceiver Module Breakout Board w/ ESP8266.
Need help as my coding connects to access point but cant get data from my temp/humidity sensor every time.
What do i do wrong? Any other better way to do this?
Here is my code:
#include <DHT.h>
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
#define SSID "SM_OFFICE_AP"
#define PASS "**************"
#define BUFFER_SIZE 128
#define DHTTYPE DHT22
#define DHTPIN 2
byte TempHi;
byte TempLo;
boolean P_N;
unsigned int Decimal;
float combined_temp_C;
float combined_temp_F;
char * strTempC = NULL;
char * strTempF = NULL;
char buffer[BUFFER_SIZE];
DHT dht(DHTPIN, DHT22);
void setup() {
lcd.begin(16, 2);
lcd.setRGB(0, 255, 0);
Serial.begin(9600);
Serial.setTimeout(5000);
Serial.println("AT+RST\r\n");
delay(1000);
if(Serial.find("ready"))
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Module ready");
}
else
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("No response.");
while(1);
}
delay(1000);
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){while(1);}
delay(1000);
Serial.println("AT+CIPMUX=1\r\n");
Serial.println("AT+CIPSERVER=1,1084\r\n");
Serial.println("AT+CIPSTO=60\r\n");
}
void loop() {
int con_id, packet_len;
char *pb;
Serial.readBytesUntil('\n', buffer, BUFFER_SIZE);
if(strncmp(buffer, "+IPD,", 5)==0) {
sscanf(buffer+5, "%d,%d", &con_id, &packet_len);
if (packet_len > 0) {
pb = buffer+5;
while(*pb!=':') pb++;
pb++;
}
delay(1000);
lcd.setCursor(13, 0);
lcd.print(con_id);
delay(10);
senddata(con_id);
}
}
//----------------------------------------------------------
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1\r\n");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd + "\r\n");
delay(2000);
if(Serial.find("OK"))
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Connected");
return true;
}else
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Failed");
return false;
}
}
void senddata(int con_id) {
String Datai;
Datai = String(dht.readTemperature());
Datai += String(";");
Datai += String(dht.readHumidity());
Datai += String(";");
Datai += String(dewPoint(dht.readTemperature(), dht.readHumidity()));
Datai += String("\r\n");
Serial.print("AT+CIPSEND=");
Serial.print(con_id);
Serial.print(",");
Serial.print(Datai.length());
delay(100);
if (Serial.find(">")) {
Serial.println(Datai);
delay(100);
}
}
//-------------------------------------------------------
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);
return (241.88 * T) / (17.558-T);
}
void Cal_Temp()
{
if (TempHi&0x80)
P_N = 0;
else
P_N = 1;
TempHi = TempHi & 0x7F;
TempLo = TempLo & 0xF0;
TempLo = TempLo >>4;
Decimal = TempLo;
Decimal = Decimal * 625;
combined_temp_C= TempHi + TempLo*.0625;
combined_temp_F = combined_temp_C*1.8+32;
sprintf(strTempC, "%f", combined_temp_C);
sprintf(strTempF, "%f", combined_temp_F);
}
Hope anyone can help . . .
Thank You
Albertus Geyser
I baught the WiFi Serial Transceiver Module Breakout Board w/ ESP8266.
Need help as my coding connects to access point but cant get data from my temp/humidity sensor every time.
What do i do wrong? Any other better way to do this?
Here is my code:
#include <DHT.h>
#include <Wire.h>
#include "rgb_lcd.h"
rgb_lcd lcd;
#define SSID "SM_OFFICE_AP"
#define PASS "**************"
#define BUFFER_SIZE 128
#define DHTTYPE DHT22
#define DHTPIN 2
byte TempHi;
byte TempLo;
boolean P_N;
unsigned int Decimal;
float combined_temp_C;
float combined_temp_F;
char * strTempC = NULL;
char * strTempF = NULL;
char buffer[BUFFER_SIZE];
DHT dht(DHTPIN, DHT22);
void setup() {
lcd.begin(16, 2);
lcd.setRGB(0, 255, 0);
Serial.begin(9600);
Serial.setTimeout(5000);
Serial.println("AT+RST\r\n");
delay(1000);
if(Serial.find("ready"))
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Module ready");
}
else
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("No response.");
while(1);
}
delay(1000);
boolean connected=false;
for(int i=0;i<5;i++)
{
if(connectWiFi())
{
connected = true;
break;
}
}
if (!connected){while(1);}
delay(1000);
Serial.println("AT+CIPMUX=1\r\n");
Serial.println("AT+CIPSERVER=1,1084\r\n");
Serial.println("AT+CIPSTO=60\r\n");
}
void loop() {
int con_id, packet_len;
char *pb;
Serial.readBytesUntil('\n', buffer, BUFFER_SIZE);
if(strncmp(buffer, "+IPD,", 5)==0) {
sscanf(buffer+5, "%d,%d", &con_id, &packet_len);
if (packet_len > 0) {
pb = buffer+5;
while(*pb!=':') pb++;
pb++;
}
delay(1000);
lcd.setCursor(13, 0);
lcd.print(con_id);
delay(10);
senddata(con_id);
}
}
//----------------------------------------------------------
boolean connectWiFi()
{
Serial.println("AT+CWMODE=1\r\n");
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd + "\r\n");
delay(2000);
if(Serial.find("OK"))
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Connected");
return true;
}else
{
lcd.setCursor(1, 1);
lcd.clear();
lcd.print("Failed");
return false;
}
}
void senddata(int con_id) {
String Datai;
Datai = String(dht.readTemperature());
Datai += String(";");
Datai += String(dht.readHumidity());
Datai += String(";");
Datai += String(dewPoint(dht.readTemperature(), dht.readHumidity()));
Datai += String("\r\n");
Serial.print("AT+CIPSEND=");
Serial.print(con_id);
Serial.print(",");
Serial.print(Datai.length());
delay(100);
if (Serial.find(">")) {
Serial.println(Datai);
delay(100);
}
}
//-------------------------------------------------------
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity/100);
double Td = (b * temp) / (a - temp);
return Td;
}
double dewPoint(double celsius, double humidity)
{
double A0= 373.15/(273.15 + celsius);
double SUM = -7.90298 * (A0-1);
SUM += 5.02808 * log10(A0);
SUM += -1.3816e-7 * (pow(10, (11.344*(1-1/A0)))-1) ;
SUM += 8.1328e-3 * (pow(10,(-3.49149*(A0-1)))-1) ;
SUM += log10(1013.246);
double VP = pow(10, SUM-3) * humidity;
double T = log(VP/0.61078);
return (241.88 * T) / (17.558-T);
}
void Cal_Temp()
{
if (TempHi&0x80)
P_N = 0;
else
P_N = 1;
TempHi = TempHi & 0x7F;
TempLo = TempLo & 0xF0;
TempLo = TempLo >>4;
Decimal = TempLo;
Decimal = Decimal * 625;
combined_temp_C= TempHi + TempLo*.0625;
combined_temp_F = combined_temp_C*1.8+32;
sprintf(strTempC, "%f", combined_temp_C);
sprintf(strTempF, "%f", combined_temp_F);
}
Hope anyone can help . . .
Thank You
Albertus Geyser