Last active
August 29, 2015 14:13
-
-
Save teos0009/c6ccea717d6a35db1a0c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
//#include <SoftwareSerial.h> | |
//use mega Serial 2 for serial monitor; Serial 1 on pins 19 (RX) and 18 (TX);// Serial2 on pins 17 (RX) and 16 (TX), Serial3 on pins 15 (RX) and 14 (TX). | |
//DS18B20 uses onewire protocol; need 5k pullup | |
#include <OneWire.h> | |
#include <DallasTemperature.h> | |
#define SSID "SSID_HERE" | |
#define PASS "PASSWORD_HERE" | |
#define DST_IP "220.181.111.85" //baidu.com | |
#define thingspeakIP "184.106.153.149" // thingspeak.com | |
//SoftwareSerial dbgSerial(10, 11); // RX, TX | |
#define acPin 7 | |
char myChar; | |
//Data wire is plugged into pin A1 on the Arduino | |
#define ONE_WIRE_BUS A1 | |
// Setup a oneWire instance to communicate with any OneWire devices | |
// (not just Maxim/Dallas temperature ICs) | |
OneWire oneWire(ONE_WIRE_BUS); | |
// Pass our oneWire reference to Dallas Temperature. | |
DallasTemperature ds18B20(&oneWire); | |
//thingspeak API | |
String GET = "GET /update?key=API_KEY_HERE"; | |
//my write key: APIKEY1 | |
//my read key: APIKEY2 | |
//my IoT Channel: 22051 | |
void setup() | |
{ | |
// Start up the library for ds18B20 | |
ds18B20.begin(); | |
// Open serial communications and wait for port to open: | |
//serial 2 is to esp8266 | |
Serial2.begin(9600);//9600 (mine), 57600, 115200 | |
Serial2.setTimeout(1000); | |
//serial 0 is to usb | |
Serial.begin(115200); | |
//mod1 otherwise module cant be ready | |
while(!Serial); | |
while(!Serial2); | |
Serial.println("ESP8266 on Mega2560 to thingspeak IoT "); | |
//mod1 otherwise module cant be ready | |
while(Serial2.available()>0) | |
Serial2.read(); | |
delay(1000); | |
//test if the module is ready by reset | |
//Serial2.println("AT+RST");// high chance err: module not responding | |
Serial2.println("AT");//test esp8266 via "AT" instead | |
//delay(1000); | |
//delay(1000); | |
Serial.println("Resetting module"); | |
//mod1 | |
//Serial2.flush(); | |
//if(Serial2.find("ready")) | |
if(Serial2.find("OK")) | |
//if(Serial2.find("Ready")||Serial2.find("ready")||Serial2.find("OK")) | |
{ | |
//dbgSerial.println("Module is ready"); | |
Serial.println("Module is ready"); | |
} | |
else | |
{ | |
//dbgSerial.println("Module have no response."); | |
Serial.println("Module have no response."); | |
while(1);//hang here | |
} | |
delay(1000); | |
//connect to the wifi | |
boolean connected=false; | |
for(int i=0;i<5;i++) | |
{ | |
if(connectWiFi()) | |
{ | |
Serial.println(" WiFi connected"); | |
connected = true; | |
break; | |
} | |
} | |
if (!connected){ | |
Serial.println("No WiFi connected"); | |
while(1); | |
} | |
delay(5000); | |
//print the ip addr | |
Serial2.println("AT+CIFSR=?"); | |
Serial.println("IP address:"); | |
while (Serial2.available()) | |
Serial.write(Serial2.read()); | |
//set the single connection mode | |
//Serial2.println("AT+CIPMUX=0");//default single conn | |
//LEDserver use | |
//startLEDserver(); | |
} | |
void loop() | |
{ | |
/* | |
//esp8266 as browser to open baidu.com | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += DST_IP; | |
cmd += "\",80"; | |
Serial2.println(cmd); | |
//dbgSerial.println(cmd); | |
Serial.println(cmd); | |
if(Serial2.find("Error")) return; | |
cmd = "GET / HTTP/1.0\r\n\r\n"; | |
Serial2.print("AT+CIPSEND="); | |
Serial2.println(cmd.length()); | |
if(Serial2.find(">")) | |
{ | |
//dbgSerial.print(">"); | |
Serial.print(">"); | |
}else | |
{ | |
Serial2.println("AT+CIPCLOSE"); | |
//dbgSerial.println("connect timeout"); | |
Serial.println("connect timeout"); | |
delay(1000); | |
return; | |
} | |
Serial2.print(cmd); | |
delay(2000); | |
//Serial.find("+IPD"); | |
while (Serial2.available()) | |
{ | |
char c = Serial2.read(); | |
//dbgSerial.write(c); | |
Serial.write(c); | |
//if(c=='\r') dbgSerial.print('\n'); | |
if(c=='\r') Serial.print('\n'); | |
} | |
//dbgSerial.println("===="); | |
Serial.println("===="); | |
delay(1000); | |
*/ | |
/* LED server | |
//Serial.println("waiting for cmd"); | |
if (Serial2.available()>0){ | |
if(Serial2.find("LED")){ | |
setLED(); | |
} | |
} | |
*/ | |
float temp = readThermo();// read DS18B20 | |
char buffer[10]; | |
String tempC = dtostrf(temp, 4, 1, buffer); | |
updateThingSpeakTemp(tempC); | |
delay(60000);//read every 60sec | |
} | |
boolean connectWiFi() | |
{ | |
Serial2.println("AT+CWMODE=1"); | |
String cmd="AT+CWJAP=\""; | |
cmd+=SSID; | |
cmd+="\",\""; | |
cmd+=PASS; | |
cmd+="\""; | |
//dbgSerial.println(cmd); | |
Serial2.println(cmd); | |
Serial.println(cmd); | |
delay(2000); | |
if(Serial2.find("OK")) | |
{ | |
//dbgSerial.println("OK, Connected to WiFi."); | |
Serial.println("OK, Connected to WiFi."); | |
return true; | |
} | |
else | |
{ | |
//dbgSerial.println("Can not connect to the WiFi."); | |
Serial.println("Can not connect to the WiFi."); | |
return false; | |
} | |
} | |
void setLED(){ | |
int red = Serial.parseInt(); | |
int green = Serial.parseInt(); | |
int blue = Serial.parseInt(); | |
Serial.print("LED set. Red: "); | |
Serial.print(red); | |
Serial.print(" Green "); | |
Serial.print(green); | |
Serial.print(" Blue "); | |
Serial.println(blue); | |
//colorWipe(strip.Color(red,green,blue),200); | |
//simple test AC on/off for 2sec | |
Serial.println("Set AC on/off"); | |
digitalWrite(acPin,HIGH); | |
delay(2000); | |
digitalWrite(acPin,LOW); | |
} | |
float readThermo() {//input pin# | |
//using ds18B20 | |
ds18B20.requestTemperatures(); | |
float val = ds18B20.getTempCByIndex(0); | |
return val; // Return the Temperature in degC | |
} | |
void startLEDserver(){ | |
//LED server use | |
Serial2.println("AT+CIPMUX=1");//led server multi conn | |
delay(1000); | |
Serial2.println("AT+CIPSERVER=1,8266"); | |
delay(1000); | |
Serial.println("Starting TCP Server"); | |
} | |
void updateThingSpeakTemp(String tempC){ | |
String cmd = "AT+CIPSTART=\"TCP\",\""; | |
cmd += thingspeakIP; | |
cmd += "\",80"; | |
sendDebug(cmd); | |
delay(2000); | |
if(Serial2.find("Error")){ | |
Serial.print("RECEIVED: Error"); | |
return; | |
} | |
cmd = GET; | |
cmd += tempC; | |
cmd += "\r\n"; | |
Serial2.print("AT+CIPSEND="); | |
Serial2.println(cmd.length()); | |
if(Serial2.find(">")){ | |
Serial.print(">"); | |
Serial.print(cmd); | |
Serial2.print(cmd); | |
}else{ | |
sendDebug("AT+CIPCLOSE"); | |
} | |
if(Serial2.find("OK")){ | |
Serial.println("RECEIVED: OK"); | |
}else{ | |
Serial.println("RECEIVED: Error"); | |
} | |
} | |
void sendDebug(String cmd){ | |
Serial.print("SEND: "); | |
Serial.println(cmd); | |
//send cmd | |
Serial2.println(cmd); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment