I have used ESP8266 wifi module with Arduino UNO for IOT but i want run same program in ARDUINO MEGA 2560 but I i did not get result and did not upload data on server with arduino mega 2560 with esp8266 wifi module..
Please help me why is it not uploading data on server by arduino MEGA 2560.
#include <SoftwareSerial.h>
#include <stdlib.h>
// LED
int ledPin = 13;
// LM35 analog input
int lm35Pin = 0;
\
// replace with your channel's thingspeak API key
String apiKey = "NR8VOW08K766X1PW";
// connect 2 to TX of Serial USB
// connect 3 to RX of serial USB
SoftwareSerial ser(2, 3); // RX, TX
// this runs once
void setup() {
// initialize the digital pin as an output.
pinMode(ledPin, OUTPUT);
// enable debug serial
Serial.begin(9600);
// enable software serial
ser.begin(115200);
// reset ESP8266
ser.println("AT+RST");
}
// the loop
void loop() {
// blink LED on board
digitalWrite(ledPin, HIGH);
delay(200);
digitalWrite(ledPin, LOW);
// read the value from LM35.
// read 10 values for averaging.
int val = 0;
for(int i = 0; i < 10; i++) {
val += analogRead(lm35Pin);
delay(500);
}
// convert to temp:
// temp value is in 0-1023 range
// LM35 outputs 10mV/degree C. ie, 1 Volt => 100 degrees C
// So Temp = (avg_val/1023)5 Volts * 100 degrees/Volt
float temp = val50.0f/1023.0f;
// convert to string
char buf[16];
String strTemp = dtostrf(temp, 4, 1, buf);
Serial.println(strTemp);
// 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
}
// thingspeak needs 15 sec delay between updates
delay(16000);
}