Arduino WiFi functions cause problems in setup() and loop() functions - arduino-esp32

#include <Arduino.h>
#include <WiFi.h>
#include <AsyncTCP.h>
#include <ESPAsyncWebServer.h>
const char *ssid = "name";
const char *password = "pass";
AsyncWebServer server(80);
void setup() {
pinMode(LED_BUILTIN, OUTPUT);
Serial.begin(115200);
Serial.println("setup0");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
Serial.println("setup1");
}
void loop() {
delay(2000);
digitalWrite(LED_BUILTIN, HIGH);
Serial.println("loop .... ");
delay(2000);
digitalWrite(LED_BUILTIN, LOW);
}
I am running the above code using the platformio ide in vscode. Whenever I upload the code using the wifi functions and then use the serial monitor to see the output, it prints nothing. If I exclude the wifi functions and upload the rest of the code then the serial monitor shows the correct println outputs. I cannot figure out if it has something to do with the network and what would I have to change or something else. Any ideas on why this occurs would be much appreciated, thanks.

Related

MH-z19 giving -1 value after some time

I'm trying make an mqtt co2 sensor for home assistant. And I got it to work, but only for a couple of hours. At some point it starts giving me -1 values instead of the proper co2 value. What could this be?
Below my code:
#include <Arduino.h>
#include <Mhz19.h>
#include <SoftwareSerial.h>
SoftwareSerial softwareSerial(D3, D4);
Mhz19 sensor;
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
const char* ssid = "ID";
const char* password = "pw";
const char* MQTT_BROKER = "192.168.1.25";//"test.mosquitto.org";
int mqttPort = 1883;
const char* mqttUser ="mqtt-user";
const char* mqttPassword ="pw";
WiFiClient espClient;
PubSubClient client(espClient);
void setup_wifi() {
delay(100);
// Connect to Wi-Fi network
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
randomSeed(micros());
Serial.println("");
Serial.println("Connected to Wi-Fi");
}
void reconnect() {
// Loop until connected
while (!client.connected()) {
Serial.print("Connecting to MQTT...");
if (client.connect("Test1", mqttUser, mqttPassword )) {
Serial.println("Connected to MQTT");
} else {
Serial.print("Failed MQTT connection with state: ");
Serial.println(client.state());
// Re-try in 3 seconds
delay(3000);
}
}
// Once connected, publish an announcement, and subscribe
client.publish("sensor/test", "Hello from ...");
client.subscribe("sensor/test");
}
void setup() {
Serial.begin(115200);
setup_wifi(); // Connect to Wi-Fi network
client.setServer(MQTT_BROKER, mqttPort);
//client.setCallback(callback);
reconnect(); // Connect to MQTT broker
softwareSerial.begin(9600);
sensor.begin(&softwareSerial);
sensor.setMeasuringRange(Mhz19MeasuringRange::Ppm_5000);
sensor.enableAutoBaseCalibration();
Serial.println("Preheating..."); // Preheating, 3 minutes
while (!sensor.isReady()) {
delay(50);
}
Serial.println("Sensor Ready...");
}
void loop() {
if (!client.connected()) { reconnect(); }
//client.publish("Time", ctime(&now));
auto carbonDioxide = sensor.getCarbonDioxide();
if (carbonDioxide >= 0) {
Serial.println(String(carbonDioxide) + " ppm");
}
char co2String[8];
dtostrf(carbonDioxide, 1,0, co2String);
//Serial.println(co2String);
client.publish("Floating/climate/CO2", co2String);
delay(2000);
}
I also tried flashing it via ESPHome, but then I run into issues from UART. The chip doesn't work propperly as long as the sensor is connected to it.
The sensor may not be receiving enough current. Try connecting the sensor directly to a separate breadboard power supply. The sensor seems to have a warm up time. Try waiting for longer with the independent power supply connected. I hope this will solve your issue.
Thank You,
Naveen PS

scanning the barcode and control led

