Hi all, I am trying to connect a SW-240 vibration sensor to Arduino UNO and send its data to ThingSpeak via ESP8266-01 for analysis. I took reference from 2 codes, one is sending fixed variable continuously from ESP8266 to ThingSpeak, while another one is Arduino receiving sensor measurement variables and displaying on Serial Monitor.
However, when I combine and amend the 2 codes, errors appear. Below are the errors.
Arduino_Thingspeak_Vibration_Sensor:28: error: variable or field 'SentOnCloud' declared void
void SentOnCloud(measurement)
^
Arduino_Thingspeak_Vibration_Sensor:28: error: 'measurement' was not declared in this scope
void SentOnCloud(measurement)
^
Arduino_Thingspeak_Vibration_Sensor:28: error: variable or field 'SentOnCloud' declared void
void SentOnCloud(measurement)
^
Arduino_Thingspeak_Vibration_Sensor:28: error: 'measurement' was not declared in this scope
void SentOnCloud(measurement)
^
exit status 1
variable or field 'SentOnCloud' declared void
I think its something related to wrong use of function declaration. But I am really basic in programming and hence cant really figure out the problem of ‘void function ( string () )’ and ‘void function ( “long variable” )’.
Can anyone please help to solve the bugs? Really really appreciated!
Here are my codes.
#include <SoftwareSerial.h>
#define _baudrate 9600
#define _rxpin 4
#define _txpin 5
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
//*-- IoT Information
#define SSID "gloriachu"
#define PASS "0001984085235"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
// 使用 GET 傳送資料的格式
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=S0ZOGPHMTLG31SVM";
int ledPin = 13;
int EP =9;
long TP_init(){
delay(10);
long measurement=pulseIn (EP, HIGH); //wait for the pin to get HIGH and returns measurement
return measurement;
}
boolean connectWiFi()
{
debug.println("AT+CWMODE=1");
Wifi_connect();
}
void SentOnCloud(measurement)
{
// 設定 ESP8266 作為 Client 端
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
if( debug.find( "Error" ) )
{
Serial.print( "RECEIVED: Error\nExit1" );
return;
}
cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
debug.print( "AT+CIPSEND=" );
debug.println( cmd.length() );
if(debug.find( ">" ) )
{
Serial.print(">");
Serial.print(cmd);
debug.print(cmd);
}
else
{
debug.print( "AT+CIPCLOSE" );
}
if( debug.find("OK") )
{
Serial.println( "RECEIVED: OK" );
}
else
{
Serial.println( "RECEIVED: Error\nExit2" );
}
}
void Wifi_connect()
{
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
Loding("Wifi_connect");
}
void Loding(String state){
for (int timeout=0 ; timeout<10 ; timeout++)
{
if(debug.find("OK"))
{
Serial.println("RECEIVED: OK");
break;
}
else if(timeout==9){
Serial.print( state );
Serial.println(" fail...\nExit2");
}
else
{
Serial.print("Wifi Loading...");
delay(500);
}
}
}
void sendDebug(String cmd)
{
Serial.print("SEND: ");
Serial.println(cmd);
debug.println(cmd);
}
void setup() {
pinMode(ledPin, OUTPUT);
pinMode(EP, INPUT); //set EP input for measurment
Serial.begin( _baudrate );
debug.begin( _baudrate );
sendDebug("AT");
Loding("sent AT");
connectWiFi();
Serial.println("----------------------Vibration data collection begin------------------------");
}
void loop() {
long measurement =TP_init();
delay(100); // 60 second
Serial.print("measurment = ");
Serial.println(measurement);
SentOnCloud(measurement);
if (measurement > 1000){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
These are the 2 reference codes. They both work fine individually.
ESP8266 ThingSpeak
#include <SoftwareSerial.h>
#define _baudrate 9600
#define _rxpin 4
#define _txpin 5
SoftwareSerial debug( _rxpin, _txpin ); // RX, TX
//*-- IoT Information
#define SSID "gloriachu"
#define PASS "0001984085235"
#define IP "184.106.153.149" // ThingSpeak IP Address: 184.106.153.149
// 使用 GET 傳送資料的格式
// GET /update?key=[THINGSPEAK_KEY]&field1=[data 1]&filed2=[data 2]...;
String GET = "GET /update?key=S0ZOGPHMTLG31SVM";
void setup() {
Serial.begin( _baudrate );
debug.begin( _baudrate );
sendDebug("AT");
Loding("sent AT");
connectWiFi();
}
void loop() {
delay(5000); // 60 second
SentOnCloud( String(5), String(9) );
}
boolean connectWiFi()
{
debug.println("AT+CWMODE=1");
Wifi_connect();
}
void SentOnCloud( String T, String H )
{
// 設定 ESP8266 作為 Client 端
String cmd = "AT+CIPSTART=\"TCP\",\"";
cmd += IP;
cmd += "\",80";
sendDebug(cmd);
if( debug.find( "Error" ) )
{
Serial.print( "RECEIVED: Error\nExit1" );
return;
}
cmd = GET + "&field1=" + T + "&field2=" + H +"\r\n";
debug.print( "AT+CIPSEND=" );
debug.println( cmd.length() );
if(debug.find( ">" ) )
{
Serial.print(">");
Serial.print(cmd);
debug.print(cmd);
}
else
{
debug.print( "AT+CIPCLOSE" );
}
if( debug.find("OK") )
{
Serial.println( "RECEIVED: OK" );
}
else
{
Serial.println( "RECEIVED: Error\nExit2" );
}
}
void Wifi_connect()
{
String cmd="AT+CWJAP=\"";
cmd+=SSID;
cmd+="\",\"";
cmd+=PASS;
cmd+="\"";
sendDebug(cmd);
Loding("Wifi_connect");
}
void Loding(String state){
for (int timeout=0 ; timeout<10 ; timeout++)
{
if(debug.find("OK"))
{
Serial.println("RECEIVED: OK");
break;
}
else if(timeout==9){
Serial.print( state );
Serial.println(" fail...\nExit2");
}
else
{
Serial.print("Wifi Loading...");
delay(500);
}
}
}
void sendDebug(String cmd)
{
Serial.print("SEND: ");
Serial.println(cmd);
debug.println(cmd);
}
Vibration Sensor
int ledPin = 13;
int EP =9;
void setup(){
pinMode(ledPin, OUTPUT);
pinMode(EP, INPUT); //set EP input for measurment
Serial.begin(9600); //init serial 9600
Serial.println("----------------------Vibration demo------------------------");
}
void loop(){
long measurement =TP_init();
delay(50);
Serial.print("measurment = ");
Serial.println(measurement);
if (measurement > 1000){
digitalWrite(ledPin, HIGH);
}
else{
digitalWrite(ledPin, LOW);
}
}
long TP_init(){
delay(10);
long measurement=pulseIn (EP, HIGH); //wait for the pin to get HIGH and returns measurement
return measurement;
}