Arduinos IDE Serial Monitor works while XCode popen() does not. - xcode

I am trying to communicate with my Arduino through popen() function. I wrote a simple Mac App in XCode:
- (IBAction)ButtonPressed:(id)sender {
popen("echo hello world > /dev/tty.usbmodem1411", "r");
}
And here is the Arduino Code:
int redPin = 8;
int greenPin = 9;
int bluePin = 10;
int inByte = 0; // for incoming serial data
void setup()
{
pinMode(redPin, OUTPUT);
pinMode(greenPin, OUTPUT);
pinMode(bluePin, OUTPUT);
Serial.begin(8000);
}
void loop()
{
if (Serial.available() > 0) {
// get incoming byte:
inByte = Serial.read();
Serial.print(inByte);
setColor(inByte,inByte,inByte);
delay(1000);
setColor(0,0,0);
}
}
void setColor(int red, int green, int blue)
{
analogWrite(redPin, red);
analogWrite(greenPin, green);
analogWrite(bluePin, blue);
}
Now, when I try to use Arduino Serial Monitor and write some gibberish there my LED's light up fine and I see that it is working. When I run my XCode program, press the button, nothing happens. I did set break points and I did check that the line of code gets executed. I double checked the serial port, it is all fine, but no luck. It still does not work.

Basically I solved the problem by using IOKit/ioctl. I am still not sure why popen() did not work, might be because Arduino IDE was using the port and thus it was not available, however IOKit works like a charm. Plus it allows more control and flexibility, you can actually search for ports before you start and see if they are available. If anyone runs in to the same problem check http://playground.arduino.cc/Interfacing/Cocoa#IOKit. Thanks for all help.

Related

Wifimanager 8266 move to esp32 always connected. Disconnects or drops connection

I have a project on a Wemos d1 mini. I am using the WiFi manager and for the most part works fine.
I have 2 issues I’m trying to fix:
I’m trying to move all the code to lolin esp d32. But the WiFi manager broadcasts the station only for a minute and then if I needed to connect to it I have to reset the board. Can someone help me resolve this issue so it works like I had it on the d1
when it was on the d1 it would disconnect or lose connection. Then I would have to tee tee the credentials again if it failed to connect after so many tries
I want to move the code to an ESP32 and fix these issues. I need to have a solid, reliable connection with the WiFi.
A) possibly solve this disconnection issue possibly with a line of code for high power mode?
B) make a code that checks if it is still connects if not to reconnect t rather than send it back to the cridential page so basically solve the down time by some logic that will maintain the always connected situation.
C) I want to add a line of code to have an indicator light turn on if it can’t connect or loses connection and turns off when there is a connection.
Here is what I have so far for the main.cpp
#include <WiFiManager.h>
void setup() {
Serial.begin(115200);
Files.start();
Lights.begin();
UI.begin();
Auth.begin();
COM.begin();
Serial.print(F("\n\n====================<<<<<<<< WiFi Manager >>>>>>=====================\n\n"));
factoryTest();
Serial.println(FW_VERSION);
#ifdef COLORCODE
Lights.alertStatus = ALERT_BOOTUP;
Lights.alert();
#endif
WiFiManager wifiManager;
//wifiManager.setConfigPortalBlocking(false);
wifiManager.autoConnect("Goal Light");
//while (WiFi.status() != WL_CONNECTED) {
#ifdef COLORCODE
Lights.alertStatus = ALERT_WIFI;
if (wifiManager.getWiFiIsSaved()) {
Network.loop();
}
Lights.loop();
#endif
//wifiManager.process();
//}
Lights.alertStatus = ALERT_ERROR;
Lights.status = LIGHT_PAUSED;
Files.update();
if (Files.check("/user.txt") && Files.check("/password.txt")) {
Updater OTA;
}
Auth.authenticate();
UI.buildWeb();
}
int period = 1500;
unsigned long time_now = 0;
void loop() {
if (millis() - time_now > period) {
time_now = millis();
}
Sport.loop();
UI.wss->loop();
dnsServer.processNextRequest();
server.handleClient();
SB.loop();
Lights.loop();
#ifdef COLORCODE
Network.loop();
#endif
}
I have platformio that it was made on.
When I move it to my esp32 code I’m getting error for things Im not sure how to fix .

How to read register MD02 using ModBus ESP32?