I have a serial barcode scanner and Arduino Uno.
I need to configuration..one Correct barcode scanning led on.
my code is
void setup ()
{
Serial.begin (9600);
pinMode(3, OUTPUT);
pinMode(4, OUTPUT);
pinMode(5, OUTPUT);
}
void loop ()
{
if(Serial.available())
{
String command =Serial.readStringUntil('\n');
Serial.println(command);
if(command =="1010007")
{
digitalWrite(3, HIGH);
digitalWrite(4,LOW);}
else
if(command !="1010007")
{digitalWrite(4,HIGH);
digitalWrite(5,HIGH);
digitalWrite(3,LOW);}
}}
im scanning barcode 1010007 it showing serial monitor g⸮⸮⸮⸮ like this
how to fix it
plz help
and i try another code it showing correctly barcode,,,,,i scan 1010007 serial monitor also same 1010007
but led not on...code is below,,,
#include <SoftwareSerial.h>
SoftwareSerial barcode = SoftwareSerial(2, 3, true); // RX, TX
void setup()
{
Serial.begin(9600);
pinMode(3, OUTPUT);
barcode.begin(9600);
delay(1000);
}
void loop()
{
if(barcode.available())
{
char data = barcode.read();
Serial.write(data);
if(data=="101007")
{
digitalWrite(3, HIGH);
}
}}

Sim800l save input as a string

I'm trying to save my received SMS into a string but it doesn't work. This is my error message :
recive_0_1:50:28: error: invalid conversion from 'int' to 'char' [-fpermissive] exit status 1 invalid conversion from 'int' to 'char'
[-fpermissive] This report would have more information with "Show
verbose output during compilation" option enabled in File ->
Preferences.**
#include <SoftwareSerial.h>
#include <string.h>
char message[160]; // max size of an SMS
char* text;
String data;
//Create software serial object to communicate with SIM800L
SoftwareSerial mySerial(4, 5); //SIM800L Tx & Rx is connected to Arduino #3 & #2
void setup()
{
//Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
Serial.begin(9600);
//Begin serial communication with Arduino and SIM800L
mySerial.begin(9600);
Serial.println("Initializing...");
delay(1000);
mySerial.println("AT"); //Once the handshake test is successful, it will back to OK
updateSerial();
mySerial.println("AT+CMGF=1"); // Configuring TEXT mode
updateSerial();
mySerial.println("AT+CNMI=1,2,0,0,0"); // Decides how newly arrived SMS messages should be handled
updateSerial();
}
void loop()
{
updateSerial();
}
void updateSerial()
{
// char c;
delay(500);
while (Serial.available())
{
mySerial.write(Serial.read());//Forward what Serial received to Software Serial Port
//char data=Serial.read();
//Serial.println(data,DEC);
}
while(mySerial.available())
{
Serial.write(mySerial.read());//Forward what Software Serial received to Serial Port
text=mySerial.read();
Serial.print(text);
//}
}
}
It looks like you're running into problems from trying to convert integers to characters. I use the below code to read from my SIM card to my SIM900 device:
1st: my software serial is defined and my setup script run:
SoftwareSerial gprsSerial(7, 8);
void setup()
{
pinMode(13, OUTPUT);
gprsSerial.begin(19200);
Serial.begin(19200);
delay(200);
// set up your AT COMMANDS HERE - I HAVE NOT INCLUDED THESE
}
Then I run my loop function as follows, calling the additional functions specified thereafter, :
void loop() {
readSMS();
delay(2000);
}
void toSerial(){
delay(200);
if(gprsSerial.available()>0){
textMessage = gprsSerial.readString();
delay(100);
Serial.print(textMessage);
}
}
void readSMS() {
textMessage = "";
gprsSerial.print("AT+CSQ\r");
toSerial();
gprsSerial.print("AT+CMGF=1\r");
toSerial();
gprsSerial.println("AT+CNMI=2,2,0,0,0\r");
delay(2000);
toSerial();
}
With the above you should not get any data type conversion hassles.

Cannot set hostname for ESP8266

