Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Arduino <function> not declared in this scope (aka Arduino implicit forwarding declarations)#9105

Unanswered
Jadblack asked this question inQ&A
Discussion options

Hello everybody I am trying to compile this code with Arduino ide on a esp8266 but I get this error on line 45:
setup_wifi not declared in this scope
How can I fix it and however can someone help me to publish a reading of a rain sensor over Mqtt?

Thanks everyone

#include<ESP8266WiFi.h>#include<PubSubClient.h>#include<Arduino.h>#defineWIFI_SSID"wifissid"#defineWIFI_PASSWORD"password"#defineMQTT_SERVER"10.25.73.8"#defineMQTT_PORT1883// use 8883 for SSL#defineMQTT_USER"pi"//user for Mosquitto#defineMQTT_PASSWORD"raspberry"//passord for Mosquitto#definerain_topic"sensor/rain"//Topic Rainconstint sensorMin =0;// sensor minimumconstint sensorMax =1024;// sensor maximumchar message_buff[100];bool debug =true;WiFiClient espClient;PubSubClientclient(espClient);voidsetup() {Serial.begin(9600);pinMode(A0, INPUT);setup_wifi();client.setServer(MQTT_SERVER, MQTT_PORT);client.setCallback(callback);//Connecfion WiFivoidsetup_wifi() {delay(10);// We start by connecting to a WiFi networkSerial.println();Serial.print("Connecting to");Serial.println(WIFI_SSID);WiFi.mode(WIFI_STA);WiFi.begin(WIFI_SSID, WIFI_PASSWORD);while (WiFi.status() != WL_CONNECTED) {delay(500);Serial.print(".");}Serial.println("");Serial.println("WiFi connected");Serial.print("=> IP address:");Serial.print(WiFi.localIP());Serial.println("");}//Reconnexionvoidreconnect() {while (!client.connected()) {Serial.print("Attempting MQTT connection...");// Attempt to connectif (client.connect("ESP8266Client", MQTT_USER, MQTT_PASSWORD)) {Serial.println("Connected");}else {Serial.print("Failed, Error:");Serial.print(client.state());Serial.println(" Retrying MQTT connection in 5 seconds...");delay(5000);// Wait 5 seconds before retrying}}}voidloop() {if (!client.connected()){reconnect();}client.loop();int sensorReading =analogRead(A0);int range =map(sensorReading, sensorMin, sensorMax,0,3);// range value:switch (range) {case0:// Sensor getting wetSerial.println("Flood");client.publish(rain_topic,"Flood",true);break;case1:// Sensor getting wetSerial.println("Rain Warning");client.publish(rain_topic,"raining",true);break;case2:// Sensor dry - To shut this up delete the " Serial.println("Not Raining"); " below.Serial.println("Sun");client.publish(rain_topic,"sun",true);break;}delay(1000 *10);// delay between reads}// Start after receiving the messagevoidcallback(char* topic, byte* payload,unsignedint length) {int i =0;if ( debug ) {Serial.println("Message recu => topic:" +String(topic));Serial.print(" | longueur:" +String(length, DEC));}// create character buffer with ending null terminator (string)for (i =0; i < length; i++) {message_buff[i] = payload[i];}message_buff[i] ='\0';String msgString =String(message_buff);if ( debug ) {Serial.println("Payload:" + msgString);}}
You must be logged in to vote

Replies: 1 comment

Comment options

d-a-v
Mar 25, 2024
Collaborator

This is an Arduino code where it is not mandatory to declare function before their use.

This is made possible with an Arduino specific preprocessing on source code aimed at beginners and often counter-productive.

What you can do is adding this single linevoid setup_wifi(); before thesetup(){ function.

Similar issue:https://community.platformio.org/t/code-works-in-arduino-ide-but-not-platformio/1907

You must be logged in to vote
0 replies
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Category
Q&A
Labels
None yet
2 participants
@Jadblack@d-a-v

[8]ページ先頭

©2009-2025 Movatter.jp