iot - I'm trying to send temperature data from arduino to thingspeak through ESP8266, But I don't have code to configure my wifi. Please help me -
i'm trying send temperature data arduino thingspeak through esp8266, don't have code configure wifi. i'm searching code last 2 days, not working. please provide me code connect local wifi using ssid , password.
#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 = 0; int len = strlen(term); while (millis() < t + timeout) { if (esp.available()) { buffer[i++] = esp.read(); if (i >= len) { if (strncmp(buffer + - len, term, len) == 0) { found = true; break; } } } } buffer[i] = 0; dbg.print(buffer); return found; } //celsius fahrenheit conversion double fahrenheit(double celsius) { return 1.8 * celsius + 32; } //celsius 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 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 = 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); // 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 client mode waitforesp(2000); esp.println("at+cwjap=\"" + ssid + "\",\"" + pass + "\""); // join ap delay(8000); // joining 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 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 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 //
i don't have device nor way test results simple google search resulted in following:
at+cwjap="<access_point_name>","<password>"
Comments
Post a Comment