我正试图通过ESP8266将温度数据从arduino发送到thingpeak,但我没有配置我的WiFi的代码。我正在搜索过去两天的代码,但没有工作。请提供代码,使用SSID和密码连接到我的本地wifi。
#include <SoftwareSerial.h>
#define BUFFER_SIZE 512
#define GET_SIZE 64
#define dbg Serial // USB local debug
#include <dht11.h>
dht11 DHT11;
#define DHT11PIN 7
int ledPin = 13;
int lightLevel;
String apiKey = "20YMPJA0XII0IEF4";
SoftwareSerial ser(2,3);
SoftwareSerial esp(11, 12);
String ssid = "ATL-WL";
String pass = "atl#yd81";
String serverPort = "80";
char buffer[BUFFER_SIZE]; // Don't touch
char get_s[GET_SIZE];
char OKrn[] = "OK\r\n"; // Don't touch
String currentCommand = "0000";
byte waitForEsp(int timeout, char* term = OKrn) {
unsigned long t = millis();
bool found = false;
int i = 0;
int len = strlen(term);
while (millis() < t + timeout) {
if (esp.available()) {
buffer[i++] = esp.read();
if (i >= len) {
if (strncmp(buffer + i - len, term, len) == 0) {
found = true;
break;
}
}
}
}
buffer[i] = 0;
dbg.print(buffer);
return found;
}
//Celsius to Fahrenheit conversion
double Fahrenheit(double celsius)
{
return 1.8 * celsius + 32;
}
//Celsius to Kelvin conversion
double Kelvin(double celsius)
{
return celsius + 273.15;
}
double dewPoint(double celsius, double humidity)
{
// (1) Saturation Vapor Pressure = ESGG(T)
double RATIO = 373.15 / (273.15 + celsius);
double RHS = -7.90298 * (RATIO - 1);
RHS += 5.02808 * log10(RATIO);
RHS += -1.3816e-7 * (pow(10, (11.344 * (1 - 1/RATIO ))) - 1) ;
RHS += 8.1328e-3 * (pow(10, (-3.49149 * (RATIO - 1))) - 1) ;
RHS += log10(1013.246);
// factor -3 is to adjust units - Vapor Pressure SVP * humidity
double VP = pow(10, RHS - 3) * humidity;
// (2) DEWPOINT = F(Vapor Pressure)
double T = log(VP/0.61078); // temp var
return (241.88 * T) / (17.558 - T);
}
double dewPointFast(double celsius, double humidity)
{
double a = 17.271;
double b = 237.7;
double temp = (a * celsius) / (b + celsius) + log(humidity*0.01);
double Td = (b * temp) / (a - temp);
return Td;
}
void setup() {
pinMode(13, OUTPUT); // For debugging purposes w/o USB
// Set baud rates
digitalWrite(13, HIGH);
esp.begin(9600);
dbg.begin(9600);
ser.begin(9600);
dbg.println("DEBUG: Running Setup");
// Reset ESP, Test, Configure, Connect, Start Server
esp.println("AT+RST"); // Reset
waitForEsp(4000);
esp.println("AT"); // Test
waitForEsp(2000);
esp.println("AT+CWMODE=1"); // Set to client mode
waitForEsp(2000);
esp.println("AT+CWJAP=\"" + ssid + "\",\"" + pass + "\""); // Join AP
delay(8000); // Sometimes joining is really slow...
waitForEsp(6000);
esp.println("AT+CIPMUX=1"); // Allow multiple connections
waitForEsp(2000);
esp.println("AT+CIPSERVER=1," + serverPort); // Start server on port
waitForEsp(2000);
esp.println("AT+CIFSR");
waitForEsp(1000);
dbg.println("DEBUG: Setup complete\n\n");
digitalWrite(13, LOW);
ser.println("AT+RST");
}
void loop()
{
// blink LED on board
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
int lightLevel = analogRead(A0);
Serial.println("\n");
int chk = DHT11.read(DHT11PIN);
Serial.print("Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Serial.println("OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Serial.println("Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Serial.println("Time out error");
break;
default:
Serial.println("Unknown error");
break;
}
Serial.print("Humidity (%): ");
Serial.println((float)DHT11.humidity, 2);
Serial.print("Temperature (°C): ");
Serial.println((float)DHT11.temperature, 2);
Serial.print("Temperature (°F): ");
Serial.println(Fahrenheit(DHT11.temperature), 2);
Serial.print("Temperature (°K): ");
Serial.println(Kelvin(DHT11.temperature), 2);
Serial.print("Dew Point (°C): ");
Serial.println(dewPoint(DHT11.temperature, DHT11.humidity));
Serial.print("Dew PointFast (°C): ");
Serial.println(dewPointFast(DHT11.temperature, DHT11.humidity));
Serial.print("Light Level: ");
Serial.println(lightLevel);
esp_8266();
}
void esp_8266()
{
// convert to string
char buf[32];
String strTemp = dtostrf( DHT11.temperature, 4, 1, buf);
Serial.print(strTemp);
Serial.println(" C");
// TCP connection
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += "184.106.153.149"; // api.thingspeak.com
cmd += "\",80";
ser.println(cmd);
if(ser.find("Error")){
Serial.println("AT+CIPSTART error");
return;
}
// prepare GET string
String getStr = "GET /update?api_key=";
getStr += apiKey;
getStr +="&field1=";
getStr += String(strTemp);
getStr += "\r\n\r\n";
// send data length
cmd = "AT+CIPSEND=";
cmd += String(getStr.length());
ser.println(cmd);
if(ser.find(">")){
ser.print(getStr);
}
else{
ser.println("AT+CIPCLOSE");
// alert user
Serial.println("AT+CIPCLOSE");
}
// thingspeak needs 15 sec delay between updates
delay(5000);
}
//
// END OF FILE
//
答案 0 :(得分:0)
I don't have this device nor a way to test the results but a simple Google search resulted in the following:
AT+CWJAP="<access_point_name>","<password>"
答案 1 :(得分:0)
您需要更改:
String ssid = "ATL-WL";
String pass = "atl#yd81";
无论你的ssid和通行证是什么,你的WiFi。我也恳请你改变:
String apiKey = "20YMPJA0XII0IEF4";
String ssid = "ATL-WL";
String pass = "atl#yd81";
现在你已将它们发布到interwebz了。
您还应该确保从thingspeak.com获得新的API密钥。
您还需要确保在setup()循环中正确处理CR / LF。当可能是错误的串行连接设置时,您可能会错误地假设代码不正确。