I am facing a problem, as setting host name for my ESP8266 is not working. Even though when I'm trying to connect through default host name "ESP_xxxx", it's not working.
Actually when I upload my code with my mobile hotspot SSID and password then it's working fine, but as soon as I gave the SSID and password of my router then it's not working.
Here's my code (setup part):
#include <ESP8266WiFi.h>
const char* ssid = "xxxxxx";
const char* password = "xxxxxx";
int ledPin = 13; // GPIO13
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
// Connect to WiFi network
Serial.println();
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.hostname("xyz");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
// Start the server
server.begin();
Serial.println("Server started");
// Print the IP address
Serial.print("Use this URL to connect: ");
Serial.print("http://");
Serial.print(WiFi.localIP());
Serial.println("/");
Serial.println(WiFi.hostname());
}
Try this:
#include <ESP8266WiFi.h> //https://github.com/esp8266/Arduino
//
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include "WiFiManager.h" //https://github.com/tzapu/WiFiManager
void configModeCallback (WiFiManager *myWiFiManager)
{
Serial.println("Entered config mode");
Serial.println(WiFi.softAPIP());
Serial.println(myWiFiManager->getConfigPortalSSID());
}
//
void setup()
{
Serial.begin(115200);
//
WiFiManager wifiManager;
WiFi.hostname("myhostname");
//
//wifiManager.resetSettings();
//
//
wifiManager.setAPCallback(configModeCallback);
if (!wifiManager.autoConnect("myhostname"))
{
Serial.println("failed to connect and hit timeout");
// reset
ESP.reset();
delay(1000);
}
//
Serial.println("connected...yeey :)");
}
void loop()
{
//
}
Stumbled upon this issue and below is the code which works for me.
WiFi.disconnect(true);
WiFi.begin(ssid, password);
WiFi.setHostname(device);`
Also came across below code, with this line the module is not receiving any IP. Hence deleted it.
WiFi.config(INADDR_NONE, INADDR_NONE, INADDR_NONE);
Running on Arduino 1.8.15 and esp8266 board version 3.0.1
Try using mDNS instead.
Include the mDNS library
#include <ESP8266mDNS.h>
Then in the setup after you connected to WiFi, start mDNS like this.
if (!MDNS.begin("your-desired-hostname")) {
Serial.println("Error setting up MDNS responder!");
}

How to convert the incoming characters coming from gsm module into a string?

Its my first time to use gsm shield to my arduino so I'm kinda confused so i need directions. My aim is to read the message sent to my gsm shield then compare that message into a specific string. if they are the same, arduino will do something. Example is, the GSM shield received a text message containing STATUS, the arduino will do something. The problem I'm stuck up with now is how to read the incoming characters from the gsm module into a single string then compare that string into a specific word. I have this code as of now.
#include <SoftwareSerial.h>
#include <String.h>
char inchar[255];
SoftwareSerial cell(2,3);
int led1 = 22;
#define powerOn 4
int i;
//char comparestring[160];
char command[]={'S','T','A','T','U','S','\0'}; // this is a string for command ended with null terminator
void setup()
{
// ilagay sa loob ng setup
digitalWrite(powerOn, HIGH);
delay(1500);
digitalWrite(powerOn, LOW);
delay(5000);
pinMode(led1, OUTPUT);
digitalWrite(led1, LOW);
Serial.begin(9600);
cell.b-egin(9600);
delay(30000);
cell.println("AT+CMGF=1"); // set SMS mode to text
delay(200);
cell.println("AT+CNMI=1,2,0,0,0 "); // set module to send SMS data to serial out upon receipt
delay(200);
Serial.println("GSM SHIELD IS NOW OK AND READY");
}
void loop()
{
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
{
digitalWrite(led1, HIGH);
cell.write("AT+CMGS=\"");
cell.write("09267955775");
cell.write("\"\r");
delay(1000);
cell.write("\nTerminal Monitoring System");
delay(1000);
cell.write(0x1A); // End the SMS with a control-z
}
else
{
Serial.println("\nInvalid Keyword! Type ?");
dig-italWrite(led1, LOW);
}
}
}
these codes are the codes that supposed to do the trick but i think its not working. I hope you can teach me the right way. Thank you!
while(cell.available() >0)
{
inchar[i]=cell.read();
i++;
inchar[i] = '\0';
Serial.print(inchar);
if (inchar==command)
inchar is a string so try using strcmp(inchar,command) for comparing the two.

Resources