Sie sind auf Seite 1von 3

#include <SoftwareSerial.

h>
//period between posts, set at 60 seconds
#define DELAY_PERIOD 60000

// Important!! We use pin 13 for enable esp8266


#define WIFI_ENABLE_PIN 13
#define SSID "PLDTMyDSL"
#define PASS "lopez1234"
#define PUBLIC_KEY "o8zmOw4OKrHaVrng2822"
#define PRIVATE_KEY "yzlAnrenX9uMPoGwZ9ZZ"
#define DEBUG 0

char serialbuffer[100];//serial buffer for request command


long nextTime;//next time in millis that the temperature read will fire
int wifiConnected = 0;
SoftwareSerial mySerial(2, 3); // rx, tx
const int sensorPin = A2; // pin A2
int mapped;
int constrained = 0;
int sensorValue;
int pulse = 0;
float energy = 0;
float price = 0;
void setup()
{
mySerial.begin(9600);//connection to ESP8266
Serial.begin(9600);// .initializing
if(DEBUG) {
while(!Serial);
}
//set mode needed for new boards
mySerial.println("AT+RST");
delay(3000);//delay after mode change
mySerial.println("AT+CWMODE=1");
delay(300);
mySerial.println("AT+RST");
delay(500);
nextTime = millis();//reset the timer
}
boolean connectWifi() {
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
Serial.println(cmd);
mySerial.println(cmd);
for(int i = 0; i < 20; i++) {
Serial.print(".");
if(mySerial.find("OK"))
{
wifiConnected = 1;
break;
}
delay(50);
}
Serial.println(
wifiConnected ?
"OK, Connected to WiFi." :
"Can not connect to the WiFi."
);
return wifiConnected;
}
void loop()
{
if(!wifiConnected) {
mySerial.println("AT");
delay(1000);
if(mySerial.find("OK")){
Serial.println("Module Test: OK");
connectWifi();
}
}
if(!wifiConnected) {
delay(500);
return;
}
//output everything from ESP8266 to the Arduino Micro Serial output
while (mySerial.available() > 0) {
Serial.write(mySerial.read());
}

//wait for timer to expire


if(nextTime<millis()){
Serial.print("timer reset: ");
Serial.println(nextTime);
//reset timer
nextTime = millis() + DELAY_PERIOD;

//send energy
SendEnergyData(energy);
}
//instruction to send data
void SendEnergyData(float temperature){
char temp[10];
Serial.print("temp: ");
Serial.println(temperature);

//create start command


String startcommand = "AT+CIPSTART=\"TCP\",\"data.sparkfun.com\", 80";
mySerial.println(startcommand);
Serial.println(startcommand);
//test for a start error
if(mySerial.find("Error")){
Serial.println("error on start");
return;
}
//create the request command
String sendcommand = "GET /input/";
sendcommand.concat(PUBLIC_KEY);
sendcommand.concat("?private_key=");
sendcommand.concat(PRIVATE_KEY);
sendcommand.concat("&temp=");
sendcommand.concat(String(temp));
sendcommand.concat("\r\n");
//sendcommand.concat(" HTTP/1.0\r\n\r\n");
Serial.println(sendcommand);

//send to ESP8266
mySerial.print("AT+CIPSEND=");
mySerial.println(sendcommand.length());
//display command in serial monitor
Serial.print("AT+CIPSEND=");
Serial.println(sendcommand.length());
if(mySerial.find(">"))
{
Serial.println(">");
}else
{
mySerial.println("AT+CIPCLOSE");
Serial.println("connect timeout");
delay(1000);
return;
}
//Serial.print("Debug:");
//Serial.print(sendcommand);
mySerial.print(sendcommand);
delay(1000);
mySerial.println("AT+CIPCLOSE");
}

Das könnte Ihnen auch gefallen