I have an MD02 sensor (SHT20). In the storefront it says that this sensor is part of the MD02 series and not the XY-MD02.
But the store description says it can be configured according to the XY-MD02 register. After I tried, the register couldn't be used on the modbus poll. I used the register datasheet on the web http://www.sah.rs/media/sah/techdocs/xy-md02-manual.pdf
I also tried to read Modbus using HW0519 and ESP32, but the result is still the same. The register does not issue any output.
My Code:
#define RXD2 16
#define TXD2 17
byte ByteArray[250];
int ByteData[20];
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
}
void loop() {
// put your main code here, to run repeatedly:
delay(1000);
byte msg[] = {0x01,0x04,0x00,0x01,0x00,0x01,0x60,0x0A};
int i;
int len=8;
Serial.println("Sending Data...");
for(i=0 ; i < len ; i++){
Serial2.write(msg[i]);
Serial.print("[");
Serial.print(i);
Serial.print("]");
Serial.print("=");
Serial.print(String(msg[i],HEX));
}
len = 0;
Serial.println();
Serial.println();
int a = 0;
while(Serial2.available()){
ByteArray[a] = Serial2.read();
a++;
}
int b = 0;
String Register;
Serial.println("Receiving Data...");
for(b=0;b<a;b++){
Serial.print("[");
Serial.print(b);
Serial.print("]");
Serial.print("=");
Register = String(ByteArray[b],HEX);
Serial.print(Register);
Serial.print(" ");
}
Serial.println();
Serial.println();
}
I've made sure the wiring diagram is correct. How to fix it? I'm very confused, because there are no relevant solutions on the internet.
i have same problem. But, i solved this.
The solution
Change your modbus poll with odd/even parity. I swear that the description is wrong.
That the result if changing your parity
Temp/Hum have 100 Resolution, just divide by 100.
Change your code using SERIAL_8E1 or SERIAL_8O1
The code:
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial2.begin(9600, SERIAL_8E1, RXD2, TXD2);
}
Have a nice day!!!
Although the manufacturer claimed the device worked on 9600 8N1 - my device only worked on 9600 8N2 (or 8X1 + any parity check). In addition, a large timeout for waiting for a response is required (in my case, a stable response was only with a timeout of 1000 ms or more).

How to add two physical buttons to AC fan dimmer sketch and update the corresponding slider/step widget once it’s pressed

There are a lot of AC Fan dimmer codes are available in internet with zero cross detection and runs by Blynk app as well.
Problem is All those are only controllable by wifi (with internet) , rather have no manual control (without internet) at all.
I share a code below for AC fan dimmer which is runs by blynk app (Board NodeMCU) . It is only runs when wifi is available, i.e it has no manual contro. I am trying to improve/modify the same code by adding two physical push buttons to control Fan speed manually when internet is not available. In this case I am unable to modify the codes for these two push buttons which also capable to increase and decrease the fan speed along with the Blynk app slider button. Can anyone help/Guide me to develop this.
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define triacPulse 4 //D2
#define ZVC 12 //D6
int Slider_Value;
int dimming;
int x = 0;
char auth[] = "AUTH TOKEN"; // You should get Auth Token in the Blynk App.
char ssid[] = "SSID"; // Your WiFi credentials.
char pass[] = "PASS"; // Set password to "" for open networks.
BLYNK_WRITE(V1) // function to assign value to variable Slider_Value whenever slider changes position
{
Slider_Value = param.asInt(); // assigning incoming value from pin V1 to a variable
}
void setup()
{
pinMode(ZVC, INPUT_PULLUP);
//digitalWrite(2, INPUT_PULLUP); // pull up
pinMode(triacPulse, OUTPUT);
Serial.begin(9600);
Blynk.begin(auth, ssid, pass);
attachInterrupt(digitalPinToInterrupt(ZVC), acon, FALLING); // attach Interrupt at PIN2
}
void loop()
{
Blynk.run();
// When the switch is closed
dimming = map(Slider_Value, 0, 100, 7200, 200);
}
void acon()
{
// Serial.println("REad");
delayMicroseconds(dimming); // read AD0
digitalWrite(triacPulse, HIGH);
delayMicroseconds(50); //delay 50 uSec on output pulse to turn on triac
digitalWrite(triacPulse, LOW);
// Serial.println(digitalRead(triacPulse));
}

Arduino Output issue

I am facing a total weird problem one set of code is running and other ain't.
This code is working:
int pin = 2;
void setup() {
// put your setup code here, to run once:
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
}
And at the same time this is not working:
int pin = 2;
void setup() {
// put your setup code here, to run once:
pinMode(pin, OUTPUT);
digitalWrite(pin, LOW);
}
void loop() {
// put your main code here, to run repeatedly:
digitalWrite(pin,HIGH);
delay(5000);
digitalWrite(pin,LOW);
delay(5000);
}
Try putting an LED on your D2 pin to check whether it lights up or not. Make sure to add a resistor (220 ohm or 330 ohm) before the LED. Also, LED has polarity. The small leg should be towards the ground and the long leg (anode) should be towards the D2 pin.
If you don't have an LED or resistor, try using Serial.print() to display whether the system is going through your code or not. You can view your serial response through your serial monitor.

Raspberry Pi Ultimate GPS Hat - UART buffer is completely filled with 0 values

In the meanwhile, I bought two Raspberry Pi Ultimate GPS Hat. I thought the first GPS Hat might be broken, but both of them shows the same behaviour - the buffer received from the UART, is completely filled with 0 values (512 bytes)!
See processBuffer(byte[] buffer, int count) Method in the NmeaGpsModule class.
private void processBuffer(byte[] buffer, int count) {
for (int i = 0; i < count; i++) {
if (mParser.getFrameStart() == buffer[i]) {
handleFrameStart();
} else if (mParser.getFrameEnd() == buffer[i]) {
handleFrameEnd();
} else if (buffer[i] != 0){
//Insert all other characters except '0's into the buffer
mMessageBuffer.put(buffer[i]);
}
}
}
I use the GPS example with the following settings:
public static final int UART_BAUD = 9600;
public static final float ACCURACY = 2.5f; // From GPS datasheet
Any ideas? Whats wrong?
Try to follow this post: UART peripherals on Android Things for Raspberry Pi 3
The console is flooding the serial with junk. Need to disable the output to clean that up.

Resources